{key1:value1, key2:value2, ………}
{"id":1, "name":"houhou"}
[value1,value2,value3…..]
[1,2,3]
或["haha","xixi","houhou"]
[{key1:value1,key2:value2},{key3:value3,key4:value4},…….]
[{"id":1, "name":"houhou"},{"id":2, "name":"xixi"}]
CREATE TABLE `tableA(
`id` int(11),
`custom_biz_ex_info` json NULL
)
INSERT INTO tableA( `id, `custom_biz_ex_info` ) values (1,'{"fileName": "fileTest3.xml","idT":12}');
INSERT INTO tableA( `id, `custom_biz_ex_info` ) values (2,'{"idT": "13", "fileName": "fileTest1.xml"}')
INSERT INTO tableA( `id, `custom_biz_ex_info` ) values (3,'{"idT": "17", "fileName": "fileTest2.xml"}')
SELECT * FROM tableA where custom_biz_ex_info->'$.fileName' like '%file%';
SELECT * FROM tableA where json_extract(`custom_biz_ex_info`,'$.fileName') like concat('%', 'file', '%') ;
序号 | id | custom_biz_ex_info |
---|---|---|
1 | 1 | {“idT”: “13”, “fileName”: “fileTest1.xml”} |
2 | 2 | {“idT”: “17”, “fileName”: “fileTest2.xml”} |
3 | 3 | {“idT”: “22”, “fileName”: “fileTest3.xml”} |
SELECT custom_biz_ex_info->'$.fileName' FROM tableA where custom_biz_ex_info->'$.fileName' like '%file%';
SELECT json_extract(`custom_biz_ex_info`,'$.fileName') FROM `tableA` where json_extract(`custom_biz_ex_info`,'$.fileName') like '%file%';
序号 | json_extract(custom_biz_ex_info ,’$.fileName’) |
---|---|
1 | “fileTest1.xml” |
2 | “fileTest2.xml” |
3 | “fileTest3.xml” |
concat()
函数的使用CONCAT(str1,str2,…)
mysql> select concat('10'); // 10
mysql> select concat('11','22','33'); // 112233
mysql> select concat('11','22',null); // NULL
定义
${}
:直接填充,即解析出来的参数值不带单引号#{}
会预编译,即解析传过来参数带单引号示例
// 如果入参是table= user, column = id
//1.${}
select * from ${table} where ${column} =1; --> select * from user where id =1;
//2.#{}
select * from #{table} where #{column} =1; --> select * from 'user' where 'id' =1;
select * from aTable where c1=${uu}`
'or '1=1
,就会返回所有数据,即select * from aTable where c1= '' or '1=1'
。//@Param("params") 必须写params,这样在mapper层就可以使用params.keys和params.values
Page<DTO> listDTO(@Param("params") Map<String, String> customMap);
<select id="listDTO" resultType="com.zijikanwa.DTO">
select * from tableA
<where>
<if test="customMap!=null and customMap.size()>0">
<foreach collection="params.keys" item="key">
and json_extract(custom_biz_ex_info, CONCAT('$.',#{key})) like concat('%',#{params[${key}]},'%')
</foreach>
</if>
</where>
</select>
$
会有SQL注入的风险,不推荐使用。foreach属性 | 是否必选 | 说明 |
---|---|---|
item | 必选 | 循环体中的具体对象,如item.age, item.info.details具体说明:若collection属性为list或array,则item代表list或array里面的一个元素; 若collection属性为map,则item代表的是map中的value集合中的单个value |
collection | 必选 | foreach遍历的对象。1. 属性值为list或array或map。List对象默认用list代替作为键,数组对象用array代替作为键,Map对象没有默认的键。2.使用@Param(“params”)来设置键,设置keyName后,list,array将会失效。 3.如果传入参数类型为map,这个入参有注解@Param(“params”),则map的所有的key集合可以写成params.keys,所有值集合可以写成params.values。这样foreach就可以对key集合或值集合进行迭代了 |
separator | 可选 | 元素之间的分隔符。例如在in(1,2)的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误 |
open | 可选 | foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时 |
close | 可选 | foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时 |
index | 可选 | 在list和数组中,index是元素的序号;在map中,index是元素的key |
Page<DTO> listDTO(@Param("customMap") Map<String, String> customMap);
<select id="listDTO" resultType="com.zijikanwa.DTO">
select * from tableA
<where>
<if test="customMap!=null and customMap.size()>0">
<foreach collection="customMap" index="key" item="value">
and json_extract(custom_biz_ex_info, CONCAT('$.',#{key})) like concat('%',#{value},'%')
</foreach>
</if>
</where>
</select>