JBOSS 点滴

丰丰的博客

mybatis学习之路----mysql批量新增数据

来源:
https://blog.csdn.net/xu1916659422/article/details/77971867
 
   mysql新增语句 
    insert into 表名(字段,字段。。。) values ( 值,值 。。。);此种适合单条插入。
批量插入,
   一种可以在代码中循环着执行上面的语句,但是这种效率太差,下面会有对比,看看它有多差。
另一种,可以用mysql支持的批量插入语句,
insert into
     表名(字段,字段。。。) values ( 值,值 。。。),( 值,值 。。。),( 值,值 。。。)....
这种方式相比起来,更高效。
实现过程mapper.xml
 

    <!-- 跟普通的insert没有什么不同的地方 ,主要用来跟下面的批量插入做对比。-->
    <insert id="insert" parameterType="com.soft.mybatis.model.Customer">
        <!-- 跟自增主键方式相比,这里的不同之处只有两点
                    1  insert语句需要写id字段了,并且 values里面也不能省略
                    2 selectKey 的order属性需要写成BEFORE 因为这样才能将生成的uuid主键放入到model中,
                    这样后面的insert的values里面的id才不会获取为空
              跟自增主键相比就这点区别,当然了这里的获取主键id的方式为 select uuid()
              当然也可以另写别生成函数。-->
        <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">
        <!-- 这里只做演示用,真正项目中不会写的这么简单。 -->
        insert into
          t_customer (id,c_name,c_sex,c_ceroNo,c_ceroType,c_age)
        values
        <!-- foreach mybatis循环集合用的
              collection="list" 接收的map集合中的key 用以循环key对应的属性
                 separator=","  表示每次循环完毕,在sql后面放一个逗号
                 item="cus" 每次循环的实体对象 名称随意-->
        <foreach collection="list" separator="," item="cus">
            <!-- 组装values对象,因为这张表的主键为非自增主键,所以这里 (select uuid()) 用于生成id的值-->
            ((select uuid()),#{cus.name},#{cus.sex},#{cus.ceroNo},#{cus.ceroType},#{cus.age})
        </foreach>
    </insert>
实体model对象

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 + '\'' +

                '}';

    }

}
接口
  1. int add(Customer customer);
  2.     int batchInsert(Map<String,Object> param);

    实现
实现

   
/**

     * 新增数据

     * 
@param customer

     * 
@return

     
*/

    
public int add(Customer customer) {

        
return insert("customer.insert", customer);

    }

 

    
/**

     * 批量插入数据

     * 
@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 半导体 阅读(121) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: