MyBaties-Plus 批量入库

@

0、简介

MyBaties-Plus 是 MyBaties 的增强版,MyBaties 有的功能它都有,MyBaties 没有的功能它也有。MP 有许多优点,但是这里我只记录批量插入的方法,好处是大数据量速度相对来说很快,有兴趣的可以自己做下对比。实现步骤如下。

1、引入 POM

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

2、实体类

@TableName("t_a")
public class A{
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
}

3、Mapper 接口

public interface AMapperextends BaseMapper<A>{}

4、service 接口

public interface IAService extends IService<A> {}

5、Impl 实现类

public class AServiceImpl extends ServiceImpl<AMapper, A> implements IAService {}

6、MySQL 连接参数最后上加如下参数 (重要)

# &rewriteBatchedStatements=true 告诉 jdbc 要使用批处理
jdbc:mysql://ip:3306/t_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&rewriteBatchedStatements=true

7、使用批量插入

public class testService {
    @Autowired
    private IAService aService;
    
    public void test(List<A> list) {
        aService.saveBatch(list);
    }
}

记录如有不对烦请指出,先行感谢

posted @ 2022-01-23 14:11  charmsongo  阅读(104)  评论(0编辑  收藏  举报