Mybatis 动态SQL
Mybatis 动态SQL OGNL表达式
在 MyBatis 中提供了动态 SQL 功能。将使用 Java 代码拼接 SQL 语句,改变为在 XML 映65
射文件中使用标签拼接 SQL 语句。 MyBatis 中动态 SQL 是编写在 mapper.xml 中的,其语法和 JSTL 类似,但是却是基于强大 的 OGNL 表达式实现的。
1.添加多条件查询 用if
1.1在mapper配置文件中修改sql,添加多条件查询
代码片段
点击查看代码
<select id="selectUsersByProperty" resultType="Users">
select * from users where 1=1
<!-- 使用 pojo 为参数传递 , 而且参数里面名字不能乱写,否则接受不到参数-->
<if test="userid != 0">and userid = #{userid}</if>
<if test="username != null and username != ''">and username = #{username}</if>
<if test="usersex != null and usersex != ''">and usersex = #{usersex}</if>
</select>
点击查看代码
package com.bjsxt.service.impl;
import com.bjsxt.mapper.UsersMapper;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UsersService;
import com.bjsxt.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class UsersServiceImpl implements UsersService {
@Override
public List<Users> selectUsersByProperty(Users users) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
List<Users> list = usersMapper.selectUsersByProperty(users);
return list;
}
}
2.choose when otherwise
用法和switch差不多,进行多选一查询时,可用
代码片段
点击查看代码
<select id="selectUsersByChoose" resultType="Users">
<choose>
<when test="username != null and username != ''">and username = #{username}</when>
<when test="usersex != null and usersex != ''">and usersex = #{usersex}</when>
<otherwise>and userid = #{userid}</otherwise>
</choose>
</select>
3.where
使用 where 标签,就不需要提供 where 1=1 这样的条件了。如果判断条件不为空则自 动添加 where 关键字,并且会自动去掉第一个条件前面的 and 或 or。
代码片段
点击查看代码
<select id="selectUsersByPropertyWhere" resultType="Users">
select * from users
<where>
<if test="userid != 0">and userid = #{userid}</if>
<if test="username != null and username != ''">and username = #{username}</if>
<if test="usersex != null and usersex != ''">and usersex = #{usersex}</if>
</where>
</select>
4.bind
bind 标签允许我们在 OGNL 表达式以外创建一个变量,并可以将其绑定到当前的 SQL 语句中。一般应用于模糊查询,通过 bind 绑定通配符和查询值。
代码片段‘
点击查看代码
<!-- 模糊查询,拼接字符串-->
<select id="selectUsersByLikeName" resultType="Users">
<!-- select * from users where username like concat('%',l,'%'); -->
<!-- name 是一个变量,传过来的值会放入name,然后进行拼接 -->
<bind name="LikeName" value="'%'+name+'%'"/>
select * from users where username like #{LikeName};
</select>
5.set
set 标签用在 update 语句中。借助 if 标签,可以只对有具体值的字段进行更新。set 标 签会自动添加 set 关键字,自动去掉最后一个 if 语句的多余的逗号。
点击查看代码
<update id="usersUpdate">
update users
<set>
<if test="username != null and username != ''">username = #{username},</if>
<if test="usersex != null and usersex != ''">usersex = #{usersex},</if>
</set>
where userid = #{userid}
</update>
6.foreach
foreach 标签的功能非常强大,我们可以将任何可迭代对象如 List、Set 、Map 或者数 组对象作为集合参数传递给 foreach 标签进行遍历。它也允许我们指定开头与结尾的字符串 以及集合项迭代之间的分隔符。

点击查看代码
<select id="selectUsersByIdUseCollection" resultType="users">
select * from users where userid in
-- 将传过来的collection(list,set) 遍历,以(开始, 逗号分割每个元素,)结束,结果传给userid
<foreach collection="collection" item="userid" open="(" separator="," close=")">
-- 获取userid的值
#{userid}
</foreach>
</select>
6.1
数组(array)存放
点击查看代码
<select id="selectUsersByIdUseArray" resultType="users">
select * from users where userid in
<foreach collection="array" item="userid" open="(" separator="," close=")">
#{userid}
</foreach>
</select>
6.2
map存放
注意${} 和 #{} 获取值的区别
点击查看代码
<select id="selectUsersCount" resultType="int">
select count(*) from users where
<!-- index 存放每个属性值对应的 key -->
<foreach collection="suibian" item="value" index="key" separator="and">
<!-- ${}这个不用编译位?#{}会被编译为?-->
${key} = #{value}
</foreach>
</select>
6.3
同时添加多个值
点击查看代码
<update id="insertUsersBatch">
insert into users value
<foreach collection="collection" item="user" separator=",">
(default,#{user.username},#{user.usersex} )
</foreach>
</update>

浙公网安备 33010602011771号