实现按性别和用户名查询
select * from [user] where and user.sex = ? and user.userName like ?
mybatis mapper.xml映射文件这样实现
<select id="findUserByInfo" parameterType="userVo" resultType="user">
select * from [user]
<where>
<!-- where会自动删除第一个and -->
<if test="user.sex != null and user.sex !=''">
and [user].sex = #{user.sex}
</if>
<if test="user.userName != null and user.userName !=''">
and [user].userName like '%${user.userName}%}
</if>
</where>
</select>
实现按用户序号查询多个用户详细信息
select * from [user] where [user].id in(?,?,?)
<select id="findUserByIds" parameterType="userVo" resultType="user">
select * from [user]
<where>
<if test="ids!=null">
<foreach collection="ids" item="userId" open="[user].id in(" close=")" separator=",">
#{userId}
</foreach>
</if>
</where>
</select>
用sql段编写代码
<sql id="query_user1">
...
</sql>
调用sql段代码
<include refid="query_user1> </include>