1. 程式人生 > 其它 >mybatis踩坑記錄之if標籤

mybatis踩坑記錄之if標籤

技術標籤:mybatis踩坑記錄mybatis

mybatis踩坑記錄之if標籤

參考網址:

https://mp.weixin.qq.com/s?__biz=MzAwMjk5Mjk3Mw==&mid=2247494777&idx=2&sn=324b2cbcba0f8aeef12d42cee4a9c382&chksm=9ac3525badb4db4d27bf2a97506404087712293d4d7308a01c186b1f65bd5a3aa7092a8921c6&mpshare=1&scene=23&srcid=0107OvSYpJ4U87Qon2fSjRp4&sharer_sharetime=1610370060848&sharer_shareid=9d1e76e919cc0b2f3ca23ed1f5ef67a8#rd

https://blog.csdn.net/weixin_44144211/article/details/109347443?ops_request_misc=&request_id=&biz_id=102&utm_term=mybaits%25E4%25B8%25ADif%25E6%25A0%2587%25E7%25AD%25BE&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-1-109347443.first_rank_v2_pc_rank_v29

帖子參考

<!-- 以下為錯誤寫法,會拋NumberFormatException異常 -->
<if test="username == 'U'"> <!-- 正確寫法如下兩種 --> <if test="username == 'U'.toString()"> <if test='username == "U"'>

測試

1.準備測試環境

springboot整合mybatis-plus

說明:springboot整合mybatis也是可以的

  • sql建表指令碼
CREATE TABLE `demo` (
`id` int(11) DEFAULT NULL,
`name` varchar
(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8

測試資料

MariaDB [test]> select * from demo;
+------+-------+
| id   | name  |
+------+-------+
|    1 | root  |
|    1 | NULL  |
| NULL | root  |
|    2 | root2 |
+------+-------+
4 rows in set (0.000 sec)
  • 實體類
@Data
@TableName("demo")
public class Demo {
    private Integer id;
    private String name;
}

  • mapper介面
package com.shaoming.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shaoming.model.entity.Demo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Auther: shaoming
 * @Date: 2021/1/11 15:37
 * @Description:
 */
public interface DemoMapper extends BaseMapper<Demo> {
    /**
     * 測試<if test></if>的踩坑記錄
     * @param name
     * @return
     */
    List<Demo> selectByName(@Param("name") String name);
}

  • mapper.xml
<?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">
<mapper namespace="com.shaoming.mapper.DemoMapper">

    <select id="selectByName" resultType="com.shaoming.model.entity.Demo">
        select * from demo
        <where>
            <if  test='name=="all"'>
              1=1
            </if>
        </where>
    </select>
</mapper>

2.需求分析(需求一)

  • 具體需求

如果查詢name的值為admin 那麼就查詢所有

  • 測試方法
package com.shaoming;

import com.shaoming.mapper.DemoMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @Auther: shaoming
 * @Date: 2021/1/11 15:38
 * @Description:
 */
@SpringBootTest
public class DaoTest {
    @Autowired
    private DemoMapper demoMapper;
  
    @Test
    public void testMybatisOfIf(){
        demoMapper.selectByName("all").forEach(System.out::println);
    }
}
    /**
     * 控制檯列印
     Demo(id=1, name=root)
     Demo(id=1, name=null)
     Demo(id=null, name=root)
     Demo(id=2, name=root2)
     */

  • mapper.xml的三種寫法

第一種


    <select id="selectByName" resultType="com.shaoming.model.entity.Demo">
        select * from demo
        <where>
            <if  test='name=="all"'>
              1=1
            </if>
        </where>
    </select>
</mapper>

第二種

    <select id="selectByName" resultType="com.shaoming.model.entity.Demo">
        select * from demo
        <where>
            <if  test="name='all'.toString()">
              1=1
            </if>
        </where>
    </select>

第三種

去除1=1, 1=1表示where條件恆成立,就是查詢所有,如果去掉也是可以的

    <select id="selectByName" resultType="com.shaoming.model.entity.Demo">
        select * from demo
        <where>
            <if  test="name='all'.toString()">
           
            </if>
        </where>
    </select>

3.演變的需求分析(需求二)

如果name的值不是all,那麼就按照實際條件進行查詢

第一種寫法

 <select id="selectByName" resultType="com.shaoming.model.entity.Demo">
        select * from demo
        <where>
            <if  test="name!='all'">
               name = #{name}
            </if>
        </where>
    </select>

第二種寫法

  <select id="selectByName" resultType="com.shaoming.model.entity.Demo">
        select * from demo
        <where>
            <if  test="name!='all'.toString()">
               name = #{name}
            </if>
        </where>
    </select>

測試方法

//查詢name為root的值記錄 
@Test
    public void testMybatisOfIf(){
        demoMapper.selectByName("root").forEach(System.out::println);
    }
    /**
     * 控制檯列印
     Demo(id=1, name=root)
     Demo(id=null, name=root)
     */

4.需求三

臨時通過比較需求二和需求三,突然想起來test後面比較的如果是Integer

特別注意

if標籤把空字串和數字0判斷成了相等

<if test="a==''">
</if>
上面程式碼等價於
<if test="a==0">
</if>

如果比較的是其他數字

等於

 <select id="selectById" resultType="com.shaoming.model.entity.Demo">
        select * from demo
        <where>
          <if test="id==1">
              id is null
          </if>
        </where>
    </select>
 @Test
    public void testid(){
        demoMapper.selectById(1).forEach(System.out::println);
    }
        /**
控制檯列印
Demo(id=null, name=root)
*/

不等於

<select id="selectById" resultType="com.shaoming.model.entity.Demo">
        select * from demo
        <where>
          <if test="id!=1">
              id is null
          </if>
        </where>
    </select>
 @Test
    public void testid(){
        demoMapper.selectById(2).forEach(System.out::println);
    }
/**
控制檯列印
Demo(id=null, name=root)
*/

5.總結

1.比較需求一和需求二

<!-- if標籤字元竄等於的書寫方式 --> 
<if  test="name='all'.toString()">
<!-- if標籤字元竄不等於的書寫方式 -->    
 <if  test="name!='all'">

mybaits中if標籤等於和不等於書寫規則是不同,建議使用上面這種方法書寫,容易記憶

2.需求三是數值型別

注意當和0比較時候的特殊性