Mybatis--动态sql
AI-摘要
切换
Tianli GPT
AI初始化中...
介绍自己
生成本文简介
推荐相关文章
前往主页
前往tianli博客
本文最后更新于 2023-07-23,文章内容可能已经过时,请注意内容的辨别。
Mybatis--动态sql
if
<select id="getUserList" resultMap="userList">
select u.*,r.roleName from smbms_user u,smbms_role r where u.userRole = r.id
<if test="userRole != null">and u.userRole = #{userRole} </if>
<if test="userName != null and userName != ''">and u.userName like CONCAT ('%',#{userName},'%') </if>
</select>
trim
<select resultType="User" id="getUserList">
select * from smbms_user
<trim prefixOverrides="and | or" prefix="where">
<if test="userName != null and userName != ''">and userName like CONCAT ('%',#{userName},'%') </if>
<if test="userRole != null">and userRole = #{userRole} </if>
</trim>
</select>
<update id="modify" parameterType="User">
update smbms_user
<trim prefix="set" suffix="where id = #{id}" suffixOverrides=",">
<if test="userCode != null">userCode=#{userCode},</if>
<if test="userName != null">userName=#{userName},</if>
<if test="userPassword != null">userPassword=#{userPassword},</if>
<if test="gender != null">gender=#{gender},</if>
<if test="birthday != null">birthday=#{birthday},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="address != null">address=#{address},</if>
<if test="userRole != null">userRole=#{userRole},</if>
<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
<if test="modifyDate != null">modifyDate=#{modifyDate},</if>
</trim>
</update>
where
<select resultType="User" id="getUserList">
select * from smbms_user
<where>
<if test="userName != null and userName != ''">and userName like CONCAT ('%',#{userName},'%') </if>
<if test="userRole != null">and userRole = #{userRole} </if>
</where>
</select>
set
<update id="modify" parameterType="User">
update smbms_user
<set>
<if test="userCode != null">userCode=#{userCode},</if>
<if test="userName != null">userName=#{userName},</if>
<if test="userPassword != null">userPassword=#{userPassword},</if>
<if test="gender != null">gender=#{gender},</if>
<if test="birthday != null">birthday=#{birthday},</if>
<if test="phone != null">phone=#{phone},</if>
<if test="address != null">address=#{address},</if>
<if test="userRole != null">userRole=#{userRole},</if>
<if test="modifyBy != null">modifyBy=#{modifyBy},</if>
<if test="modifyDate != null">modifyDate=#{modifyDate}</if>
</set>
where id = #{id}
</update>
choose(when、otherwise)
<select resultType="User" id="getUserList_choose">
select * from smbms_user where 1=1
<choose>
<when test="userName != null and userName != ''">and userName like CONCAT ('%',#{userName},'%') </when>
<when test="userCode != null and userCode != ''">and userCode like CONCAT ('%',#{userCode},'%') </when>
<when test="userRole != null">and userRole=#{userRole} </when>
<otherwise>
<!-- and YEAR(creationDate) = YEAR(NOW()) -->
and YEAR(creationDate) = YEAR(#{creationDate})
</otherwise>
</choose>
</select>
foreach
<select id="getUserByConditionMap_foreach_map" resultMap="userMapByRole">
select * from smbms_user where gender = #{gender} and userRole in
<foreach close=")" separator="," open="(" item="roleMap" collection="roleIds">#{roleMap} </foreach>
</select>
<select id="getUserByRoleId_foreach_array" resultMap="userMapByRole">
select * from smbms_user where userRole in
<foreach close=")" separator="," open="(" item="roleIds" collection="array">#{roleIds} </foreach>
</select>
<select id="getUserByRoleId_foreach_list" resultMap="userMapByRole">
select * from smbms_user where userRole in
<foreach close=")" separator="," open="(" item="roleList" collection="list">#{roleList} </foreach>
</select>
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 小张同学
评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果