Mybatis动态sql语句(OGNL语法)

下面是Mybatis动态sql语句(即OGNL语法)的简单案例

 

1.创建表作为案例测试使用,剩下的Mybatis框架使用步骤就不写了,这里直接讲动态sql

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);
创建 test 表

 

2.<if> 标签

    <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>标签

    <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规则是我添加的,原本只有过滤逗号"," -->

 

转载于:https://www.cnblogs.com/Peter-Yu/p/9285088.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值