日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

spring中使用mybatis實(shí)現(xiàn)批量插入的示例代碼

瀏覽:20日期:2023-09-04 08:26:00

有3種實(shí)現(xiàn)方式:foreach,spring事務(wù),以及ExecutorType.BATCH.

1. foreach方式

這種方式實(shí)際是對SQL語句進(jìn)行拼接,生成一個長長的SQL,對很多變量進(jìn)行綁定。如果數(shù)據(jù)量不大(1000個以內(nèi)),可以用這種方式。如果數(shù)據(jù)量太大,可能數(shù)據(jù)庫會報錯。

定義接口

public interface StudentMapper05 { public void insertStudent(List<Student> studentList);}

定義mapper

適用于Oracle數(shù)據(jù)庫

<insert id='insertStudent'> BEGIN <foreach collection='list' item='student' index='index' separator=''> INSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, #{student.name}, #{student.branch}, #{student.percentage}, #{student.phone}, #{student.email}); </foreach> END;</insert>

這個mapper的含義,就是把上送的studentList拼接成一個長SQL,拼成的SQL類似:

BEGININSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, ?, ?, ?, ?, ?);INSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, ?, ?, ?, ?, ?);INSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, ?, ?, ?, ?, ?);...END;

studentList有幾個,就會生成多少個insert語句拼接到一起,每個?都會進(jìn)行變量綁定,所以當(dāng)studentList中數(shù)據(jù)量較多時,生成的SQL會很長,導(dǎo)致數(shù)據(jù)庫執(zhí)行報錯。

dao

