<mappernamespace="com.course.dao.UserDao"> <selectid="queryUser01"parameterType="User"resultType="User"> select id, name, age, address, birthday from user <where> <iftest="name != null and name !=''"> name like "%"#{name}"%" </if> <iftest="address != null and address != ''"> and address like "%"#{address}"%" </if> </where> </select> <selectid="queryUser02"parameterType="User"resultType="User"> select id, name, age, address, birthday from user <where> <choose> <whentest="name != null and name !=''"> name like "%"#{name}"%" </when> <whentest="address != null and address != ''"> and address like "%"#{address}"%" </when> <otherwise> and age < 80 </otherwise> </choose> </where> </select> <updateid="updateUserSet"> update user <set> <iftest="name != null and name !=''"> name = #{name}, </if> <iftest="address != null and address != ''"> address = #{address} </if> </set> <where> <choose> <whentest="id == null"> id = 20 </when> <otherwise> id = #{id} </otherwise> </choose> </where> </update> <selectid="queryUserByArray"parameterType="int[]"resultType="User"> select id, name, age, address, birthday from user <where> id in <!-- collection: 数组或者集合,不是数组或者集合形参名 item: 数据或者集合的元素 index: 循环遍历,索引值 open: 开始字符串 close: 结束字符串 separator: 分隔符 --> <foreachcollection="array"item="id"open="("close=")"separator=","> #{id} </foreach> </where> </select> </mapper>
UserDao.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package com.course.dao;
import java.util.List;
import com.course.vo.User;
publicinterfaceUserDao { public List<User> queryUser01(User user); public List<User> queryUser02(User user); publicintupdateUserSet(User user); public List<User> queryUserByArray(int[] idArray); }