Mybatis批量新增的三種實(shí)現(xiàn)方式
導(dǎo)入依賴
<!-- 數(shù)據(jù)庫驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>Dao
@Repository // 代表持久層
public interface DeptMapper {
int addDept(Dept dept);
int foreachAdd(List<Dept> list);
}
記得在啟動(dòng)類上加
@MapperScan(“com.example.demo.dao”)
DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.DeptMapper">
<!--普通新增-->
<insert id="addDept" parameterType="com.example.demo.pojo.Dept">
INSERT INTO dept (dname, db_source) VALUES (#{dname},#{dbSource})
</insert>
<!--foreachMySql寫法-->
<insert id="foreachAdd" parameterType="java.util.List">
insert into dept (
dname,
db_source
)
values
<foreach collection="list" item="dept" index="index" separator="," >
(
#{dept.dname,jdbcType=VARCHAR},
#{dept.dbSource,jdbcType=VARCHAR}
)
</foreach>
</insert>
</mapper>實(shí)體類
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
private long deptno;
private String dname;
private String dbSource;
}
配置文件
# mysql 5 驅(qū)動(dòng)不同 com.mysql.jdbc.Driver # mysql 8 驅(qū)動(dòng)不同com.mysql.cj.jdbc.Driver、需要增加時(shí)區(qū)的配置 serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/db01?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis-plus.mapper-locations=classpath:mapping/*Mapper.xml
核心測(cè)試類
@SpringBootTest
class DemoApplicationTests {
@Autowired
private DeptMapper deptMapper;
//測(cè)試數(shù)據(jù)
public List<Dept> textData(){
ArrayList<Dept> deptList = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
Dept dept = new Dept();
dept.setDname("foreach");
dept.setDbSource("db02");
deptList.add(dept);
}
return deptList;
}
// 循環(huán)插入
@Test
public void add(){
List<Dept> textData = textData();//測(cè)試數(shù)據(jù)
long start = System.currentTimeMillis();//開始時(shí)間
for(Dept dept : textData){
deptMapper.addDept(dept);
}
System.out.println(System.currentTimeMillis() - start);//統(tǒng)計(jì)時(shí)間
}
// foreach標(biāo)簽
@Test
public void foreach(){
List<Dept> textData = textData();//測(cè)試數(shù)據(jù)
long start = System.currentTimeMillis();//開始時(shí)間
deptMapper.foreachAdd(textData);
System.out.println(System.currentTimeMillis() - start);//統(tǒng)計(jì)時(shí)間
}
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Test
public void testInsertBatch(){
List<Dept> textData = textData();//測(cè)試數(shù)據(jù)
long start = System.currentTimeMillis();//開始時(shí)間
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
DeptMapper studentMapperNew = sqlSession.getMapper(DeptMapper.class);
textData.stream().forEach(student -> studentMapperNew.addDept(student));
sqlSession.commit();
sqlSession.clearCache();
System.out.println(System.currentTimeMillis() - start);//統(tǒng)計(jì)時(shí)間
}
}總結(jié)
其實(shí)實(shí)際意義上來說,包括在程序里面for循環(huán)還是在sql里面for循環(huán)都不算是批量操作。
只有將ExecutorType設(shè)置為BATCH模式才是真正意義上的批量操作。
并且事實(shí)證明在sql循環(huán)時(shí)設(shè)置batch與否其實(shí)執(zhí)行時(shí)間差別不是很大,幾乎可以忽略不計(jì)。
所以其實(shí)如果不是特別要求性能??梢灾苯釉趕ql中使用for循環(huán)即可。
謹(jǐn)慎使用batch,如果需要使用batch,請(qǐng)?jiān)谛枰暮瘮?shù)上面設(shè)置batch,不要全局使用。
因?yàn)閎atch也是有副作用的。比如在Insert操作時(shí),在事務(wù)沒有提交之前,是沒有辦法獲取到自增的id,此外,對(duì)于update、delete無法返回更新、插入條數(shù)。這在某型情形下是不符合業(yè)務(wù)要求的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud項(xiàng)目集成Feign、Hystrix過程解析
這篇文章主要介紹了SpringCloud項(xiàng)目集成Feign、Hystrix過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Spring Boot 把配置文件和日志文件放到j(luò)ar外部
如果不想使用默認(rèn)的application.properties,而想將屬性文件放到j(luò)ar包外面,怎么做呢?下面小編給大家?guī)砹藘煞N方法解決Spring Boot 把配置文件和日志文件放到j(luò)ar外部問題,感興趣的朋友一起看看吧2018-02-02
Java詳細(xì)講解堆排序與時(shí)間復(fù)雜度的概念
本文主要介紹了java實(shí)現(xiàn)堆排序以及時(shí)間復(fù)雜度,堆排序這種排序算法是我們經(jīng)常用到的,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Required?request?body?is?missing的問題及解決
這篇文章主要介紹了Required?request?body?is?missing的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java enum的用法詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了Java enum的用法詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
SpringData JPA快速上手之關(guān)聯(lián)查詢及JPQL語句書寫詳解
JPA都有SpringBoot的官方直接提供的starter,而Mybatis沒有,直到SpringBoot 3才開始加入到官方模版中,這篇文章主要介紹了SpringData JPA快速上手,關(guān)聯(lián)查詢,JPQL語句書寫的相關(guān)知識(shí),感興趣的朋友一起看看吧2023-09-09