public class StudentDao05 { private StudentMapper05 studentMapper; // 省略getter和setter public void insertStudentList(List<Student> studentList) { studentMapper.insertStudent(studentList); }}

beans

mybatis-spring-05.xml:

<bean class='org.mybatis.spring.SqlSessionFactoryBean'> <property name='dataSource' ref='oracleDataSource' /> <property name='configLocation' value='classpath:mybatis/config/mybatis-config-05.xml'/></bean><bean class='org.mybatis.spring.mapper.MapperFactoryBean'> <property name='mapperInterface' value='com.ws.experiment.spring.mybatis.mapper.StudentMapper05' /> <property name='sqlSessionFactory' ref='sqlSessionFactory' /></bean><bean class='com.ws.experiment.spring.mybatis.dao.StudentDao05'> <property name='studentMapper' ref='studentMapper05' /></bean>

main函數(shù)

public static void main(String[] args) { String[] configFiles = new String[]{'spring-beans-config.xml', 'mybatis/mybatis-spring-05.xml'}; // 分別配置datasource和mybatis相關(guān)bean ApplicationContext context = new ClassPathXmlApplicationContext(configFiles); StudentDao05 studentDao = (StudentDao05)context.getBean('studentDao05'); int counts[] = new int[]{10, 50, 100, 200, 500, 1000, 2000, 3000, 5000, 8000}; for (int count : counts) { List<Student> studentList = new ArrayList<>(); for (int i = 0; i < count; i++) { Student st = new Student(); st.setName('name'); st.setBranch(''); st.setEmail(''); st.setPercentage(0); st.setPhone(0); studentList.add(st); } long startTime = System.currentTimeMillis(); studentDao.insertStudentList(studentList); long endTime = System.currentTimeMillis(); System.out.println('插入' + count + '筆數(shù)據(jù)耗時: ' + (endTime - startTime) +' ms'); }}

測試結(jié)果

插入100筆數(shù)據(jù)耗時: 197 ms插入200筆數(shù)據(jù)耗時: 232 ms插入500筆數(shù)據(jù)耗時: 421 ms插入1000筆數(shù)據(jù)耗時: 650 ms插入2000筆數(shù)據(jù)耗時: 1140 ms插入3000筆數(shù)據(jù)耗時: 27113 ms插入5000筆數(shù)據(jù)耗時: 98213 ms插入8000筆數(shù)據(jù)耗時: 301101 ms

2. 借助spring事務(wù)

借助spring事務(wù),插入一組數(shù)據(jù)

開啟spring事務(wù)

<bean class='org.springframework.jdbc.datasource.DataSourceTransactionManager'> <property name='dataSource' ref='oracleDataSource' /></bean><tx:annotation-driven transaction-manager='transactionManager' />

定義接口

public interface StudentMapper06 { public void insertStudent(@Param('student') Student student);}

mapper

<insert id='insertStudent'> INSERT INTO test_student(ID, NAME, BRANCH, PERCENTAGE, PHONE, EMAIL) VALUES (SEQ_ID.nextval, #{student.name}, #{student.branch}, #{student.percentage}, #{student.phone}, #{student.email})</insert>

dao

public class StudentDao06 { private StudentMapper06 studentMapper; // 省略getter和setter @Transactional // spring事務(wù)控制 public void insertStudentList(List<Student> students) { for (Student student : students) { studentMapper.insertStudent(student); } }}

beans

<bean class='org.mybatis.spring.SqlSessionFactoryBean'> <property name='dataSource' ref='oracleDataSource' /> <property name='configLocation' value='classpath:mybatis/config/mybatis-config-06.xml'/></bean><bean class='org.mybatis.spring.mapper.MapperFactoryBean'> <property name='mapperInterface' value='com.ws.experiment.spring.mybatis.mapper.StudentMapper06' /> <property name='sqlSessionFactory' ref='sqlSessionFactory' /></bean><bean class='com.ws.experiment.spring.mybatis.dao.StudentDao06'> <property name='studentMapper' ref='studentMapper06' /></bean>

main

測試結(jié)果

batchInsert001插入10筆數(shù)據(jù)耗時: 602 msbatchInsert001插入50筆數(shù)據(jù)耗時: 196 msbatchInsert001插入100筆數(shù)據(jù)耗時: 284 msbatchInsert001插入200筆數(shù)據(jù)耗時: 438 msbatchInsert001插入500筆數(shù)據(jù)耗時: 944 msbatchInsert001插入1000筆數(shù)據(jù)耗時: 1689 msbatchInsert001插入2000筆數(shù)據(jù)耗時: 3138 msbatchInsert001插入3000筆數(shù)據(jù)耗時: 4427 msbatchInsert001插入5000筆數(shù)據(jù)耗時: 7368 msbatchInsert001插入8000筆數(shù)據(jù)耗時: 11832 ms

3. 使用ExecutorType.BATCH

基本原理是SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);,設(shè)置BATCH方式的sqlSession

有三種設(shè)置方式:

3.1 在mybatis的config文件中設(shè)置

SqlSessionFactoryBean中可以配置配置文件:

<bean class='org.mybatis.spring.SqlSessionFactoryBean'> <property name='dataSource' ref='oracleDataSource' /> <property name='configLocation' value='classpath:mybatis/config/mybatis-config-06.xml'/></bean>

這個mybatis配置文件中,設(shè)置BATCH方式:

<configuration> <settings> <!-- 默認(rèn)打開BATCH的Executor --> <setting name='defaultExecutorType' value='BATCH' /> </settings> <mappers> <mapper /> </mappers></configuration>

這樣,默認(rèn)打開的sqlSession就都是BATCH方式的。再與spring的事務(wù)結(jié)合(參看上一節(jié)中的spring事務(wù)設(shè)置),就可以實(shí)現(xiàn)批量插入。

測試結(jié)果:

batchInsert001插入10筆數(shù)據(jù)耗時: 565 msbatchInsert001插入50筆數(shù)據(jù)耗時: 117 msbatchInsert001插入100筆數(shù)據(jù)耗時: 98 msbatchInsert001插入200筆數(shù)據(jù)耗時: 106 msbatchInsert001插入500筆數(shù)據(jù)耗時: 145 msbatchInsert001插入1000筆數(shù)據(jù)耗時: 132 msbatchInsert001插入2000筆數(shù)據(jù)耗時: 154 msbatchInsert001插入3000筆數(shù)據(jù)耗時: 163 msbatchInsert001插入5000筆數(shù)據(jù)耗時: 200 msbatchInsert001插入8000筆數(shù)據(jù)耗時: 250 ms

3.2 自己創(chuàng)建sqlSession,手工commit

SqlSessionFactory sqlSessionFactory = (SqlSessionFactory)context.getBean('sqlSessionFactory');SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);StudentMapper06 studentMapper = sqlSession.getMapper(StudentMapper06.class);for (int i = 0; i < count; i++) { Student st = new Student(); st.setName('name'); ... studentMapper.insertStudent(st);}sqlSession.commit();sqlSession.clearCache();sqlSession.close();

測試結(jié)果:

batchInsert002插入10筆數(shù)據(jù)耗時: 568 msbatchInsert002插入50筆數(shù)據(jù)耗時: 157 msbatchInsert002插入100筆數(shù)據(jù)耗時: 132 msbatchInsert002插入200筆數(shù)據(jù)耗時: 135 msbatchInsert002插入500筆數(shù)據(jù)耗時: 148 msbatchInsert002插入1000筆數(shù)據(jù)耗時: 139 msbatchInsert002插入2000筆數(shù)據(jù)耗時: 151 msbatchInsert002插入3000筆數(shù)據(jù)耗時: 139 msbatchInsert002插入5000筆數(shù)據(jù)耗時: 207 msbatchInsert002插入8000筆數(shù)據(jù)耗時: 299 ms

3.3 使用sqlSessionTemplate在XML文件中創(chuàng)建bean

創(chuàng)建一個SqlSessionTemplate,然后注入到MapperFactoryBean中,生成對應(yīng)的mapper:

<!-- 以ExecutorType.BATCH方式插入數(shù)據(jù)庫 --><bean class='org.mybatis.spring.SqlSessionTemplate'> <constructor-arg name='sqlSessionFactory' ref='sqlSessionFactory' /> <constructor-arg name='executorType' value='BATCH' /></bean><bean class='org.mybatis.spring.mapper.MapperFactoryBean'> <property name='mapperInterface' value='com.ws.experiment.spring.mybatis.mapper.StudentMapper06' /> <property name='sqlSessionTemplate' ref='batchSqlSessionTemplate' /></bean><bean class='com.ws.experiment.spring.mybatis.dao.StudentDao06'> <property name='studentMapper' ref='studentMapper06_batch' /></bean>

與spring的事務(wù)結(jié)合后(參看上一節(jié)中的spring事務(wù)設(shè)置),就可以實(shí)現(xiàn)批量插入

測試結(jié)果

batchInsert003插入10筆數(shù)據(jù)耗時: 651 msbatchInsert003插入50筆數(shù)據(jù)耗時: 133 msbatchInsert003插入100筆數(shù)據(jù)耗時: 124 msbatchInsert003插入200筆數(shù)據(jù)耗時: 129 msbatchInsert003插入500筆數(shù)據(jù)耗時: 144 msbatchInsert003插入1000筆數(shù)據(jù)耗時: 179 msbatchInsert003插入2000筆數(shù)據(jù)耗時: 229 msbatchInsert003插入3000筆數(shù)據(jù)耗時: 241 msbatchInsert003插入5000筆數(shù)據(jù)耗時: 216 msbatchInsert003插入8000筆數(shù)據(jù)耗時: 259 ms

到此這篇關(guān)于spring中使用mybatis實(shí)現(xiàn)批量插入的示例代碼的文章就介紹到這了,更多相關(guān)spring mybatis批量插入內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产综合欧美| 日韩激情一区| 欧美综合国产| 日韩av中文字幕一区| 欧美日韩激情| 日韩国产欧美一区二区| 欧美在线首页| 91精品一区国产高清在线gif| 久久国产乱子精品免费女| 美女国产一区| 偷拍欧美精品| 色偷偷偷在线视频播放| 精品视频免费| 精品一区二区三区亚洲| 国产精品久久久久久久久久久久久久久 | 伊人影院久久| av亚洲一区二区三区| 福利一区和二区| 精品国产99| 亚洲欧美视频| 一本色道精品久久一区二区三区| 91精品国产91久久久久久黑人| 中文一区一区三区高中清不卡免费| 青草久久视频| 日本久久二区| 日韩高清欧美激情| 91欧美在线| 日韩不卡视频在线观看| 日韩精品1区2区3区| 久久亚洲欧美| 欧美在线日韩| 天堂√8在线中文| 视频一区视频二区在线观看| 欧美亚洲精品在线| 99国产精品99久久久久久粉嫩| 一区二区三区四区在线看| 欧美日韩一二三四| 欧美.日韩.国产.一区.二区| 亚洲国产综合在线看不卡| 久久麻豆精品| 好看不卡的中文字幕| 99综合视频| 亚洲一区二区三区无吗| 1024精品久久久久久久久| 国产精品日本| 日韩在线观看中文字幕| 日韩高清欧美激情| 国产精品久久久久久久久免费高清 | 日韩欧美少妇| 欧美亚洲在线日韩| 美女网站视频一区| 欧美精品一区二区三区精品| 国产午夜久久| 欧美日韩调教| 久久久久久久久99精品大| 日韩精品亚洲专区| 日韩高清中文字幕一区二区| 欧美在线看片| 91视频一区| 国产精品亚洲片在线播放| 黄色av日韩| 成人在线视频免费看| 97se综合| 亚洲精品第一| 日韩伦理一区| 日韩一级精品| 午夜精品婷婷| 日韩精品免费一区二区夜夜嗨| 国产精品白丝av嫩草影院| 国产在线成人| 麻豆极品一区二区三区| 日韩精品一二区| 免费污视频在线一区| 久久国产三级| 亚洲深夜av| 免费看av不卡| 日韩综合一区二区| 激情婷婷久久| 高潮久久久久久久久久久久久久| 日韩影院在线观看| 久久精品高清| 精品久久不卡| 日韩av一二三| 人人爽香蕉精品| 国产综合欧美| 日韩黄色大片网站| 久久亚洲黄色| 国产日韩视频| 亚洲日本欧美| 亚洲一区二区三区高清| 日韩欧美看国产| 国产精品久久久久久久免费软件| 亚洲资源网站| 福利视频一区| 国产精品亚洲欧美| 激情欧美一区二区三区| 天海翼亚洲一区二区三区| 六月婷婷综合| 欧美欧美黄在线二区| 久久夜色精品| 天堂网在线观看国产精品| se01亚洲视频| 日本а中文在线天堂| 亚洲综合另类| 午夜精品成人av| 精品亚洲精品| 欧美日韩中文| 蜜桃久久av一区| 在线亚洲自拍| 亚洲福利久久| 色偷偷偷在线视频播放| 精品资源在线| 你懂的国产精品永久在线| 日本三级亚洲精品| 日本v片在线高清不卡在线观看| 蜜臀av性久久久久蜜臀aⅴ四虎| 欧美日韩国产探花| 婷婷综合五月| 亚洲精品网址| 黑丝美女一区二区| 国产精品av一区二区| 精品一区二区三区视频在线播放 | 日韩精品专区| 九九色在线视频| 香蕉久久精品| 尤物在线精品| 首页国产欧美久久| 亚洲免费毛片| 日本精品一区二区三区在线观看视频| 中文字幕日韩亚洲| 日韩欧美三区| 99国产精品自拍| 国产精品老牛| 99国产精品99久久久久久粉嫩| 99精品一区| 国产一级一区二区| 日本免费一区二区视频| 国产成人精品亚洲线观看| 欧美激情久久久久久久久久久| 国产美女一区| 美女尤物久久精品| 蜜臀va亚洲va欧美va天堂| 婷婷亚洲成人| 国产欧美视频在线| 精品国产不卡| 成人久久一区| 国产精品美女久久久| 亚洲另类视频| 亚洲不卡视频| 国产精品xxx在线观看| 国产一区二区三区视频在线| 色老板在线视频一区二区| 亚洲激精日韩激精欧美精品| 在线观看视频免费一区二区三区| 日韩av网站在线观看| 欧美国产另类| 丝袜美腿诱惑一区二区三区 | 三上亚洲一区二区| 在线视频观看日韩| 亚洲日本国产| 国产精品极品在线观看| 97精品97| 午夜免费一区| 日韩高清在线不卡| 久久久久久自在自线| 一区三区视频| 国产调教一区二区三区| 精品欧美一区二区三区在线观看| 久久高清国产| 成人在线视频中文字幕| 国产精品美女久久久| 国产一区二区视频在线看| 日韩av黄色在线| 日韩精品一二三| 好吊一区二区三区| 久久香蕉国产| 1000部精品久久久久久久久| 国产精品精品| 伊人精品在线| 99re国产精品| 国产精品成人一区二区网站软件| 久久久久久一区二区| 免费日韩视频| 国产精品久久久久久久久久10秀| 亚洲一区国产一区| 麻豆免费精品视频| av不卡在线| 精品三级在线观看视频| 亚洲一区区二区| 精品国产中文字幕第一页| 亚洲主播在线| 91亚洲成人| 亚洲无线观看| 色一区二区三区四区| 日韩一区二区三区精品| 桃色一区二区| 国产精品免费不| 日韩视频在线一区二区三区| 美女高潮久久久| 中文字幕成人|