简单的实现 mybatisplus实现真实的批量插入

总所周知,mybatisplus 的saveBatch()是一个伪批量插入,性能比较差。真实的批量插入需要for循环读取value 拼装成一条insert语句才插入。下面我将简单的介绍 使用mybatisplus实现真实的批量的步骤。

1.引入依赖,3.4.0之上的版本都可以

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

2.新建InsertBatchSqlInjector 类

@Component
public class InsertBatchSqlInjector extends DefaultSqlInjector {
  @Override
  public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
    List<AbstractMethod> methodList = super.getMethodList(mapperClass);
    methodList.add(new InsertBatchSomeColumn());
    return methodList;
  }
}

3.创建CustomBaseMapper接口

public interface CustomBaseMapper<T> extends BaseMapper<T> {

    /**
     * 批量插入
     * @param entityList 要插入的数据
     * @return 成功插入的数据条数
     */
    int insertBatchSomeColumn(List<T> entityList);
}

4.把CustomBaseMapper当成BaseMapper使用即可

需要在dao层或mapper层去继承CustomBaseMapper,如:
public interface TStudentDao extends CustomBaseMapper

5.通过basemapper使用即可

this.baseMapper.insertBatchSomeColumn(studentEntityList);
或者直接注入
@Autowired
TStudentDao studentDao
studentDao.insertBatchSomeColumn(studentEntityList);

最终配置sql日志,即可看到效果
mybatis:
#mapper配置文件
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
日志
在这里插入图片描述
可以看到一条sql更新了两个记录

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MybatisPlus 提供了批量插入的方法,具体操作如下: 1. 首先,需要定义一个实体类,该实体类需要继承 MybatisPlus 提供的 BaseMapper 接口。 2. 在实体类中,编写批量插入的方法。例如: ```java public interface UserMapper extends BaseMapper<User> { void batchInsert(List<User> userList); } ``` 3. 在 XML 配置文件中,编写批量插入的 SQL 语句,例如: ```xml <insert id="batchInsert" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user (name, age) VALUES <foreach collection="list" item="item" separator=","> (#{item.name}, #{item.age}) </foreach> </insert> ``` 4. 在业务逻辑中调用批量插入的方法即可,例如: ```java @Autowired private UserMapper userMapper; public void insertUsers(List<User> userList) { userMapper.batchInsert(userList); } ``` 以上就是 MybatisPlus 实现批量插入简单操作步骤。 ### 回答2: MyBatis Plus是一个在MyBatis基础上进行封装的持久层框架,提供了很多便捷的开发功能。在MyBatis Plus中,批量插入是一项比较常用的功能。 在MyBatis Plus中,批量插入可以通过使用mapper的批量插入方法来实现。一般而言,批量插入可以分为两种方式:使用XML映射文件和使用注解方式。 使用XML映射文件的方式,首先需要在XML中定义一个批量插入的语句,语句中通过foreach标签循环遍历插入的数据集合,然后再通过insert标签进行数据的插入。在程序中调用该批量插入语句即可实现批量插入操作。 使用注解方式的批量插入操作,可以通过在Mapper接口中定义一个@InsertProvider注解的方法来实现。在该方法中通过遍历数据集合,拼接插入语句,最后通过@Param注解将数据集合传递给插入语句进行批量插入。 无论是使用XML映射文件还是注解方式,批量插入操作都可以提高数据库插入的效率,减少了与数据库的交互次数,提高了数据插入的速度。同时,在使用批量插入时,需要注意的是要对需要插入的数据进行有效的封装和处理,以确保插入的数据是符合要求的。 总之,MyBatis Plus提供了方便的批量插入操作,可以通过XML映射文件或者注解方式来实现。通过批量插入,可以大大提高数据插入的效率,适用于需要一次性插入大量数据的场景。 ### 回答3: MyBatis Plus是一个基于MyBatis框架的增强工具,它提供了许多方便的功能,其中包括批量插入数据。 在使用MyBatis Plus进行批量插入时,我们可以采用以下步骤: 1. 创建实体类和对应的数据库表,确保实体类中的属性与表中的字段一一对应。 2. 在Mapper接口中添加批量插入的方法,方法的参数可以是一个实体类的List集合,表示要插入的数据。 ```java public interface MyMapper extends BaseMapper<MyEntity> { void batchInsert(List<MyEntity> list); } ``` 3. 在对应的Mapper XML文件中编写SQL语句,使用foreach标签来遍历List集合,并将实体类的属性值插入到数据库表中。 ```xml <insert id="batchInsert" parameterType="java.util.List"> insert into my_table (column1, column2) values <foreach collection="list" item="item" separator=","> (#{item.property1}, #{item.property2}) </foreach> </insert> ``` 4. 在Service层调用Mapper接口的方法,并传入要插入的数据。 ```java @Service public class MyService { @Autowired private MyMapper myMapper; public void batchInsert(List<MyEntity> list) { myMapper.batchInsert(list); } } ``` 通过以上步骤,我们就可以使用MyBatis Plus实现批量插入操作了。使用批量插入可以有效地减少数据库操作的次数,提高数据插入的效率。同时,使用MyBatis Plus的批量插入功能,可以简化开发流程,减少重复的代码编写。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值