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)文章
Java Spring-IOC容器與Bean管理之基于注解的方式案例詳解
這篇文章主要介紹了Java Spring-IOC容器與Bean管理之基于注解的方式案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了SpringBoot+easypoi實(shí)現(xiàn)數(shù)據(jù)的Excel導(dǎo)出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05java?NIO實(shí)現(xiàn)簡(jiǎn)單聊天程序
這篇文章主要為大家詳細(xì)介紹了java?NIO實(shí)現(xiàn)簡(jiǎn)單聊天程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11java枚舉類的屬性、方法和構(gòu)造方法應(yīng)用實(shí)戰(zhàn)
這篇文章主要介紹了java枚舉類的屬性、方法和構(gòu)造方法應(yīng)用,結(jié)合實(shí)例形式分析了java枚舉類的定義、構(gòu)造及相關(guān)應(yīng)用操作技巧,需要的朋友可以參考下2019-08-08Netty分布式flush方法刷新buffer隊(duì)列源碼剖析
這篇文章主要為大家介紹了Netty分布式flush方法刷新buffer隊(duì)列源碼剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03Java編程實(shí)現(xiàn)軌跡壓縮算法開放窗口實(shí)例代碼
這篇文章主要介紹了Java編程實(shí)現(xiàn)軌跡壓縮算法開放窗口實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11SpringBoot Filter修改返回內(nèi)容,解決請(qǐng)求卡死200的錯(cuò)誤
這篇文章主要介紹了SpringBoot Filter修改返回內(nèi)容,解決請(qǐng)求卡死200的錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07