MyBatis-Plus 提供了 InsertBatchSomeColumn 方法,通过 SQL 自动注入器接口 ISqlInjector 注入通用方法 SQL 语句,然后继承 BaseMapper 添加自定义方法,实现批量插入。 首先,需要自定义 SQL 注入器: java public class MySqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethod...
之前,开发项目使用的是tk-mapper,当使用批量操作时,通常使用insertList就可以了。但是,最近的项目使用的是mybaits-plus,在使用批量操作saveBatch的使用,却遇到了一个问题,这个一开始让我以为我的数据出现了重复,但是仔细看,不是数据出现了重复,而是因为有一个字段相同,报唯一索引字段重复插入 Duplicate entry。 下面是...
@OverridepublicList<AbstractMethod>getMethodList(Class<?> mapperClass, TableInfo tableInfo) {// super.getMethodList() 保留 Mybatis Plus 自带的方法List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);// 添加自定义方法:批量插入,方法名为 insertBatchSomeColumnmethodList.add(ne...
未修改代码前,使用默认方法 saveOrUpdateBatch(list) 每次在报错的情况下仅能插入最多1001行数据。 跟着杨老师的代码解决问题,由于mybits-plus升级,有个细节需要变更, InsertBatch 类中 tableInfo.getAllInsertSqlColumn(false) tableInfo.getAllInsertSqlProperty(false,null) 变更为: tableInfo.getAllInsertSqlColumn...
<insert id="insertStudent" parameterType="StudentEntity" useGeneratedKeys="true" keyProperty="studentID"> 1. 推荐使用这种用法。 另外,还可以使用selectKey元素。下面例子,使用mysql数据库nextval(‘student’)为自定义函数,用来生成一个key。 <!-- 插入学生 自动主键--> ...
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE)); return methodList; } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 2、将SQL注入器交给Spring容器 在MybatisPlusConfig类中,将刚才创建的SQL注入器InsertBatchSqlInjector 注册为一个bean。
insertBatchSomeColumn(userList); } 在上面的示例中,通过注入BaseMapper接口的实例,然后调用其insertBatchSomeColumn方法进行批量插入操作。该方法接受一个实体类列表作为参数,并指定要插入的属性。无论使用哪种方法进行批量插入,都应该注意处理可能出现的异常和错误,并确保数据的一致性和完整性。二、批量更新批量更新是...
MyBatis Plus中的insert用于向数据库中插入数据。使用insert方法时,需要传入一个实体对象作为参数,该实体对象包含了要插入的数据。根据实体类的注解或配置文件中的映射关系,MyBa...
mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl 2、测试插入的代码 @Test void testInsert() { UserEntity userEntity = new UserEntity(); userEntity.setName("pipizhen"); userEntity.setAge(10); userEntity.setEmail("ppz@qq.com"); ...
从源码可以看到,所谓的批量插入就是一个for循环插入,很明显,这不是我们想要结果。而当我们继续阅读mybatis-plus的源码可以发现,在com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn包中已经为我们实现了真正意义上的批量插入方法,这里就不贴实现的源码了,有兴趣的可以去看看。