mybatis动态SQL
来源:互联网 发布:c 怎么获取数组长度 编辑:程序博客网 时间:2024/05/17 01:34
mybatis提供了对SQL语句动态的组装能力,利用几个简单的基本元素,就可以进行大量的判断工作。我们可以在mapper.xml中配置,这样可以大大减少我们工作。(也可以使用注解方式,但是该方式受限,可读性差,故仅介绍xml配置)
1.概述
mybatis主要包含如下几种元素
2.if元素
if可以说是最常用的判断语句,常常与test属性联合使用。
基本用法:
假设此处要根据角色名称(roleName)来查找角色,但是角色名称可填可不填。此处就用if简单判断
<select id="findRoles" parameterType="String" resultType="roleResultMap"> select role_no,ro_name,note from t_role where 1=1 <if test="roleName!=null and roleName!=''"> and role_name like concat('%',#{roleName},'%') </if> </select>
这里写了句 “where 1=1”,为什么要写1=1呢?因为这是用来防止我们没有匹配到roleName。 如果不写这句话,就会变成”select …略 from t_role where “,这样就无法运行了。
3.choose, when, otherwise元素
当我们有多种选择的时候,就需要用这几个元素了,类似于我们定的”switch…case…default”。下面看看例子:
假设我们玩游戏查询一个角色时候有如下要求:1.当角色编号不为空,则只用角色编号查询;2.当角色编号为空,角色名称不为空,则用角色名称查询;3.当两者都为空,则发出警告”角色备注不能为空!”
<select id="findRoles" parameterType="String" resultType="roleResultMap"> select role_no,ro_name,note from t_role where 1=1 <choose> <when test="roleNo!=null ande roleNo!=''"> and role_no=#{roleNo} </when> <when test="roleName!=null and roleName!=''"> and role_name like concat('%',#{roleName},'%') </when> <otherwise> and note is not null </otherwise> </choose> </select>
4)trim, where, set元素
我们在if元素中加了个”1=1”,我也解释了如果不添加,有可能SQL语句会出错。但是添加这句话又显得很奇怪,那么如何去掉呢?用where元素就可以了。让我们再把if元素中的代码重写一遍,对照看看不同点:
修改前:
<select id="findRoles" parameterType="String" resultType="roleResultMap"> select role_no,ro_name,note from t_role where 1=1 <if test="roleName!=null and roleName!=''"> and role_name like concat('%',#{roleName},'%') </if> </select>
修改后
<select id="findRoles" parameterType="String" resultType="roleResultMap"> select role_no,ro_name,note from t_role <where> <if test="roleName!=null and roleName!=''"> and role_name like concat('%',#{roleName},'%') </if> </where> </select>
这样一来,只有当where元素里的条件成立时候,才会加入where这个关键字进入SQL,否则就不加
有时候我们要去掉一些特殊的SQL语法,比如and,or。使用trim元素可以达到预期效果
<select id="findRoles" parameterType="String" resultType="roleResultMap"> select role_no,ro_name,note from t_role <trim prefix="where" prefixOverrides="and"> <if test="roleName!=null and roleName!=''"> and role_name like concat('%',#{roleName},'%') </if> </trim> </select>
解释下,trim元素一位置我们要去掉一些特殊的字符串,prefix代表的是语句的前缀,而prefixOverrides代表你要去掉的字符串,上面写法基本和where元素语句等效
(感觉if语句中好像都写了and啊,这里的where和trim就是两种方法把那个and注释掉)
hibernate中更新一个对象,需要全部属性全部更新一遍,假设我们有1000个属性,那么全部更新就太麻烦了。此处我们就用mybatis的set元素完成多个属性中仅仅更新少量属性
<update id="updateRole" parameterType="role"> update t_role <set> <if test="roleName !=null and roleName!=''"> role_name=#{roleName}, </if> <if test="note!=null and note !=''"> note=#{note} </if> </set> where role_no=#{roleNo} </update>
5)foreach元素
假设有个表的列为sex,里面有三个对应的数:1-男 2-女 0-未知。我们要遍历查询,应该如下写代码:
<select id="findUserBySex" resultType="user"> select * from t_user where sex in <foreach collection="sexList" index="index" item="sex" open="(" separator="," close=")"> #{sex} </foreach> </select>
说明,collection配置的sexList是传递进来的参数名称,是个List、Set等集合;item配置的是循环中当前的元素;index配置的是当前元素的位置下标;open和close配置的是以什么符号将这些元素集合包装起来;separator是各个元素的间隔符
6)test属性
test用于条件判断的语句中,常常用于判断空或者非空,通常和if连用。代码如下
<select id="getRoleTest" parameterType="String" resultMap="roleResultMap"> select role_no,role_name,note from t_role <if test="type='Y'"> where 1=1 </if> </select>
把type=’Y’传递给这条SQL,我们发现程序自动加入了 where 1=1这句话。
7)bind元素
bind元素时通过OGNL表达式去自定义一个上下文变量,这样方便我们使用。在我们进行模糊查询时候,如果是mysql数据库,常常用到 concat来把“%”和参数相连;而在Oracle中则是用连接符号“II”,这样SQL需要提供两种形式去实现。但是bind元素我们可以不用关心数据库的语言,只要用mybatis的语言即可。
比如我们要按照角色名称进行模糊查询,可以这么写:
<select id="findRole" resultType="com.learn.chapter5.mybatis.bean.RoleBean"> <bind name="pattern" value="'%'+_parameter+'%'" /> select id,role_name as roleName,create_date as createDate,end_date as endDate,end_flag as endFlag,note from t_role where role_name like #{pattern} </select>
这里的“_parameter”代表的就是传递进来的参数,它和通配符连接后,赋给#{pattern},我们就可以进行模糊查询,不管是MySQL还是Oracle都可以使用这样的语句
那么如何传递多个参数呢?代码如下
首先,定义接口方法:
public List<RoleBean> findRole(@Param("roleName") String roleName,@Param("note") String note);
然后定义映射文件,定义两个新的变量去执行模糊查询
<select id="findRole" resultType="com.learn.chapter5.mybatis.bean.RoleBean"> <bind name="pattern_roleName" value="'%'+roleName+'%'" /> <bind name="pattern_note" value="'%'+note+'%'" /> select id,role_name as roleName,create_date as createDate,end_date as endDate,end_flag as endFlag,note from t_role where role_name like #{pattern_roleName} and note like #{pattern_note} </select>
- mybatis动态SQL语句
- MyBatis动态SQL
- MyBatis 动态SQL
- Mybatis 动态SQL
- MyBatis动态SQL
- MyBatis动态SQL完整版
- mybatis动态sql
- mybatis动态SQL语句
- mybatis动态SQL语句
- MyBatis的动态SQL
- MyBatis动态SQL
- MyBatis动态SQL
- MyBatis 动态SQL语句
- MyBatis动态SQL
- Mybatis组建动态SQL
- MyBatis 动态sql
- mybatis 动态SQL语句
- MyBatis动态SQL详解
- prototype.js常用函数详解
- Linux服务器信息查看
- Hdu 3016 Man Down【线段树+Dp】
- 移动端ionic App 资讯上下循环滚动的实现
- HDU 5733 tetrahedron (求四面体内切球模板)
- mybatis动态SQL
- ListView item的点击冲突解决办法
- 模拟unity自带地形的grass
- 008
- Zookeeper
- NSString字符串截取的几种方式
- 如何防止高并发
- 进程间的通信方式
- java-冒泡排序