### Error opening session. Cause: java.lang.NullPointerException

在测试mybatis SQL时遇到'Error opening session. Cause: java.lang.NullPointerException'的问题。错误源于在spring-context.xml中整合mybatis时,缺少数据库配置。解决方法是在mybatis-config.xml同级目录创建SqlMapConfig.xml,包含数据库配置,然后在Java代码中替换mybatis-config.xml为SqlMapConfig.xml。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在测试mybatis的SQL语句时,出现如下错误提示:

在这里插入图片描述
我的Java代码:

//测试查询
@Test
public void run1() throws Exception {
    // 加载配置文件
    InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
    // 创建SqlSessionFactory对象
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
    // 创建SqlSession对象
    SqlSession session = factory.openSession();
    // 获取到代理对象
    BlogMapper mapper = session.getMapper(BlogMapper.class);

    List<Blog> emps = mapper.selectByExample(null);
    for (Blog blog : emps) {
        System.out.println(blog);
    }
    // 关闭资源
    session.close();
    in.close();
}

错误原因是我在整合SSM框架的时候,把mybatis框架的部分配置,整合到了spring-context.xml配置文件中。下面是可以发现下面mybatis-config.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">
<!-- MyBatis的全局配置文件 -->
<configuration>

   <settings>
      <!-- 映射下划线到驼峰命名    last_name ==> lastName    -->
      <setting name="mapUnderscoreToCamelCase" value="true"/>
      <!-- 开启延迟加载 -->
      <setting name="lazyLoadingEnabled" value="true"/>
      <!-- 指定加载的属性是按需加载 -->
      <setting name="aggressiveLazyLoading" value="false"/>
      <!-- 二级缓存 -->
      <setting name="cacheEnabled" value="true"/>
   </settings>

   <!--使用typeAliases配置别名,它只能配置domain中类的别名 -->
   <typeAliases>
      <package name="com.online.domain"/>
   </typeAliases>

   <!--分页插件-->
   <plugins>
      <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
   </plugins>

</configuration>

解决办法:
在mybatis-config.xml的同级目录下,添加一个新的Mybatis配置文件SqlMapConfig.xml进行测试使用。
SqlMapConfig.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>
    <!-- 配置环境 -->
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatisdb"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <!-- 引入映射配置文件 -->
    <mappers>
        <package name="com.online.dao"/>
    </mappers>
</configuration>

然后将Java代码中的mybatis-config.xml换为SqlMapConfig.xml。

//测试查询
@Test
public void run1() throws Exception {
    // 加载配置文件
    InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
    // 创建SqlSessionFactory对象
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
    // 创建SqlSession对象
    SqlSession session = factory.openSession();
    // 获取到代理对象
    BlogMapper mapper = session.getMapper(BlogMapper.class);

    List<Blog> emps = mapper.selectByExample(null);

    for (Blog blog : emps) {
        System.out.println(blog);
    }
    // 关闭资源
    session.close();
    in.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值