JBOSS 點滴

          豐豐的博客

          mybatis學(xué)習(xí)之路----mysql批量新增數(shù)據(jù)

          來源:
          https://blog.csdn.net/xu1916659422/article/details/77971867
           
             mysql新增語句 
              insert into 表名(字段,字段。。。) values ( 值,值 。。。);此種適合單條插入。
          批量插入,
             一種可以在代碼中循環(huán)著執(zhí)行上面的語句,但是這種效率太差,下面會有對比,看看它有多差。
          另一種,可以用mysql支持的批量插入語句,
          insert into
               表名(字段,字段。。。) values ( 值,值 。。。),( 值,值 。。。),( 值,值 。。。)....
          這種方式相比起來,更高效。
          實現(xiàn)過程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才不會獲取為空
                        跟自增主鍵相比就這點區(qū)別,當然了這里的獲取主鍵id的方式為 select uuid()
                        當然也可以另寫別生成函數(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">
                  <!-- 這里只做演示用,真正項目中不會寫的這么簡單。 -->
                  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對應(yīng)的屬性
                           separator=","  表示每次循環(huán)完畢,在sql后面放一個逗號
                           item="cus" 每次循環(huán)的實體對象 名稱隨意-->
                  <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);

            實現(xiàn)
          實現(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) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 西充县| 梅河口市| 海阳市| 伽师县| 榕江县| 文登市| 迁西县| 潍坊市| 鹤壁市| 吉木萨尔县| 泾阳县| 临邑县| 新乡县| 蓬溪县| 遂宁市| 鄄城县| 佳木斯市| 牡丹江市| 卢湾区| 东乡族自治县| 巴彦县| 手游| 瓮安县| 铁岭市| 武隆县| 苏尼特右旗| 阜平县| 中山市| 青河县| 太康县| 千阳县| 凤城市| 永城市| 丹巴县| 任丘市| 鄂托克前旗| 宁都县| 舞钢市| 成武县| 奉新县| 南安市|