验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

MyBatis怎么根据条件批量修改字段

阅读:1125 来源:乙速云 作者:代码code

MyBatis怎么根据条件批量修改字段

      MyBatis根据条件批量修改字段

      背景:

      给学生改作业,只要是对的都批量进行数据库的修改

      代码以及注释

      • conttoller

      @RestController
      @RequestMapping("/work")
      public class WorkController {
          @Autowired
          private WorkService workService;
          
          @PutMapping("/examine")
          public HttpResponse examine(Integer[] id) {
              Integer total = workService.examine(id);
              return HttpResponse.ok("共批改[ "+total+" ]条道题","");
          }
      }
      • service

      @Service
      public class WorkService {
          @Autowired
          private WorkMapper workMapper;
          
          public Integer examine(Integer[] id) {
              return workMapper.examine(id);
          }
      }
      • mapper

      @Mapper
      public interface WorkMapper {
          Integer examine(@Param("id")Integer[] id);
      }
      • xml

      
      
          UPDATE WORK SET isEnable=TRUE WHERE id IN
          
              #{id}
          
      
      • 工具类HttpResponse(此需求中无关紧要的类)

      public class HttpResponse {
          private Integer status;
          private String message;
          private Object object;
          
          public static HttpResponse ok(String message) {
              return new HttpResponse(200, message, null);
          }
          
          public static HttpResponse ok(Object object) {
              return new HttpResponse(200, null, object);
          }
          
          public static HttpResponse ok(String message,Object object) {
              return new HttpResponse(200, message, object);
          }
          
          public static HttpResponse error(Integer status, String message) {
              return new HttpResponse(500, message, null);
          }
          
          public static HttpResponse error(String message) {
              return new HttpResponse(500, message, null);
          }
          
          public static HttpResponse error(String message,Object object) {
              return new HttpResponse(500, message, object);
          }
          
          
          protected HttpResponse() {
              super();
          }
          
          private HttpResponse(Integer status, String message, Object object) {
              super();
              this.status = status;
              this.message = message;
              this.object = object;
          }
          public Integer getStatus() {
              return status;
          }
          public void setStatus(Integer status) {
              this.status = status;
          }
          public String getMessage() {
              return message;
          }
          public void setMessage(String message) {
              this.message = message;
          }
          public Object getObject() {
              return object;
          }
          public void setObject(Object object) {
              this.object = object;
          }
      }

      重点:

      如果mapper没加@Param("id")注解会报错找不到参数"id"

      至此MyBatis根据id批量修改数据库的某字段需求完成!

      MyBatis多条件批量修改

      简单记录下

      想要修改一张表,是联合主键,也就是where后两个以上条件才能唯一确定一条数据。

      如果有唯一键,那么foreach中 用in就可以解决。

      现在没办法用in,不然 A in (#{})and B in (#{})感觉成笛卡尔了。

      简单点就是要实现执行多条语句。对update做循环。传入参数为 List,ATest类中字段A,B为联合主键,修改C的值

      
              
                  update table_ABC
                  
                      C = #{val.C},
                  
                  where A=#{val.A} and B= #{val.B}
              
      
          

      执行出来效果为:

      begin 
      update table_ABC set C = '2' where A = '1' and B= '1';
      update table_ABC set C = '2' where A = '1' and B= '2';
      update table_ABC set C = '4' where A = '3' and B= '2';
      end;

      这里使用的数据库为oracle,oracle执行认为begin和end之前为一条语句

      当然,也可以用or 的形式拼接,不过还没测试。

      网上查有的说是mysql数据库 mybatis一次执行一条语句。支持多条,可以MySQL连接数据库时,添加语句:“allowMultiQueries=true”

      作用:

      1.可以在sql语句后携带分号,实现多语句执行。

      2.可以执行批处理,同时发出多个SQL语句。

    分享到:
    *特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
    相关文章
    {{ v.title }}
    {{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
    你可能感兴趣
    推荐阅读 更多>