Mybatis注解批量插入数据报空指针异常
TeacherDAO
package com.imooc.mybatis.dao;
import com.imooc.mybatis.entity.Teacher;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectKey;
import java.util.List;
public interface TeacherDAO {
@Insert({
"<script>",
"insert into teacher (name,sex,j_no,subject,grade,description) values",
"<foreach collection='list' item='item' index='index' separator=','>",
"(#{item.name},#{item.sex},#{item.jNo},#{item.subject},#{item.grade},#{item.description})",
"</foreach>",
"</script>"
})
@SelectKey(statement = "select last_insert_id()", before = false ,keyProperty = "id" ,resultType = Integer.class)
public int batchInsert(@Param("list") List<Teacher> list);
}
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">
<configuration>
<settings>
<!-- goods_id ==> goodsId 驼峰命名转换 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--设置默认指向的数据库-->
<environments default="c3p0_dev ">
<!--配置环境,不同的环境不同的id名字-->
<environment id="dev">
<!-- 采用JDBC方式对数据库事务进行commit/rollback -->
<transactionManager type="JDBC"></transactionManager>
<!--采用连接池方式管理数据库连接-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="wrx199704"/>
</dataSource>
</environment>
<environment id="c3p0_dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="com.imooc.mybatis.datasource.C3P0DataSourceFactory">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="wrx199704"/>
<property name="initialPoolSize" value="5"/>
<property name="maxPoolSize" value="20"/>
<property name="minPoolSize" value="5"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper class="com.imooc.mybatis.dao.GoodsDAO"></mapper>-->
<package name="com.imooc.mybatis.dao"/>
</mappers>
</configuration>
MybatisUtils
package com.imooc.mybatis.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
/**
* MyBatisUtils工具类,创建全局唯一的SqlSessionFactory对象
*/
public class MyBatisUtils {
//利用static(静态)属于类不属于对象,且全局唯一
private static SqlSessionFactory sqlSessionFactory = null;
//利用静态块在初始化类时实例化sqlSessionFactory
static {
Reader reader = null;
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
//初始化错误时,通过抛出异常ExceptionInInitializerError通知调用者
throw new ExceptionInInitializerError(e);
}
}
/**
* openSession 创建一个新的SqlSession对象
* @return SqlSession对象
*/
public static SqlSession openSession(){
//默认SqlSession对自动提交事务数据(commit)
//设置false代表关闭自动提交,改为手动提交事务数据
return sqlSessionFactory.openSession(false);
}
/**
* 释放一个有效的SqlSession对象
* @param session 准备释放SqlSession对象
*/
public static void closeSession(SqlSession session){
if(session != null){
session.close();
}
}
}
JUNITTest
@Test
public void testTeacherBatchInsert(){
SqlSession session = null;
try {
session = MyBatisUtils.openSession();
TeacherDAO teacherDAO = session.getMapper(TeacherDAO.class);
List list = new ArrayList();
for(int i=0;i<500;i++){
Teacher t = new Teacher();
t.setName("测试名字" + i);
t.setSex(i%2 == 0 ? "男" : "女");
t.setjNo(i+100);
t.setSubject(i%2 == 0 ? "语文" : "数学");
t.setGrade(i % 6 + "年级");
t.setDescription("测试备注" + i);
list.add(t);
}
int num = teacherDAO.batchInsert(list);
session.commit();
System.out.println("更新了" + num + "条数据");
} catch (Exception e){
e.printStackTrace();
} finally {
MyBatisUtils.closeSession(session);
}
}
报错信息:
正在回答
同学你好,代码不完整无法进行测试呢,建议将全部代码上传至问答区。出现空指针可能是数据源的问题,也就是配置文件中的问题,建议检查下environments的default与environment的id的书写是否一致。
另外建议同学选中对应代码语言后再上传代码。
祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星