<if>The test attribute of the label writes the attribute name of the object. If it is an object of the packaging class, use the OGNL expression.
<selectid="findUserByCondition"resultType="com.xww.domain.User"parameterType="com.xww.domain.User">
select * from user where 1=1
<iftest="username != null">
and username = #{username}
</if></select>
31.3.2 where tag
<selectid="findUserByCondition"resultType="com.xww.domain.User"parameterType="com.xww.domain.User">
select * from user where
<where><iftest="username != null">
and username = #{username}
</if><iftest="sex != null">
and sex = #{sex}
</if></where></select>
31.3.3 forEach tag
SQL statement:select 字段from user where id in (?)
<foreach>The tag is used to traverse the collection, its attributes:
collection : representativeCollection element to be traversed, Pay attention to when writingDon't write #{}
open : represents the statementBeginning part
close : representativeEnd part
item : represents each element of the traversal collection, generatedvariable name
sperator : representativeDelimiter
<!-- 根据queryvo中的Id集合实现查询用户列表 --><selectid="findUserInIds"resultType="com.xww.domain.User"parameterType="com.xww.domain.QueryVo">
select * from user
<where><iftest="ids != null and ids.size()>0"><foreachcollection="ids"open="and id in ("close=")"item="id"separator=",">
#{id}
</foreach></if></where></select>
/**
* 测试foreach标签的使用
*/@TestpublicvoidtestFindInIds(){
QueryVo vo =newQueryVo();
List<Integer> list =newArrayList<Integer>();
list.add(41);
list.add(42);
list.add(46);
vo.setIds(list);//5.执行查询所有方法
List<User> users = userDao.findUserInIds(vo);for(User user : users){
System.out.println(user);}}