接受前端参数存入数据库
实体类:
@ColumnType(typeHandler = JSONArrayHandler.class)
private JSONArray gender; //json数组
@ColumnType(typeHandler = JSONTypeHandler.class)
private JSONObject geoLocation; //json对象
mybatis mapper类:
<resultMap id="selectAudienceMap" type="com.leiting.ads.model.entity.audience.AdsGdtAudienceConfig">
<result property="userOs" column="user_os" typeHandler="com.leiting.ads.mybatis.handlers.JSONArrayHandler"/>
<result property="geoLocation" column="geo_location" typeHandler="com.leiting.ads.mybatis.handlers.JSONTypeHandler"/>
</resultMap>
<select id="selectAudienceList" resultMap="selectAudienceMap">
</select>
在数据库中存入json字符串并使用sql语句进行查找:
例一
select * from member where json_contains(info, '4');
特别注意,如果参数不是数据库中的字段的话,一定要加引号,就算是整型也得加
例二
select * from member where json_contains(json_array(1,2,3,4,5,6,7), info);

例三
select * from member where json_contains(json_array(21,31,41,51), json_array(age));
这种用法的结果和in是一样的,也跟前面我们讲json_array一样,区别在于一个是数据库本身就是array,另外一个是我们自己创建