mybatis3----配置动态SQL语句
1.where 和 if 标签
1.作用
where标签:where
标签可以过滤掉条件语句中的第一个and
或or
关键字。
if标签:if
标签一般用于WHERE语句中,经过判断参数值来决定是否使用某个查询条件
2.例子
UserTabMapper 接口
List<UserTab> getUserList(@Param("userName") String userName, @Param("userAccount") String userAccount);
UserTabMapper.xml
<select id="getUserList" resultMap="baseResult">
select user_name,user_account,password from user_tab
<where>
<if test="userName != null and userName != ''">
and user_name = #{userName}
</if>
<if test="userAccount != null and userAccount != ''">
and user_account = #{userAccount}
</if>
</where>
</select>
PS:
1.where
标签只能去除第一个条件中出现的前置 and 关键字。像以下情况,where
无法去除掉后面的and关键字,此时sql语句出现语法错误。
<where>
<if test="userName != null and userName != ''">
user_name = #{userName} and
</if>
<if test="userAccount != null and userAccount != ''">
user_account = #{userAccount} and
</if>
</where>
2. foreach标签
1.作用
用于遍历集合,构建in条件语句或者批量操作语句
2.例子
1.单参数是List 类型
UserTabMapper 接口
List<UserTab> getUserList(@Param("userAccountList") List<String> userAccountList);
UserTabMapper.xml
<select id="getUserList" resultMap="baseResult">
select user_name,user_account,password from user_tab
<where>
<if test="userAccountList != null and userAccountList.size() > 0">
and user_account in
<foreach collection="userAccountList" open="(" close=")" item="userAccount" separator=",">
#{userAccount}
</foreach>
</if>
</where>
</select>
2.单参数array数组的类型
UserTabMapper 接口
List<UserTab> getUserList(@Param("userAccountList") String[] userAccountList);
UserTabMapper.xml
<select id="getUserList" resultMap="baseResult">
select user_name,user_account,password from user_tab
<where>
<if test="userAccountList != null and userAccountList.length > 0">
and user_account in
<foreach collection="userAccountList" open="(" close=")" item="userAccount" separator=",">
#{userAccount}
</foreach>
</if>
</where>
</select>
3.参数封装成Map的类型
UserTabServiceImpl
@Override
public List<UserTab> getUserList(String[] userAccountList) {
Map params = new HashMap();
params.put("userAccountList",userAccountList);
return userTabMapper.getUserList(params);
}
UserTabMapper 接口
List<UserTab> getUserList(@Param("params") Map params);
UserTabMapper.xml
<select id="getUserList" resultMap="baseResult">
select user_name,user_account,password from user_tab
<where>
<if test="params.userAccountList != null and params.userAccountList.length > 0">
and user_account in
<foreach collection="params.userAccountList" open="(" close=")" item="userAccount" separator=",">
#{userAccount}
</foreach>
</if>
</where>
</select>
PS:
1.foreach标签主要有以下参数
- collection: 指定输入对象中的集合属性
- item: 集合中元素迭代时的别名(自定义,但#{}里的字段名必须和item一样)
- open为开始遍历时拼接的字符串
- close为结束便利时要拼接的字符串
- separator为遍历两个对象中间需要拼接的串
2.sql语句SELECT * FROM user_tab WHERE id IN () 这个会报错,所以需要判断一下参数是有值的
3.choose、when、otherwise 标签
1.作用
这三个标签需要组合在一起使用,类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。只有一个条件生效,也就是只执行满足的条件 when,没有满足的条件就执行 otherwise,表示默认条件。
2.例子
UserTabMapper 接口
List<UserTab> getUserList(@Param("userName") String userName, @Param("userAccount") String userAccount);
UserTabMapper.xml
<select id="getUserList" resultMap="baseResult">
select user_name,user_account,password from user_tab
<where>
<choose>
<when test="userName != null and userName != ''">
and user_name = #{userName}
</when>
<when test="userAccount != null and userAccount != ''">
and user_account = #{userAccount}
</when>
<otherwise>
and user_account = 'admin'
</otherwise>
</choose>
</where>
</select>
4.set标签
1.作用
主要是用在更新操作的时候,动态的配置SET 关键字和剔除追加到条件末尾的任何不相关的逗号,使用 if+set 标签修改后,如果某项为 null 则不进行更新,而是保持数据库原值。
2.例子
UserTabMapper 接口
void updateUserById(@Param("userId") Integer userId,@Param("userName") String userName,@Param("userAccount") String userAccount);
UserTabMapper.xml
<update id="updateUserById">
update user_tab
<set>
<if test="userName != null and userName != ''">
user_name =#{userName},
</if>
<if test="userAccount != null and userAccount != ''">
user_account =#{userAccount},
</if>
</set>
where user_id =#{userId}
</update>
5.trim标签
1.作用
一般用于去除sql语句中多余的and关键字,逗号,或者给sql语句前拼接 “where“、“set“以及“values(“ 等前缀,或者添加“)“等后缀,可用于选择性插入、更新、删除或者条件查询等操作。
以下是trim标签中涉及到的属性:
属性 | 描述 |
---|---|
prefix | 给sql语句拼接的前缀 |
suffix | 给sql语句拼接的后缀 |
prefixOverrides | 去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为"AND",当sql语句的开头为"AND",trim标签将会去除该"AND" |
suffixOverrides | 去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定 |
2.例子
UserTabMapper 接口
void insertUser(@Param("userName") String userName,@Param("userAccount") String userAccount,@Param("password") String password);
UserTabMapper.xml
<insert id="insertUser">
insert into user_tab (user_account,state
<trim prefix="," suffixOverrides=",">
<if test="userName != null and userName != ''">
user_name,
</if>
<if test="password != null and password != ''">
password,
</if>
</trim>
) values (#{userAccount},1
<trim prefix="," suffixOverrides=",">
<if test="userName != null and userName != ''">
#{userName},
</if>
<if test="password != null and password != ''">
#{password},
</if>
</trim>
)
</insert>