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

您的位置:首頁技術文章
文章詳情頁

spring中使用mybatis實現批量插入的示例代碼

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

有3種實現方式:foreach,spring事務,以及ExecutorType.BATCH.

1. foreach方式

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

定義接口

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

定義mapper

適用于Oracle數據庫

<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語句拼接到一起,每個?都會進行變量綁定,所以當studentList中數據量較多時,生成的SQL會很長,導致數據庫執行報錯。

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函數

public static void main(String[] args) { String[] configFiles = new String[]{'spring-beans-config.xml', 'mybatis/mybatis-spring-05.xml'}; // 分別配置datasource和mybatis相關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 + '筆數據耗時: ' + (endTime - startTime) +' ms'); }}

測試結果

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

2. 借助spring事務

借助spring事務,插入一組數據

開啟spring事務

<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事務控制 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

測試結果

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

3. 使用ExecutorType.BATCH

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

有三種設置方式:

3.1 在mybatis的config文件中設置

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配置文件中,設置BATCH方式:

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

這樣,默認打開的sqlSession就都是BATCH方式的。再與spring的事務結合(參看上一節中的spring事務設置),就可以實現批量插入。

測試結果:

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

3.2 自己創建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();

測試結果:

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

3.3 使用sqlSessionTemplate在XML文件中創建bean

創建一個SqlSessionTemplate,然后注入到MapperFactoryBean中,生成對應的mapper:

<!-- 以ExecutorType.BATCH方式插入數據庫 --><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的事務結合后(參看上一節中的spring事務設置),就可以實現批量插入

測試結果

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

到此這篇關于spring中使用mybatis實現批量插入的示例代碼的文章就介紹到這了,更多相關spring mybatis批量插入內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲四虎影院| 国产精品v日韩精品v欧美精品网站 | 鲁大师影院一区二区三区| 久久中文在线| 国产一区二区三区日韩精品| 久久香蕉网站| 国户精品久久久久久久久久久不卡| 亚洲午夜av| 日韩动漫一区| 国产成人精品亚洲日本在线观看| 九九在线精品| 欧美一区=区三区| 亚洲狼人精品一区二区三区| 欧美1区免费| 91成人精品在线| 久久久水蜜桃av免费网站| 亚洲男人在线| sm捆绑调教国产免费网站在线观看 | 国产96在线亚洲| 国产精品99在线观看| 99在线|亚洲一区二区| 7m精品国产导航在线| 亚洲一级少妇| 日韩一区二区三免费高清在线观看 | 麻豆精品久久久| 久久久人人人| 国产精品久久免费视频| 免费视频一区三区| 欧美激情视频一区二区三区免费| 亚洲免费精品| 91嫩草亚洲精品| 欧美日韩伊人| 久久亚洲精品伦理| 久久久久国产精品一区二区| 日韩在线麻豆| 亚洲激情久久| 中文在线资源| 91成人网在线观看| 久久精品一区二区三区中文字幕| 视频一区中文字幕国产| 999久久久精品国产| 久久97视频| 国产精品一区毛片| 免费在线观看一区二区三区| 日韩欧美国产精品综合嫩v| 日本色综合中文字幕| 免费看欧美美女黄的网站| 久久亚洲国产| 久久久777| 米奇777超碰欧美日韩亚洲| 99久久99视频只有精品| 99国产精品视频免费观看一公开| 美女网站一区| 国产精品sm| 鲁大师影院一区二区三区| 在线看片不卡| 国产国产精品| 美女精品在线| 在线免费观看亚洲| 一区在线视频观看| 亚洲综合欧美| 日韩精品乱码av一区二区| 欧美伊人久久| 久久久久久久久成人| 9999国产精品| 日韩午夜精品| 日韩精品高清不卡| 国产日产高清欧美一区二区三区| 麻豆视频一区| 亚洲一级特黄| 亚洲精品一区二区在线播放∴| 欧美精品影院| 久久青草久久| 四虎在线精品| caoporn视频在线| 天使萌一区二区三区免费观看| 欧美在线不卡| 日韩欧美一区二区三区免费观看| 亚洲一区中文| 国产精品久久久久久久久久妞妞| 中文字幕在线官网| 亚洲黄色在线| 日韩美女国产精品| 国产成人精品一区二区免费看京| 欧美一级精品| 国产精品九九| 亚洲女同一区| 日本视频一区二区| 国产高清一区二区| 精品国产一区二区三区性色av| 国产精品美女久久久| 综合亚洲视频| 99久久精品费精品国产| 国产欧美亚洲精品a| 久久影视一区| 精品国产亚洲日本| 日韩一区精品| 日韩综合精品| 久久超级碰碰| 日韩1区2区日韩1区2区| 亚洲欧美日韩精品一区二区 | 国产精品成人一区二区不卡| 亚洲一区二区av| 四虎4545www国产精品 | 国产精东传媒成人av电影| 日韩制服丝袜先锋影音| 久久久久一区| 特黄毛片在线观看| 美女视频黄免费的久久| 欧美天堂在线| 日韩黄色在线观看| 亚洲一区二区免费看| 欧美日韩视频| 成人av二区| 亚洲高清久久| 国产91久久精品一区二区| av中文字幕在线观看第一页| 久久午夜影院| 国产一区二区三区久久久久久久久| 亚洲伊人精品酒店| 亚洲精品亚洲人成在线观看| 日韩午夜在线| 黄色亚洲在线| 91久久午夜| 在线精品福利| 日韩一区二区三区四区五区| 免费国产亚洲视频| 一区二区三区国产盗摄| 蜜臀久久99精品久久久久久9| 久热综合在线亚洲精品| 亚洲免费专区| 久久久国产精品网站| 婷婷激情一区| 热久久久久久久| 免费观看日韩电影| 国产精品xvideos88| 亚洲播播91| 视频一区二区三区在线| 国产欧美亚洲一区| 欧美日韩午夜| 亚洲精品护士| 久久国产三级精品| 色综合五月天| 亚洲精品在线观看91| 日韩精品亚洲专区在线观看| 久久伊人国产| 免费视频一区三区| 日韩av中文在线观看| 国内自拍视频一区二区三区| 国产一区二区久久久久| 不卡一区2区| 麻豆精品在线观看| 欧美日韩国产高清电影| 亚洲精品欧美| 欧洲av不卡| 麻豆精品新av中文字幕| 欧美一区激情| 图片区亚洲欧美小说区| 国产日韩一区二区三区在线| 激情综合激情| 狠狠久久伊人| 日本不卡一二三区黄网| 亚洲二区三区不卡| 福利一区和二区| 综合日韩在线| 精品在线播放| 日韩精品91| 麻豆国产欧美一区二区三区| 一二三区精品| 午夜国产一区二区| 亚洲成a人片| 国产 日韩 欧美一区| 欧美国产另类| 日本不卡一二三区黄网| 噜噜噜躁狠狠躁狠狠精品视频 | 日本aⅴ免费视频一区二区三区| 视频在线在亚洲| 久久福利影视| 婷婷激情综合| 免费精品国产| 不卡视频在线| 午夜在线一区| 巨乳诱惑日韩免费av| 亚洲天堂日韩在线| 少妇精品在线| 日韩高清在线观看一区二区| 亚洲三级网站| 日本成人手机在线| 91精品国产自产在线丝袜啪| 中文字幕成人| 91精品国产自产精品男人的天堂| 欧美日韩中文| 久久久亚洲欧洲日产| 国产一区福利| 中国字幕a在线看韩国电影| 91精品国产福利在线观看麻豆| 伊人久久大香线蕉av不卡| 欧美日韩国产精品一区二区亚洲| 国产一区导航| 国产乱码精品一区二区三区亚洲人|