java批量修改

public class User {


private int uid;


private String uname;
private String age;
private String sex;


private String phone;
private Date creatTime;
public User() {
super();
}

public class UserInput {


private List<User> userList;


public UserInput() {
super();
// TODO Auto-generated constructor stub
}

sql语句

<update id="updateUsers" parameterType="com.koopoo.liuguosheng.model.vo.input.UserInput">
        update t_user set
        uname =
        <foreach collection="userList" item="item" index="index"
                 separator=" " open="case uid" close="end">
            <if test="item.uname != null and item.uname != '' ">
                when #{item.uid} then #{item.uname}
            </if>
            <if test="item.uname == null ">
                when #{item.uid} then t_user.uname
            </if>
        </foreach>
        ,age =
        <foreach collection="userList" item="item" index="index"
                 separator=" " open="case uid" close="end">
            <if test="item.age != null and item.age !='' ">
                when #{item.uid} then #{item.age}
            </if>
            <if test="item.age == null ">
                when #{item.uid} then t_user.age
            </if>
        </foreach>
        ,sex =
        <foreach collection="userList" item="item" index="index"
                 separator=" " open="case uid" close="end">
            <if test="item.sex!=null and item.sex!='' ">
                when #{item.uid} then #{item.sex}
            </if>
            <if test="item.sex == null  ">
                when #{item.uid} then t_user.sex
            </if>
        </foreach>
        ,phone =
        <foreach collection="userList" item="item" index="index"
                 separator=" " open="case uid" close="end">
           <if test="item.phone!=null and item.phone!='' ">
                when #{item.uid} then #{item.phone}
            </if>
            <if test="item.phone == null ">
                when #{item.uid} then t_user.phone
            </if>
        </foreach>
        where uid in
        <foreach collection="userList" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.uid}
        </foreach>
    </update>

dao里面

int updateUsers(UserInput userInput);

service里面

Dto updateUsers(UserInput userInput);

serviceImpl里面

@Override
    public Dto updateUsers(UserInput userInput) {
        Dto dto =new Dto();
        userMapper.updateUsers(userInput);
        return dto;
    }

controller里面

@RequestMapping(value = "/updateUsers",method =RequestMethod.POST)
    @ResponseBody
    public Dto updateUsers(@RequestBody UserInput userInput)  {
       
        return userService.updateUsers(userInput);
    }

用的是postman测的

    结果传参示例:

   

 {
"userList":[
    {
        "uid":13,
        "uname":"测试13",
        "age":"24",
        "sex":"2",
        "phone":"12435435"
    },
    {
        "uid":14,
        "uname":"测试14",
        "age":"24",
        "sex":"2",
        "phone":"12435435"
    }
            ]

}

返回结果示例:

{
    "code": 0,
    "msg": "success"
}

猜你喜欢

转载自blog.csdn.net/qq_28433613/article/details/81216692
今日推荐