接受前端参数存入数据库

  实体类:

  @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,另外一个是我们自己创建

json_contains_path
这个函数用来判断是否有键名的,我的看法是这个函数基本用不到,数据库结果基本都是提前设计好的,不需要判断。第一个参数判断的目标,第二个参数是one或者all,第三个参数指定的键名,以后的参数都是键名,如果第二个参数是one,则其中一个键名存在则返回正确;如果第二个参数是all,则所有键名都存在才返回正确。

例一
select * from member where json_contains_path(info, 'one', '$[0]');

例二

select * from member where json_contains_path(info, 'one', '$[3]');


例三
select * from member where json_contains_path(info, 'one', '$.a');

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