Mybatis中的if test判断

mysql数据库

dao:

List<String> test(@Param("id") Integer id, @Param("port") String port);

sql:

	<select id="test" resultType="string">
		SELECT
		'1'
		FROM
		WORKER_NODE
		WHERE 1 = 1
		<if test="port == null">  <!-- StringL类型  双引号 is null   -->
			and 2 = 2
		</if>
		<if test='port == null'>  <!-- StringL类型 单引号 is null   -->
			and 3 = 3
		</if>
		<if test="port == ''">  <!-- StringL类型 里面单引号 is 空字符   -->
			and 4 = 4
		</if>
		<if test="port == ''.toString()">  <!-- StringL类型 里面单引号 is 空字符转字符串   -->
			and 5 = 5
		</if>
		<if test='port == ""'>  <!-- StringL类型 里面双引号 is 空字符   -->
			and 6 = 6
		</if>
		<if test="port == '   '">  <!-- StringL类型 里面单引号 is 3个空字符   -->
			and 41 = 41
		</if>
		<if test="port == '   '.toString()">  <!-- StringL类型 里面单引号 is 3个空字符转字符串   -->
			and 51 = 51
		</if>
		<if test='port == "   "'>  <!-- StringL类型 里面双引号 is 3个空字符   -->
			and 61 = 61
		</if>
		<if test="port != null">  <!-- StringL类型 双引号 非null   -->
			and 7 = 7
		</if>
		<if test='port != null'>  <!-- StringL类型 单引号 非null   -->
			and 71 = 71
		</if>
		<if test="port != null and port != ''">  <!-- StringL类型 外面双引号 里面单引号 非空   -->
			and 8 = 8
		</if>
		<if test='port != null and port != ""'>  <!-- StringL类型 外面单引号 里面双引号 非空   -->
			and 9 = 9
		</if>
		<if test="id != null">  <!--  int类型  双引号 非null   -->
			and 10 = 10
		</if>
		<if test='id != null'>  <!--  int类型  单引号 非null   -->
			and 11 = 11
		</if>
		<if test='id == null'>  <!--  int类型  单引号 is null   -->
			and 12 = 12
		</if>
		<if test="id == null">  <!--  int类型  双引号 is null   -->
			and 13 = 13
		</if>
		<if test='id = !null and id == 0'>  <!--  int类型  单引号 非null等于0  -->
			and 14 = 14
		</if>
		<if test="id != null and id != 0">  <!--  int类型  外面双引号 非null非0   -->
			and 15 = 15
		</if>
		<if test="id != null and id != ''">  <!-- int类型 外面双引号 里面单引号  非null非空   -->
			and 16 = 16
		</if>
		<if test="id != null and id != '  '">  <!-- int类型 外面双引号 里面单引号  非null非两个空格   -->
			and 17 = 17
		</if>
		<if test="id != null and id != '   '">  <!-- int类型 外面双引号 里面单引号  非null非三个空格   -->
			and 18 = 18
		</if>
		<if test='id != null and id != ""'>  <!-- int类型 外面单引号 里面双引号  非null非空   -->
			and 19 = 19
		</if>

	</select>

测试代码:

    @PostMapping("/test2")
    public void test2() {
        workerNodeDAO.test(3, "HHH");
        System.out.println("---0---");
        workerNodeDAO.test(null, null);
        System.out.println("---1---");
        workerNodeDAO.test(0, "");
        System.out.println("---2---");
        workerNodeDAO.test(1, " ");
        System.out.println("---3---");
        workerNodeDAO.test(1, "   "); // 三个空格
        System.out.println("---4---");
        workerNodeDAO.test(2, "a");
        System.out.println("---5---");
        workerNodeDAO.test(11, "aaa");
        System.out.println("---6---");
        workerNodeDAO.test(11, "1");
        System.out.println("---7---");
        workerNodeDAO.test(11, "123");
        System.out.println("---8---");
    }

日志打印:

