MaBatis:动态SQL使用foreach进行批量修改

简介: MaBatis:动态SQL使用foreach进行批量修改

foreach

介绍

 - collection:必填,值为要迭代循环的属性名。这个属性值的情况有很多

 - item :变量名,值为从迭代对象中取出来的每一个值

 - index: 索引的属性名,在集合数组情况下值为当前索引值,当迭代循环的对象时Map类型时,这个值为map的key值

 - open: 整个循环内容开头的字符串

 - close:整个循环内容结尾的字符串

 - separator: 每次循环的分隔符

 

参数

 - 只有一个数组参数或集合参数

 

   - 当参数类型为集合的时候,默认会转换为map类型,并添加一个key为collection的值

   - 如果参数类型是List集合,那么就继续添加一个key为list的值

   - 当参数类型为数组的时候,也会转成map类型,默认的key为array

 

 - 有多个参数

   - 当有多个参数的时候,要使用@Param注解给每个参数指定一个名字,否则在SQL中使用参数时就会不方便。因此将collection设置为@Param注解指定的名字

 

 - 参数时Map类型

   - 使用Map和使用@Param注解方式类似,将collection指定为对应Map中的key即可

   - 如果要循环所传入的Map,推荐使用@Param注解指定名字,此时可将collection设置为指定的名字

   - 如果不想指定名字,就使用默认值_parameter


XML

<update id="updateRepertoryBatchRollback" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
            update STOREROOM_REPERTORY
            <set>
                UPDATE_TIME =#{item.updateTime},
                STATUS =#{item.status}
            </set>
            where UUID= #{item.uuid}
        </foreach>
    </update>

DAO 数据层

 

void updateRepertoryBatchRollback(@Param("list") List<StoreroomRepertory> list);


service业务层

 

@Transactional(readOnly = false)
    @Override
    public void updateRepertoryBatchRollback(List<StoreroomRepertory> list) {
        try {
            int batchCount = 50;//每批commit的个数
            int batchLastIndex = batchCount - 1;//每批最后一个的下标
            for(int index = 0; index < list.size()-0;){
                if(batchLastIndex > list.size()-1){
                    batchLastIndex = list.size();
                    storeroomRepertoryDao.updateRepertoryBatchRollback(list.subList(index, batchLastIndex));
                    System.out.println("index:"+index+"     batchLastIndex:"+batchLastIndex);
                    break;//数据插入完成,退出循环
                }else{
                    storeroomRepertoryDao.updateRepertoryBatchRollback(list.subList(index, batchLastIndex));
                    System.out.println("index:"+index+"     batchLastIndex:"+batchLastIndex);
                    index = batchLastIndex;//设置下一批下标
                    batchLastIndex = index + (batchCount - 1);
                    if(index==list.size()-1){
                        storeroomRepertoryDao.updateRepertoryBatchRollback(list.subList(index, index+1));
                    }
                }
            }
        }catch(Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            // 设置回滚点
            Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
            // 回滚到 savePoint
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            log.error("批量异常捕获",e);
            e.printStackTrace();
        }
    }
 controller控制层
    @RequestMapping(value = "updateRepertoryBatchRollback",method = RequestMethod.POST)
    @ApiOperation(value = "批量修改")
    public PmpResult updateRepertoryBatchRollback(@RequestBody List<StoreroomRepertory> list){
        try {
            storeroomRepertoryService.updateRepertoryBatchRollback(list);
            logger.info("批量修改成功");
            return PmpResult.success(list);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            String errorMessage = "异常的批量修改";
            if (isDev) {
                errorMessage = e.getMessage();
            }
            return PmpResult.paramError(errorMessage);
        }
    }
目录
相关文章
|
1月前
|
SQL Java 关系型数据库
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
18 0
|
1月前
|
SQL Java 关系型数据库
MyBatis中的9种常用动态sql标签精妙用法
MyBatis中的9种常用动态sql标签精妙用法
64 0
|
1月前
|
SQL druid Java
【MyBatis】2、MyBatis 的动态 SQL 和增删改操作
【MyBatis】2、MyBatis 的动态 SQL 和增删改操作
29 0
|
2月前
|
SQL XML Java
【JavaEE进阶】 MyBatis之动态SQL
【JavaEE进阶】 MyBatis之动态SQL
|
3月前
|
SQL 缓存 Java
JAVAEE框架技术之8-myBatis ORM框架技术参数和动态SQL语句
JAVAEE框架技术之8-myBatis ORM框架技术参数和动态SQL语句
64 0
JAVAEE框架技术之8-myBatis ORM框架技术参数和动态SQL语句
|
3月前
|
SQL XML Java
【MyBatis】动态SQL
【MyBatis】动态SQL
|
3月前
|
SQL XML Java
MyBatis 实现动态 SQL
 MyBatis 中的动态 SQL 就是SQL语句可以根据不同的情况情况来拼接不同的sql。 本文会介绍 xml 和 注解 两种方式的动态SQL实现方式。
71 1
|
14天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
52 10
|
1月前
|
SQL 数据库 数据安全/隐私保护
Sql Server数据库Sa密码如何修改
Sql Server数据库Sa密码如何修改
|
24天前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
17 0