<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.UserInfoMapper">
<!-- 动态SQL之<if>元素 -->
<select id="findUserInfoByUserNameWithIf" parameterType="UserInfo"
resultType="UserInfo">
select * from user_info ui
<if test="userName != null and userName != ''">
where ui.userName LIKE CONCAT(CONCAT('%',#{userName}),'%')
</if>
</select>
<!-- 动态SQL之<where>、<if>元素 -->
<select id="findUserInfoByUserNameAndStatus" parameterType="UserInfo"
resultType="UserInfo">
select * from user_info ui
<where>
<if test="userName!=null and userName!=''">
ui.userName LIKE CONCAT(CONCAT('%', #{userName}),'%')
</if>
<if test="status>-1">
and ui.status = #{status}
</if>
</where>
</select>
<!-- 动态SQL之<set>、<if>元素 -->
<update id="updateUserInfo2" parameterType="UserInfo">
update user_info ui
<set>
<if test="userName!=null and userName!=''">
ui.userName=#{userName},
</if>
<if test="password!=null and password!=''">
ui.password=#{password}
</if>
</set>
where ui.id=#{id}
</update>
<!-- 动态SQL之<trim>元素替代<where>元素 -->
<select id="findUserInfoByUserNameWithIf_Trim" parameterType="UserInfo"
resultType="UserInfo">
select * from user_info ui
<trim prefix="where" prefixOverrides="and|or">
<if test="userName!=null and userName!=''">
ui.userName LIKE CONCAT(CONCAT('%', #{userName}),'%')
</if>
<if test="status>-1">
and ui.status = #{status}
</if>
</trim>
</select>
<!-- 动态SQL之<trim>元素替代<set>元素 -->
<update id="updateUserInfo2_trim" parameterType="UserInfo">
update user_info ui
<trim prefix="set" suffixOverrides=",">
<if test="userName!=null and userName!=''">
ui.userName=#{userName},
</if>
<if test="password!=null and password!=''">
ui.password=#{password}
</if>
</trim>
where ui.id=#{id}
</update>
<!-- 动态SQL之<choose>、<when>和<otherwise>元素 -->
<select id="findUserInfo_Choose" parameterType="UserInfo"
resultType="UserInfo">
select * from user_info ui
<where>
<choose>
<when test="userName!=null and userName!=''">
ui.userName LIKE
CONCAT(CONCAT('%',#{userName}),'%')
</when>
<when test="status>-1">
and ui.status = #{status}
</when>
<otherwise>
</otherwise>
</choose>
</where>
</select>
<!-- 动态SQL之<foreach>元素 -->
<select id="findUserInfoByIds" resultType="UserInfo">
select * from user_info ui where ui.id in
<foreach collection="list" item="ids" open="(" separator=","
close=")">
#{ids}
</foreach>
</select>
<select id="findUserInfoByIds2" resultType="UserInfo">
select * from user_info ui where ui.id in
<foreach collection="array" item="ids" open="(" separator=","
close=")">
#{ids}
</foreach>
</select>
<!-- 根据用户编号获取用户信息 -->
<select id="findUserInfoById" parameterType="int" resultType="UserInfo">
select *
from user_info where id = #{id}
</select>
<!-- 根据用户名模糊查询用户 -->
<select id="findUserInfoByUserName" parameterType="String"
resultType="UserInfo">
select * from user_info where userName like
CONCAT(CONCAT('%',#{userName}),'%')
</select>
<!-- 添加用户 -->
<insert id="addUserInfo" parameterType="UserInfo">
insert into
user_info(userName,password) values(#{userName},#{password})
</insert>
<!-- 修改用户信息 -->
<update id="updateUserInfo" parameterType="UserInfo">
update user_info set
userName=#{userName}, password=#{password} where id=#{id}
</update>
<!-- 根据用户编号删除用户 -->
<delete id="deleteUserInfo" parameterType="int">
delete from user_info
where id=#{id}
</delete>
</mapper>
mybatis 使用(5)动态SQL
猜你喜欢
转载自blog.csdn.net/qq_37769323/article/details/104320478
今日推荐
周排行