wild84
2017-05-15 12:52
采纳率: 91.6%
浏览 3.0k
已采纳

使用mybatis时sqlsession总是空指针异常

我的xml配置文件如下:

 <?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 
<configuration> 
    <properties resource="mysql.properties"/>
    <settings>
        <setting name="logImpl" value="LOG4J"></setting>
    </settings>
    <typeAliases>
        <typeAlias alias="User" type="ssm.pojo.User" />
    </typeAliases>  
    <environments default="development"> 
        <environment id="development"> 
        <transactionManager type="JDBC"/> 
        <dataSource type="POOLED"> 
            <property name="driver" value="${driver}"/> 
            <property name="url" value="${url}"/> 
            <property name="username" value="${username}"/> 
            <property name="password" value="${password}"/> 
        </dataSource> 
        </environment> 
    </environments> 
    <mappers> 
        <mapper resource="ssm/dao/UserMapper.xml"/> 
    </mappers> 
</configuration> 

获取session代码如下:

 public class Test {
    private static SqlSessionFactory sf;
    static{
        try {
            InputStream inputStream=Resources.getResourceAsStream("mybatis-config.xml");
            sf= new SqlSessionFactoryBuilder().build(inputStream);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    @org.junit.Test
    public void test() {
        SqlSession session=sf.openSession(false);
        List<User> list=session.selectList("ssm.dao.UserMapper.selectAll");
        for (User user : list) {
            System.out.println(user.getUserName());
        }
    }

}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • AngusC· 2017-05-15 15:08
    已采纳

    是那句空指针 这句么

     List<User> list=session.selectList("ssm.dao.UserMapper.selectAll");
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题