<select id="selectUserListByUser" parameterType="User" resultType="User"><!-- 查询用户性别,模糊查询用户名 查询用户cid -->
select *
from user
<where><!-- where标签可以去掉开头的and --><if test="u_sex != null and u_sex != ''">
u_sex=#{u_sex}</if><if test="u_username != null and u_username != ''">
and u_username like "%"#{u_username}"%"</if><if test="u_cid != null">
and u_cid = #{u_cid}</if></where></select>
trim标签 - 定制where标签的规则
<select id="selectUserListByUserTrim" parameterType="User" resultType="User"><!-- 查询用户性别,模糊查询用户名 查询用户cid -->
select *
from user
<trim prefix="where" suffixOverrides="AND"><if test="u_sex != null and u_sex != ''">
u_sex=#{u_sex} and
</if><if test="u_username != null and u_username != ''">
u_username like "%"#{u_username}"%" and
</if><if test="u_cid != null">
u_cid = #{u_cid}</if></trim></select>
set标签 - 解决更新数据表时字符串拼接逗号”,”问题;
<!--//更新用户表 --><!--publicvoidupdateSetUser(User u);--><update id="updateSetUser" parameterType="User">
update user
<set><if test="u_username != null and u_username !=''">
u_username = #{u_username},</if><if test="u_password != null and u_password !=''">
u_password = #{u_password},</if><if test="u_sex != null and u_sex !=''">
u_sex = #{u_sex }</if></set>
where
u_id = #{u_id}</update>
foreach标签 – 如果需要使用IN查询多条相同数据,可以使用foreach遍历;
<!--//使用多个id获取用户列表 --><!--public List<User>selectUserListByIds();(1,3,5)--><select id="selectUserListByIds" resultType="User">
SELECT *
FROM USER
WHERE u_id
IN
<!--(1,2,3)--><foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}</foreach></select>