mybatis —— 动态sql之if条件判断各种使用方式

点滴记载,点滴进步,愿自己更上一层楼。

用 mybatis 执行数据库操作仅仅能看到执行结果,如果想看到执行的 sql 语句怎么办。

查阅 mybatis 官方文档找到了解决方法。

配置什么的很简单,用的 log4j 打印,当然参照官方文档还有好几种方法,具体自弄。

这里仅作记录只用。配置很简单,将 log4j 架包加入到 classpath 里。

maven 配置。

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

 

非 maven 项目只需要将 jar 添加到项目中即可。

log4j.properties 添加到 source 根目录。

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
#log4j.logger.com.soft.test.dao=DEBUG
log4j.logger.dynamic=DEBUG
#log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

其中关键的地方是
log4j.logger.dynamic=DEBUG

log4j.logger 是固定的,dynamic 为你的 mapper.xml 的 namespace

如果我的 xml 中的 namespace 为 dynamic

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 命名空间,跟 java 的 package 类似,避免 sql id 重复,
有了这个命名空间,别的 xml 中的 sql 的 id 可以跟这个重复, 并且 namespace 不能省略,不能为空,不用接口开发,此处可以随意写-->
<mapper namespace="dynamic">
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">resultMap </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="userMap"</span><span style="color: rgba(255, 0, 0, 1)"> type</span><span style="color: rgba(0, 0, 255, 1)">="com.soft.test.model.User"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">id </span><span style="color: rgba(255, 0, 0, 1)">column</span><span style="color: rgba(0, 0, 255, 1)">="id"</span><span style="color: rgba(255, 0, 0, 1)"> property</span><span style="color: rgba(0, 0, 255, 1)">="id"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">result </span><span style="color: rgba(255, 0, 0, 1)">column</span><span style="color: rgba(0, 0, 255, 1)">="username"</span><span style="color: rgba(255, 0, 0, 1)"> property</span><span style="color: rgba(0, 0, 255, 1)">="username"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">result </span><span style="color: rgba(255, 0, 0, 1)">column</span><span style="color: rgba(0, 0, 255, 1)">="password"</span><span style="color: rgba(255, 0, 0, 1)"> property</span><span style="color: rgba(0, 0, 255, 1)">="password"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">result </span><span style="color: rgba(255, 0, 0, 1)">column</span><span style="color: rgba(0, 0, 255, 1)">="create_date"</span><span style="color: rgba(255, 0, 0, 1)"> property</span><span style="color: rgba(0, 0, 255, 1)">="createDate"</span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">resultMap</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">if 标签使用类似html的C标签的if </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">select </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="selectUseIf"</span><span style="color: rgba(255, 0, 0, 1)"> parameterType</span><span style="color: rgba(0, 0, 255, 1)">="com.soft.test.model.User"</span><span style="color: rgba(255, 0, 0, 1)"> resultMap</span><span style="color: rgba(0, 0, 255, 1)">="userMap"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
    select * from t_user where
    </span><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">if </span><span style="color: rgba(255, 0, 0, 1)">test</span><span style="color: rgba(0, 0, 255, 1)">="id != null and id != ''"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
        id=#{id}
    </span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">if</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">if </span><span style="color: rgba(255, 0, 0, 1)">test</span><span style="color: rgba(0, 0, 255, 1)">="username != null and username != ''"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
        and username like concat('%',#{username},'%')
    </span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">if</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">if </span><span style="color: rgba(255, 0, 0, 1)">test</span><span style="color: rgba(0, 0, 255, 1)">="password != null and password != ''"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">
        and password=#{password}
    </span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">if</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">select</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

</mapper>

配置完成。现在运行测试即可看到运行的 sql 语句
------------------------------------------------------------------------------------------------------------------------------

DEBUG [main] - ==>  Preparing: select * from t_user where id=? and username like concat('%',?,'%') 
DEBUG [main] - ==> Parameters: 28(Integer), xiao(String)
DEBUG [main] - <==      Total: 1

-------------------------------------------------------------------------------------------------------------------------------

mybatis 的 if 判断语句其实跟 el 表达式的 if 条件判断有些类似。

例如: <if test="id != null"> </if>

1 如果参数为数字类型的时候没有特俗需求的情况只需要判断是否为 null 即可。

例如:<if test="id != null"></if>

 如果有特俗需求,例如判断是否大于某个数的时候才行。只需要加上对应的条件判断即可

例如:<if test='id != null and id > 28'></if>

 mybatis 对于这种大于小于等等还有另一种形式。

例如:<if test='id != null and id gt 28'></if>

对应关系:

---------------------------------------

    gt            对应             >

    gte         对应              >=

    lt             对应              <(会报错  相关联的 "test" 属性值不能包含 '<' 字符)

    lte          对应               <=(会报错  相关联的 "test" 属性值不能包含 '<' 字符)

---------------------------------------

2 如果为字符串类型

2.1 如果不需要过滤空串的情况 仅仅判断 null 即可

例如:<if test="username != null"></if>

2.2 如果需要过滤空串,添加空串判断即可  不支持 &&   所以这里用 and  or  || 来做逻辑与或的判断 

例如:<if test="username != null and''!= username"></if> 或者 <if test="username != null and'' neq username"></if>

2.3 如果判断字符串是否已某个特俗字符开头,结尾等。直接调用 String 的对应方法即可

例如:<if test="username != null and username.indexOf('ji') == 0"> </if> <!-- 是否以什么开头 -->
    <if test="username != null and username.indexOf('ji') >= 0"> </if> <!-- 是否包含某字符 -->
    <if test="username != null and username.lastIndexOf('ji') > 0"></if>  <!-- 是否以什么结尾 -->

2.4 是否是某个特定字符串,某些业务有此需要。

例如:<if test="username != null and'hello' == username"></if> 或者 <if test="username != null and'hello' eq username"></if>

注意:

<if test="username != null and 'hello' == username"></if> 这种形式的写法在参数类型是字符串的时候是没有问题的,

但是参数类型为非字符串类型的时候就需要写成 <if test="username != null and 'hello'.toString() == username.toString()"></if>

仅仅写成 <if test="username != null and 'hello'.toString() == username"></if> 也会有很大可能会挂。

也许你会说非字符串的为什么要写成这样。这就要看特俗需要了。

例如:某一个 sql 片段是公用的,

<if test="username != null"></if>
<if test="password != null"></if>

该片段更新条件也用,但是当你需要将某一个字段更新成 null 的时候怎么办。

这个时候就可以通过传入一个特定的字符串来弄。当传入的字符串为特定字符串的时候就更新该字符串为 null。

<if test="username != null and 'hello'.toString() == username.toString()">xxx=null</if>

当然这样子貌似 date 型会挂。

通过 2.2 也可以看出 mybatis 对于字符串的相等不相等的判断也是有对应的特俗操作符的。

-------------------------------------------------------

eq                  对应                ==

neq               对应                 !=

------------------------------------------------------

当然还可以看出来 if 的条件判断 test 是支持对象自身方法调用的,即使是自己写的方法,可以自己尝试。当然下面会有例子。

例如:里面可以用‘xxxx’.equals(xxxx) 字符串的比较两个字符串方法

    xxxx.indexOf('ss') 判断字符串里面是否包含某个字符等等  

 3 判断 list 是否为空

上面说过,if 条件判断可以直接调用对象自身的方法进行逻辑判断,所以 list 判空。可以调用.size()>0 或者.isEmpty()

例如:<if test="userList != null and userList.isEmpty()"></if> , <if test="userList != null and userList.size()>0"></if>

4 map 参数同同理  取值的话 map.key(map 中的 key 名字) 即可

---------------------------------------------------------------------------------------------- 分割线 01-----------------------------------------------------------------------------------------------------------

这里是上面的各种理论的实践。这里可以不看,自己去实践最好。

1 数字类型。

仅作 null 判断。

 <!--if 标签使用类似 html 的 C 标签的 if -->
    <select id="selectUseIf" parameterType="com.soft.test.model.DynamicTestModel" resultMap="userMap">
        select * from t_user where 1=1
        <if test='id != null'>
            and id=#{id}
        </if>
    </select>

当 id 不为 null 的时候 打印的 log
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? 
DEBUG [main] - ==> Parameters: 28(Integer)
DEBUG [main] - <==      Total: 1

当 id 为 null 的时候 打印的 log
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 1

两项对比,可以看出 id=? 这个条件随着传入参数 id 的变化而变化。

当有特俗需求的时候,当前数据库中的表 id 为 28 仅仅有这么一条数据,做一下轻微的改动

<if test='id != null and id > 27'>

当传入 id=28 的时候
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? 
DEBUG [main] - ==> Parameters: 28(Integer)
DEBUG [main] - <==      Total: 1

当传入 id 小于 28 的时候

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 1

接下来测试下面这几种对应关系。

---------------------------------------

    gt            对应             >

    gte         对应              >=


    lt             对应              <(会报错  相关联的 "test" 属性值不能包含 '<' 字符)

            lte          对应               <=(会报错  相关联的 "test" 属性值不能包含 '<' 字符)

---------------------------------------

gt   

<if test='id != null and id gt 27'>

参数  id=25
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters:

参数  id=28
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? 
DEBUG [main] - ==> Parameters: 28(Integer)

 >= 

<if test='id != null and id >= 28'>

参数  id=28
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? 
DEBUG [main] - ==> Parameters: 28(Integer)

参数  id=27

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 

gte   

<if test='id != null and id gte 28'>

参数  id=28
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? 
DEBUG [main] - ==> Parameters: 28(Integer)

参数  id=27

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 

    使用 <   <= 直接报错       "test" 属性值不能包含 '<' 字符 看来只能用 lt   lte 了

lt

<if test='id != null and id lt 28'>

参数  id=28  
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 
参数  id=27

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? 
DEBUG [main] - ==> Parameters: 27(Integer)

lte

<if test='id != null and id lte 28'>

参数  id=28  

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and id=? 
DEBUG [main] - ==> Parameters: 28(Integer)

参数  id=29

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 

---------------------------------------------------------------------------------------------- 分割线 02-----------------------------------------------------------------------------------------------------------

2.1 跟 1 的第一条一样不做重复测试

2.2 过滤空串

<select id="selectUseIf" parameterType="com.soft.test.model.DynamicTestModel" resultMap="userMap">
        select * from t_user where 1=1
        <if test="username != null and''!= username">
            and username=#{username}
        </if>
    </select>

!=
username=“xiao”
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? 
DEBUG [main] - ==> Parameters: xiao(String)

username=“”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 

neq

<if test="username != null and''neq username">

username=“xiao”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? 
DEBUG [main] - ==> Parameters: xiao(String)

username=“”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 

各个逻辑与或的判断
and 上面已经弄过了,这里弄 or  || 两种条件  

<if test="'xiaohong' eq username or 'xiao' eq username ">

or

username=“xiao”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? 
DEBUG [main] - ==> Parameters: xiao(String)
username=“xiaohong”
DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? 
DEBUG [main] - ==> Parameters: xiaohong(String)

username=“xiaofang”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 

||

username=“xiao”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? 
DEBUG [main] - ==> Parameters: xiao(String)


username=“xiaohong”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? 
DEBUG [main] - ==> Parameters: xiaohong(String)

username=“xiaofang”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 
DEBUG [main] - ==> Parameters: 

2.3  indexOf() lastIndexOf()  判断是否包含某个特定字符

<if test="username != null and username.indexOf('xiao')==0">

username=“xiao”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? 
DEBUG [main] - ==> Parameters: xiao(String)

username=“xiaofang”

DEBUG [main] - ==>  Preparing: select * from t_user where 1=1 and username=? 
DEBUG [main] - ==> Parameters: xiaofang(String)
其他两个没什么大不同的。自行测试。
2.4   判断是否是某个字符

<if test="'xiaohong' eq username">

username=“xiaohong”
==>  Preparing: select * from t_user where 1=1 and username=? 
==> Parameters: xiaohong(String)

username=“xiaofang”

==>  Preparing: select * from t_user where 1=1 
==> Parameters: 

3  4  的本质就是再说 mybatis 的 if 条件判断语句可以直接执行对象的方法。下面自己写一个方法,在 if 里面试试。

自己定义一个类,里面一个方法用于条件判断用。

public class DynamicSql1Model {
    public boolean getMySelfMethod(){
        return true;
    }
}

该类作为一个属性放入到 model 中  仅仅贴出部分代码

public class DynamicSqlModel {
    private int id;
    private String username;
    private String password;
    private Date createDate;
    private List<String> list;
    private Map<String,Object> mapParam;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> DynamicSql1Model dynamicSql1Model;</pre>

xml 中引用该 model 的方法

 <if test="dynamicSql1Model.getMySelfMethod()">

开始测试

DynamicSqlModel user = new DynamicSqlModel();
        user.setUsername("xiaofang");
        user.setPassword("123456");
        user.setCreateDate(new Date());
        DynamicSql1Model dynamicSqlModel = new DynamicSql1Model();
        user.setDynamicSql1Model(dynamicSqlModel);
        dao.selectUseIf(user);

现在返回结果
现在方法的返回值为 true 
==>  Preparing: select * from t_user where 1=1 and username=? 
==> Parameters: xiaofang(String)
方法返回值修改为 false

==>  Preparing: select * from t_user where 1=1 
==> Parameters: 
可以看出完全可以使用自定义的方法进行 if 条件控制。通过该特性可以干一些特俗业务的事情。自己体会。

本篇说的主要是 if 条件判断动态控制 sql。可以看出有弊端。因为 if 条件不满足的时候 sql 会变成

select * from t_user where  所以我在条件后面加了个 1=1 但是这是不符合逻辑的。下节介绍 where 以及其他标签用于动态 sql。