创建mybatis项目遇到的错误

空指针错误

java.lang.NullPointerException
创建mybatis项目遇到的错误

public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //这里由于SqlSessionFactory已经声明过了,所以下面不用在声明,所以去掉
            //SqlSessionFactory  sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
       return sqlSessionFactory.openSession();
    }
}

useUnicode报错

创建mybatis项目遇到的错误
解决方法
在mybatis-config-xml配置文件中把useSSL和useUnicode都改为false代码如下

<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&amp;useUnicode=false&amp;characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/MY/dao/UserMapper.xml"/>
    </mappers>
</configuration>

具体为什么把useUnicode改为false就能用了我也还没有太理解,就是不改变编码就可以吗?

上一篇:mybatis01


下一篇:Java MyBatis 创建SqlSessionFactory和获取SqlSession的方法及示例代码