SELECT ‘1’ FROM WORKER_NODE WHERE 1 = 1 and 7 = 7 and 71 = 71 and 8 = 8 and 9 = 9 and 10 = 10 and 11 = 11
—0—
SELECT ‘1’ FROM WORKER_NODE WHERE 1 = 1 and 2 = 2 and 3 = 3 and 12 = 12 and 13 = 13
—1—
SELECT ‘1’ FROM WORKER_NODE WHERE 1 = 1 and 4 = 4 and 5 = 5 and 6 = 6 and 7 = 7 and 71 = 71 and 10 = 10 and 11 = 11 and 14 = 14 and 15 = 15 and 16 = 16 and 17 = 17 and 18 = 18 and 19 = 19
—2—
SELECT ‘1’ FROM WORKER_NODE WHERE 1 = 1 and 7 = 7 and 71 = 71 and 8 = 8 and 9 = 9 and 10 = 10 and 11 = 11
—3—
SELECT ‘1’ FROM WORKER_NODE WHERE 1 = 1 and 41 = 41 and 51 = 51 and 61 = 61 and 7 = 7 and 71 = 71 and 8 = 8 and 9 = 9 and 10 = 10 and 11 = 11
—4—
SELECT ‘1’ FROM WORKER_NODE WHERE 1 = 1 and 7 = 7 and 71 = 71 and 8 = 8 and 9 = 9 and 10 = 10 and 11 = 11
—5—
SELECT ‘1’ FROM WORKER_NODE WHERE 1 = 1 and 7 = 7 and 71 = 71 and 8 = 8 and 9 = 9 and 10 = 10 and 11 = 11
—6—
SELECT ‘1’ FROM WORKER_NODE WHERE 1 = 1 and 7 = 7 and 71 = 71 and 8 = 8 and 9 = 9 and 10 = 10 and 11 = 11
—7—
SELECT ‘1’ FROM WORKER_NODE WHERE 1 = 1 and 7 = 7 and 71 = 71 and 8 = 8 and 9 = 9 and 10 = 10 and 11 = 11
—8—

上面很多结果跟oracle不一样,
比如id = 0 时,16 = 16是进不来的;id = 1时,15 = 15 及以下是能进来的
在oracle中<if test=“id != null and id != ‘’”>
id = 0 时进不来,id = 1时可以进来,
在oracle中<if test="id != null and id != ‘’ ">,这种多个空字符的,不管几个空字符,非0数字都可以进来,
因为不管几个空字符,都当成0处理

大家在使用if test时应注意以下规范:

  • id != ‘’ 单引号是char,如果我们的入参是空字符串就是"",两种类型不同,用来比较是不同的(但在上面的结果中却是相同,可能跟mybatis的版本有关)
  • id != “” 双引号才是String类型的判空
  • id != ‘’.toString() 可以将char类型转为String类型
  • int类型<if test=“id != null”> 即可,不需要加!=''或!“”,容易造成错误

判空标准写法:

  • String类型:<if test=‘port != null and port != “”’> 1 = 1 </if>
  • int类型:<if test=‘port != null’> 1 = 1 </if>
MyBatisif test判断中,可以使用两种方式来判断一个List集合是否为空。一种是通过判断List对象是否为null,并且判断List的size是否为0来确定。示例代码如下: ```xml <if test="list!=null and list.size()!=0"></if> ``` 另一种方式是通过判断List对象是否为null,并且直接判断List对象在标签内部是否为空来确定。示例代码如下: ```xml <if test="list!=null"> <!-- 在这里写逻辑判断 --> </if> ``` 所以,根据你提供的引用内容,如果需要在MyBatis的mapper.xml中判断一个list集合是否为空,可以使用上述的两种方式之一来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Mybatis if test 判断 list不为 null 并且判断集合大小不为0](https://blog.csdn.net/zl18603543572/article/details/125006233)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [mybatis if test判断 list不为空](https://blog.csdn.net/weixin_44476553/article/details/114333172)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值