下面是Mybatis动态sql语句(即OGNL语法)的简单案例java
1.建立表做为案例测试使用,剩下的Mybatis框架使用步骤就不写了,这里直接讲动态sqlpython
create table test(id int primary key auto_increment,name varchar(20),job varchar(20),dept varchar(20),sal int) charset=utf8; insert into test values (null,'鲁班','java','甲',1456), (null,'后裔','java','甲',2440), (null,'刘禅','c','甲',3540), (null,'刘备','python','甲',4505), (null,'关羽','python','乙',1470), (null,'张飞','c','乙',2345), (null,'狄仁杰','java','乙',3640), (null,'兰陵王','c','丙',4000), (null,'花木兰','c','丙',1000), (null,'诸葛亮','java','丙',2047), (null,'甄姬','python','丁',3000), (null,'小乔','c','丁',4000), (null,'女蜗','java','丁',1000), (null,'妲己','java','丁',6705), (null,'公孙策','java','丁',null), (null,'百里守约','c','甲',null), (null,'小刘','python','丁',842), (null,'蔡文姬','python',null,500);
2.<if> 标签sql
<select id="selectTest" resultType="bean.TestBean" parameterType="bean.TestBean"> select * from test where <if test="id!=null"> <!-- test="id!=null" 这里的 id 值是调用了parameterType参数的 getId()方法获取的 --> <!-- 因此参数不能是基本类型(由于基本类型和包装类没有 get()方法) --> id=#{id} <!-- 若是 id 不等于 null ,这段sql语句变成以下所示--> </if> <!-- select * from test where id=#{id} or id=1 --> or id=1 <!-- 若是 id 等于 null(即 参数.getId() == null) ,那么语句这段sql语句变成以下所示 --> <!-- select * from where or id=1 报错!,这时候要用 <where> 标签防止这种状况发生 --> </select>
3.<where>标签框架
案例1
<select id="selectTest" resultType="bean.TestBean" parameterType="bean.TestBean">
select * from test <where> <!-- 使用<where>标签后,若是where后面紧跟着的是 or 和 and ,那么这两个关键字会被忽视--> <if test="id!=null"> <!-- 例如id等于null 那么这段代码变成 select * from where id=1 sql语句正常--> id=#{id} </if> or id=1 </where>
</select>
案例2
<select id="selectTest" resultType="bean.TestBean" parameterType="bean.TestBean"> select * from test <where> or id=1 <!-- or 被忽视,正常运行 --> </where> <!-- 接下来要讲的是<trim>标签,它能定义<where>的过滤规则 --> </select>
4.<trim>标签ide
<select id="selectTest" resultType="bean.TestBean"parameterType="bean.TestBean"> select * from test <trim prefix="where" prefixOverrides="and |or |abc "> <!-- 这段代码的效果个上面的案例2效果如出一辙 --> abc id=1 <!-- prefixOverrides:前缀覆盖--> </trim> <!-- 也就是说,where的后面紧跟着的是 and\or\abc,那么这些关键字都会被忽略 --> </select> <!-- 要注意 | 后面不能有空格,例如: |a 和| a 后面这个a和|之间有空格,会致使忽略失败 -->
5.<set>标签测试
案例1
<update id="updateTest" parameterType="bean.TestBean">
update test set <if test="name!=null"> name=#{name}, </if> <if test="sal!=null"> sal=#{sal} </if> where id=#{id}
</update>
<!-- 上面这段代码,尚未使用<set>标签,这段代码有个漏洞,若是 name!=null ,且 sal==null ,那么语句变成-->
<!-- update test set name=#{name}, where id=#{id} -->
<!-- 这里的逗号跟着写进来了,程序理所固然的报错,这时候咱们就要用到<set>标签解决这个问题 --
案例2
<update id="updateTest" parameterType="bean.TestBean"> update test <set> <if test="name!=null"> name=#{name}, </if> <if test="sal!=null"> sal=#{sal} </if> </set> where id=#{id} </update>
<!-- <set>标签和<where>标签有点相反的意思,<set>标签订义了会忽视最后的逗号 “,” 例如<set> name=#{name},</set>里面,最后的是“,”结尾,因此被忽视了,程序正常运行-->
<!-- 固然,<trim>标签一样能够定义<set>标签的规则,下面案例能够看到 -->
案例3
<update id="updateTest" parameterType="bean.TestBean"> update test <trim prefix="SET" suffixOverrides=", |abc"> <if test="name!=null"> name=#{name}, </if> <if test="sal!=null"> sal=#{sal}abc </if> </trim> where id=#{id} </update>
<!-- 使用<trim>定义<set>规则 -->
<!-- suffixOverrides=", |abc",定义了不管是逗号","结尾仍是"abc"结尾,都会被程序忽视,上面程序正常运行 -->
<!-- 文中的abc规则是我添加的,本来只有过滤逗号"," -->