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>