学无先后,达者为师

网站首页 编程语言 正文

MP、MybatisPlus、联表查询、自定义sql、Constants.WRAPPER、ew (二)

作者:m0_59259076 更新时间: 2023-10-11 编程语言

描述: 给定一个id列表,更新对应列表中动物的年龄,使得年龄都较少一岁。

要求:使用条件构造器构造条件。

 mapper:

   void updateAnimalAge(@Param(Constants.WRAPPER) Wrapper<Animal> wrapper, @Param("age") int reducedAge);

xml:

    <update id="updateAnimalAge">
        update `animal` set `age` = `age` - #{age}
        <where>
           ${ew.sqlSegment}
        </where>
    </update>

service:

    @org.junit.Test
    public void test() {
        ArrayList<String> strings = Lists.newArrayList();
        strings.add("1775e2db7fb5e0d9b8e98d4137f58b91");
        strings.add("23d210703b33bd67b901e147f874a831");

        int reducedAge = 1;

        LambdaQueryWrapper<Animal> wrapper = new QueryWrapper<Animal>().lambda();

        wrapper.in(Animal::getId,strings);


        this.animalMapper.updateAnimalAge(wrapper,reducedAge);
//
//        if (successful.size() > 0) {
//            System.out.println("success!!");
//        } else {
//            System.out.println("failed!!");
//        }
        
    }

sql:

JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@3c50ad4b] will not be managed by Spring
==>  Preparing: update `animal` set `age` = `age` - ? WHERE (id IN (?,?)) 
==> Parameters: 1(Integer), 1775e2db7fb5e0d9b8e98d4137f58b91(String), 23d210703b33bd67b901e147f874a831(String)
<==    Updates: 2

原文链接:https://blog.csdn.net/m0_59259076/article/details/133430887

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新