电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

瀏覽:6日期: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
相關文章:
主站蜘蛛池模板: 深圳宣传片制作-企业宣传视频制作-产品视频拍摄-产品动画制作-短视频拍摄制作公司 | 工控机-图像采集卡-PoE网卡-人工智能-工业主板-深圳朗锐智科 | 运动木地板厂家_体育木地板安装_篮球木地板选购_实木运动地板价格 | 聚氨酯催化剂K15,延迟催化剂SA-1,叔胺延迟催化剂,DBU,二甲基哌嗪,催化剂TMR-2,-聚氨酯催化剂生产厂家 | 智慧水务|智慧供排水利信息化|水厂软硬件系统-上海敢创 | 宜兴紫砂壶知识分享 - 宜兴壶人| 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 合肥礼品公司-合肥礼品定制-商务礼品定制公司-安徽柏榽商贸有限公司 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 防火窗_耐火窗_防火门厂家_防火卷帘门-重庆三乐门业有限公司 | 电镀整流器_微弧氧化电源_高频电解电源_微弧氧化设备厂家_深圳开瑞节能 | 粘度计维修,在线粘度计,二手博勒飞粘度计维修|收购-天津市祥睿科技有限公司 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 拉卡拉POS机官网 - 官方直营POS机办理|在线免费领取 | 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | 大巴租车平台承接包车,通勤班车,巴士租赁业务 - 鸿鸣巴士 | 打包钢带,铁皮打包带,烤蓝打包带-高密市金和金属制品厂 | 黑龙江京科脑康医院-哈尔滨精神病医院哪家好_哈尔滨精神科医院排名_黑龙江精神心理病专科医院 | 吲哚菁绿衍生物-酶底物法大肠菌群检测试剂-北京和信同通科技发展有限公司 | 法钢特种钢材(上海)有限公司 - 耐磨钢板、高强度钢板销售加工 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 领袖户外_深度旅游、摄影旅游、小团慢旅行、驴友网 | 欧景装饰设计工程有限公司-无锡欧景装饰官网 | LHH药品稳定性试验箱-BPS系列恒温恒湿箱-意大利超低温冰箱-上海一恒科学仪器有限公司 | 春腾云财 - 为企业提供专业财税咨询、代理记账服务 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 德国EA可编程直流电源_电子负载,中国台湾固纬直流电源_交流电源-苏州展文电子科技有限公司 | 隔离变压器-伺服变压器--输入输出电抗器-深圳市德而沃电气有限公司 | 分类168信息网 - 分类信息网 免费发布与查询 | 泰国试管婴儿_泰国第三代试管婴儿费用|成功率|医院—新生代海外医疗 | 长沙网站建设制作「网站优化推广」-网页设计公司-速马科技官网 | pbt头梳丝_牙刷丝_尼龙毛刷丝_PP塑料纤维合成毛丝定制厂_广州明旺 | 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 地源热泵一体机,地源热泵厂家-淄博汇能环保设备有限公司 | 河南新乡德诚生产厂家主营震动筛,振动筛设备,筛机,塑料震动筛选机 | 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | LED灯杆屏_LED广告机_户外LED广告机_智慧灯杆_智慧路灯-太龙智显科技(深圳)有限公司 | 温州食堂承包 - 温州市尚膳餐饮管理有限公司 | 中国品牌排名投票_十大品牌榜单_中国著名品牌【中国品牌榜】 | 钢绞线万能材料试验机-全自动恒应力两用机-混凝土恒应力压力试验机-北京科达京威科技发展有限公司 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 杭州代理记账多少钱-注册公司代办-公司注销流程及费用-杭州福道财务管理咨询有限公司 |