使用sql标签 实现以下方法 批量插入 int insertUsers(List<User> users) int insertUsers(@Param("users")List<User> users) 根据id来修改,当user中有值的时候就修改,当user中没有值的时候不修改 int updateSelectiveUser(@Param("user")User user) in查询 List<User> selectUserListByIds(List<Integer> ids); 根据条件查询,user成员变量有空值 List<User> selectUserListByUser(@Param("user") User user); 插入并获取主键Id(需要用到还没有讲到的selectkey) int insertUser(@Param("user") User user);
时间: 2023-06-07 19:10:46 浏览: 89
好的,以下是使用SQL标签实现以上方法的代码:
```
@Mapper
public interface UserDao {
@Insert({
"<script>",
"insert into user(name, age, gender) values ",
"<foreach item='user' index='index' collection='users' separator=','>",
"(#{user.name}, #{user.age}, #{user.gender})",
"</foreach>",
"</script>"
})
int insertUsers(@Param("users") List<User> users);
@Update({
"<script>",
"update user set",
"<if test='user.name != null'> name = #{user.name},</if>",
"<if test='user.age != null'> age = #{user.age},</if>",
"<if test='user.gender != null'> gender = #{user.gender},</if>",
"</script>"
})
int updateSelectiveUser(@Param("user") User user);
@Select({
"<script>",
"select * from user where id in ",
"<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'>",
"#{id}",
"</foreach>",
"</script>"
})
List<User> selectUserListByIds(@Param("ids") List<Integer> ids);
@Select({
"select * from user where 1=1 ",
"<if test='user.name != null'> and name=#{user.name}</if>",
"<if test='user.age != null'> and age=#{user.age}</if>",
"<if test='user.gender != null'> and gender=#{user.gender}</if>",
})
List<User> selectUserListByUser(@Param("user") User user);
@Insert({
"insert into user(name, age, gender) values (#{user.name}, #{user.age}, #{user.gender})",
})
@SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "user.id", before = false, resultType = Integer.class)
int insertUser(@Param("user") User user);
}
```
希望能够帮到你!
阅读全文