cf2000 发表于 2016-11-25 07:02:59

mybatis 批量操作

MyBatis  批量操作
       记录下常用的批量操作,增删改,处理模式一致。
   
         一.批量新增
            1.采用union all  拼接sql,导致问题sql过长,耗时,性能不好,且达到一定量会超出sql长度限制,不推荐
                一般20字段以内的表新增,数据2000条以内可用,经过测试。
      dao层:         

      /***
* 批量新增方法一
*    sql拼接
* @return
*/
public boolean insertMethodOne(List<Map<String,Object>> list){
return sqlSessionTemplate.insert(super.nameSpaces+"insertMethodOne",list)==1;
}
      mapper文件:        

<!-- 批量新增   方法一 (sql内做循环,拼接sql,但sql会越来越长,一定量之后会抛出长度限制的错误,性能也不好,大致是适用2000条以内)-->
<insert id="insertMethodOne" parameterType="List">
insert into gdgk_test_user(id,name,password)
(
<foreach collection="list" item="item" index="index" separator="union all">
select #{item.id,jdbcType=VARCHAR},#{item.name,jdbcType=VARCHAR},#{item.password,jdbcType=VARCHAR}
from dual
</foreach>
)
</insert>
            很多时候我们采用的id是通过序列得到的,所以我们拼接的sql是这样子


<insert id="insertMethodOne" parameterType="List">
<selectKey resultType="java.lang.Integer" order="BEFORE" keyProperty="id">
SELECT SLA_SEQUENCE.NEXTVAL FROM DUAL
</selectKey>
insert into gdgk_test_user(id,name,password)
(
<foreach collection="list" item="item" index="index" separator="union all" >
select #{id,jdbcType=DECIMAL},#{item.name,jdbcType=VARCHAR},#{item.password,jdbcType=VARCHAR}
from dual
</foreach>
)
</insert>
             这样问题就出来了,如果数据库做了主键唯一约束,这里就违反了,每次批量新增的时候id不变,永远是第一次从序列里面拿到的,所以解决如下:

<insert id="insertMethodOne" parameterType="List">
insert into gdgk_test_user(id,name,password)
select SLA_SEQUENCE.NEXTVAL,A.* from
(
<foreach collection="list" item="item" index="index" separator="union all" >
select #{item.name,jdbcType=VARCHAR},#{item.password,jdbcType=VARCHAR}
from dual
</foreach>
) A
</insert>
              首先把id放进去,实体里面的东西 不需要带入id的任何属性
             2.采用dao层循环,得到sqlsession  统一commit,推荐
              dao层:        

      /***
* 批量新增方法二
*    dao循环,获取session 统一commit
* @return
*/
public boolean insertMethodTwo(List<Map<String,Object>> list){
SqlSession session = sqlSessionTemplate.getSqlSessionFactory()
.openSession(ExecutorType.BATCH, false);
for (Map<String,Object> rec : list)
sqlSessionTemplate.insert(super.nameSpaces+"insertMethodTwo", rec);
session.commit();
return true;
}
          
            mapper文件:
            

    <!--批量新增   方法二(dao层做循环,推荐)-->
<insert id="insertMethodTwo" parameterType="Map">
insert into gdgk_test_user(id,name,password)
values (#{id,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR})
</insert>
      
          3.Test类:
           

      @Test
@SuppressWarnings("all")
public void test() {
ApplicationContext con = new ClassPathXmlApplicationContext(
"base.xml", TestMssiSql.class);
UserDaouserDao = con.getBean(UserDao.class);
List<Map<String,Object>> listOne = new ArrayList<>();
for(int i=10;i<14;i++){
Map<String,Object> mapOne = new HashMap<>();
mapOne.put("id", i);
mapOne.put("name", "新增方法一");
mapOne.put("password", "123");
listOne.add(mapOne);
}
userDao.insertMethodOne(listOne);
logger.info("批量新增调用第一个方法完成");
List<Map<String,Object>> listTwo = new ArrayList<>();
for(int i=14;i<18;i++){
Map<String,Object> mapTwo = new HashMap<>();
mapTwo.put("id", i);
mapTwo.put("name", "新增方法二");
mapTwo.put("password", "123");
listTwo.add(mapTwo);
}
userDao.insertMethodTwo(listTwo);
logger.info("批量新增调用第二个方法完成");
}
   
               批量修改一致,可用dao层循环那种法子,批量删除略为不同,每次带入id进行删除,也可采用in的方式拼接sql
        
              dao: 

      /***
* 批量删除
*   只循环idsql in方式拼接
* @param list
* @return
*/
public boolean deleteBatch(List<String> list){
return sqlSessionTemplate.delete(super.nameSpaces+"deleteBatch",list)==1;
}
       
            mapper文件:    

<!-- 批量删除   -->
<delete id="deleteBatch" parameterType="List">
delete gdgk_test_user
where id in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</delete>
        
            Test:

             List<String>ids = new ArrayList<>();
for(int i=10;i<20;i++){
ids.add(String.valueOf(i));
}
userDao.deleteBatch(ids);
logger.info("批量删除方法完成");
   
    记录一下,方便温习……
             
页: [1]
查看完整版本: mybatis 批量操作