mybatis學(xué)習(xí)之路----mysql批量新增數(shù)據(jù)
來(lái)源:
mysql新增語(yǔ)句
insert into 表名(字段,字段。。。) values ( 值,值 。。。);此種適合單條插入。
批量插入,
一種可以在代碼中循環(huán)著執(zhí)行上面的語(yǔ)句,但是這種效率太差,下面會(huì)有對(duì)比,看看它有多差。
另一種,可以用mysql支持的批量插入語(yǔ)句,
insert into
表名(字段,字段。。。) values ( 值,值 。。。),( 值,值 。。。),( 值,值 。。。)....
這種方式相比起來(lái),更高效。
實(shí)現(xiàn)過(guò)程mapper.xml
<!-- 跟普通的insert沒(méi)有什么不同的地方 ,主要用來(lái)跟下面的批量插入做對(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 + '\'' +
'}';
}
}
/**
* 新增數(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;
}
https://blog.csdn.net/xu1916659422/article/details/77971867
mysql新增語(yǔ)句
insert into 表名(字段,字段。。。) values ( 值,值 。。。);此種適合單條插入。
批量插入,
一種可以在代碼中循環(huán)著執(zhí)行上面的語(yǔ)句,但是這種效率太差,下面會(huì)有對(duì)比,看看它有多差。
另一種,可以用mysql支持的批量插入語(yǔ)句,
insert into
表名(字段,字段。。。) values ( 值,值 。。。),( 值,值 。。。),( 值,值 。。。)....
這種方式相比起來(lái),更高效。
實(shí)現(xiàn)過(guò)程mapper.xml
<!-- 跟普通的insert沒(méi)有什么不同的地方 ,主要用來(lái)跟下面的批量插入做對(duì)比。-->
<insert id="insert" parameterType="com.soft.mybatis.model.Customer">
<!-- 跟自增主鍵方式相比,這里的不同之處只有兩點(diǎn)
1 insert語(yǔ)句需要寫(xiě)id字段了,并且 values里面也不能省略
2 selectKey 的order屬性需要寫(xiě)成BEFORE 因?yàn)檫@樣才能將生成的uuid主鍵放入到model中,
這樣后面的insert的values里面的id才不會(huì)獲取為空
跟自增主鍵相比就這點(diǎn)區(qū)別,當(dāng)然了這里的獲取主鍵id的方式為 select uuid()
當(dāng)然也可以另寫(xiě)別生成函數(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ì)寫(xiě)的這么簡(jiǎn)單。 -->
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ì)象 名稱(chēng)隨意-->
<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 + '\'' +
'}';
}
}
接口
實(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;
}
posted on 2019-05-23 11:29 半導(dǎo)體 閱讀(136) 評(píng)論(0) 編輯 收藏