mybatis學(xué)習(xí)之路mysql批量新增數(shù)據(jù)的方法
接下來兩節(jié)要探討的是批量插入和批量更新,因?yàn)檫@兩種操作在企業(yè)中也經(jīng)常用到。
mysql新增語句
insert into 表名(字段,字段。。。) values ( 值,值 。。。);此種適合單條插入。
批量插入,一種可以在代碼中循環(huán)著執(zhí)行上面的語句,但是這種效率太差,下面會(huì)有對(duì)比,看看它有多差。
另一種,可以用mysql支持的批量插入語句,
insert into 表名(字段,字段。。。) values ( 值,值 。。。),( 值,值 。。。),( 值,值 。。。)....
這種方式相比起來,更高效。
下面開始來實(shí)現(xiàn)。
<!-- 跟普通的insert沒有什么不同的地方 ,主要用來跟下面的批量插入做對(duì)比。--> <insert id="insert" parameterType="com.soft.mybatis.model.Customer"> <!-- 跟自增主鍵方式相比,這里的不同之處只有兩點(diǎn) 1 insert語句需要寫id字段了,并且 values里面也不能省略 2 selectKey 的order屬性需要寫成BEFORE 因?yàn)檫@樣才能將生成的uuid主鍵放入到model中, 這樣后面的insert的values里面的id才不會(huì)獲取為空 跟自增主鍵相比就這點(diǎn)區(qū)別,當(dāng)然了這里的獲取主鍵id的方式為 select uuid() 當(dāng)然也可以另寫別生成函數(shù)。--> <selectKey keyProperty="id" order="BEFORE" resultType="String"> select uuid() </selectKey> insert into t_customer (id,c_name,c_sex,c_ceroNo,c_ceroType,c_age) values (#{id},#{name},#{sex},#{ceroNo},#{ceroType},#{age}) </insert> <!-- 批量插入, --> <insert id="batchInsert" parameterType="java.util.Map"> <!-- 這里只做演示用,真正項(xiàng)目中不會(huì)寫的這么簡單。 --> insert into t_customer (id,c_name,c_sex,c_ceroNo,c_ceroType,c_age) values <!-- foreach mybatis循環(huán)集合用的 collection="list" 接收的map集合中的key 用以循環(huán)key對(duì)應(yīng)的屬性 separator="," 表示每次循環(huán)完畢,在sql后面放一個(gè)逗號(hào) item="cus" 每次循環(huán)的實(shí)體對(duì)象 名稱隨意--> <foreach collection="list" separator="," item="cus"> <!-- 組裝values對(duì)象,因?yàn)檫@張表的主鍵為非自增主鍵,所以這里 (select uuid()) 用于生成id的值--> ((select uuid()),#{cus.name},#{cus.sex},#{cus.ceroNo},#{cus.ceroType},#{cus.age}) </foreach> </insert>
實(shí)體model對(duì)象
package com.soft.mybatis.model; /** * Created by xuweiwei on 2017/9/10. */ public class Customer { private String id; private String name; private Integer age; private Integer sex; private String ceroNo; private Integer ceroType; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public String getCeroNo() { return ceroNo; } public void setCeroNo(String ceroNo) { this.ceroNo = ceroNo; } public Integer getCeroType() { return ceroType; } public void setCeroType(Integer ceroType) { this.ceroType = ceroType; } @Override public String toString() { return "Customer{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + ", sex=" + sex + ", ceroNo='" + ceroNo + '\'' + ", ceroType='" + ceroType + '\'' + '}'; } }
接口
int add(Customer customer); int batchInsert(Map<String,Object> param);
實(shí)現(xiàn)
/** * 新增數(shù)據(jù) * @param customer * @return */ public int add(Customer customer) { return insert("customer.insert", customer); } /** * 批量插入數(shù)據(jù) * @param param * @return */ public int batchInsert(Map<String,Object> param) { return insert("customer.batchInsert", param); } /** * 公共部分 * @param statementId * @param obj * @return */ private int insert(String statementId, Object obj){ SqlSession sqlSession = null; try { sqlSession = SqlsessionUtil.getSqlSession(); int key = sqlSession.insert(statementId, obj); // commit sqlSession.commit(); return key; } catch (Exception e) { sqlSession.rollback(); e.printStackTrace(); } finally { SqlsessionUtil.closeSession(sqlSession); } return 0; }
測(cè)試類
@Test public void add() throws Exception { Long start = System.currentTimeMillis(); for(int i=0;i<1000;i++){ Customer customer = new Customer(); customer.setName("普通一條條插入 "+ i); customer.setAge(15); customer.setCeroNo("000000000000"+ i); customer.setCeroType(2); customer.setSex(1); int result = customerDao.add(customer); } System.out.println("耗時(shí) : "+(System.currentTimeMillis() - start)); } @Test public void batchInsert() throws Exception { Map<String,Object> param = new HashMap<String,Object>(); List<Customer> list = new ArrayList<Customer>(); for(int i=0;i<1000;i++){ Customer customer = new Customer(); customer.setName("批量插入" + i); customer.setAge(15); customer.setCeroNo("111111111111"+i); customer.setCeroType(2); customer.setSex(1); list.add(customer); } param.put("list",list); Long start = System.currentTimeMillis(); int result = customerDao.batchInsert(param); System.out.println("耗時(shí) : "+(System.currentTimeMillis() - start)); }
兩種都進(jìn)行插入1000條測(cè)試
由于我沒有用連接池等等原因,在插入了700多條的時(shí)候 junit直接掛了,
Cause: org.apache.ibatis.executor.ExecutorException: Error selecting key or setting result to parameter object.
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Data source rejected establishment of connection, message from server: "Too many connections"
數(shù)據(jù)庫插入結(jié)果:
但是第二種僅僅用了2秒多就ok了??梢娺@種效率很高。
數(shù)據(jù)庫結(jié)果
這里寫了兩個(gè),其實(shí)第一種僅僅是做對(duì)比效率用。
批量新增數(shù)據(jù)記錄完畢。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java8中的lambda表達(dá)式,看這篇絕對(duì)夠
這篇文章主要介紹了java8中的lambda表達(dá)式,看這篇絕對(duì)夠!具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot使用RestTemplate發(fā)送http請(qǐng)求的實(shí)操演示
RestTemplate是Spring 框架提供的 ,可用于在應(yīng)用中調(diào)用 rest 服務(wù),它簡化了與 http 服務(wù)的通信方式,統(tǒng)一了 RESTful 的標(biāo)準(zhǔn),封裝了 http 鏈接,本文給大家介紹了SpringBoot使用RestTemplate發(fā)送http請(qǐng)求的實(shí)操演示,需要的朋友可以參考下2024-11-11Java變量的初始化及靜態(tài)方法的實(shí)現(xiàn)
這篇文章主要介紹了Java變量的初始化及靜態(tài)方法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10rabbitmq的消息持久化處理開啟,再關(guān)閉后,消費(fèi)者啟動(dòng)報(bào)錯(cuò)問題
這篇文章主要介紹了rabbitmq的消息持久化處理開啟,再關(guān)閉后,消費(fèi)者啟動(dòng)報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11深入了解Java中String、Char和Int之間的相互轉(zhuǎn)換
這篇文章主要介紹了深入了解Java中String、Char和Int之間的相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06Java對(duì)象進(jìn)行深拷貝的五種方法實(shí)例代碼
這篇文章主要介紹了Java對(duì)象進(jìn)行深拷貝的五種方法,分別是構(gòu)造函數(shù)、重載clone()方法、Apache?Commons?Lang序列化、Gson序列化和Jackson序列化,每種方法都給出了實(shí)例代碼,需要的朋友可以參考下2025-04-04Java之SpringBoot定時(shí)任務(wù)案例講解
這篇文章主要介紹了Java之SpringBoot定時(shí)任務(wù)案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08Java多線程中ReentrantLock與Condition詳解
這篇文章主要介紹了Java多線程中ReentrantLock與Condition詳解,需要的朋友可以參考下2017-11-11