mybatis查询修改mysql的json字段

前言:

mysql5.7版本之后支持json字段类型,推荐mysql8版本,适用于属性不确定的个性化字段,比如:
身份信息{“职业”,“学生”,“兴趣”:“打乒乓球”,“特长”:“跳高,书法”};
图片信息{“日期”:“2023-12-12 22:12”,“尺寸”:“215*720”,“拍摄地”:“xxx”,“作者”:“xxx”};
标签信息:[“历史”,“军事”,“都市”,“科幻”]。
这部分信息每条数据差异较大,固定字段会大量冗余,使用json有较高的扩展性和自由度

JSON对象除了支持字符串、整型、日期类型,JSON 内嵌的字段也支持数组类型

sql语句中对json的新增和查询

新增

CREATE TABLE UserLogin (
    userId BIGINT NOT NULL,
    loginInfo JSON,
    PRIMARY KEY(userId)
);
SET @a = '
{
   "cellphone" : "1",
   "wxchat" : "码农",
   "77" : "1"
}';
INSERT INTO UserLogin VALUES (1,@a);

查询

字段回显
SELECT
    userId,
    JSON_UNQUOTE(JSON_EXTRACT(loginInfo,"$.cellphone")) cellphone,
    JSON_UNQUOTE(JSON_EXTRACT(loginInfo,"$.wxchat")) wxchat
FROM UserLogin;
等效于

SELECT 
    userId,
    loginInfo->>"$.cellphone" cellphone,
    loginInfo->>"$.wxchat" wxchat
FROM UserLogin;
条件筛选
CREATE TABLE `photos ` (
  `id` bigint NOT NULL COMMENT '文件在云端的唯一标识ID',
  `tags` json DEFAULT NULL COMMENT '标签数组',
  `info` json DEFAULT NULL COMMENT '照片生成信息json',
  PRIMARY KEY (`id`) USING BTREE
);

-- json字段属性筛选,查询拍摄地是重庆的
SELECT * FROM photos WHERE JSON_EXTRACT(info, '$.拍摄地') = '重庆';

-- 查询标签包含10的图片
SELECT * FROM photos WHERE 10 MEMBER OF(tags->"$");

-- 查询标签包含2和10的图片
SELECT * FROM photos WHERE JSON_CONTAINS(tags->"$", '[2,10]');
-- 或者
SELECT * FROM photos WHERE JSON_OVERLAPS(tags->"$", '[2,10]');

JSON_CONTAINS 返回json数组是否包含指定的数组
JSON_OVERLAPS 返回json数组是否与指定的数组有交集

建立索引

相当于增加虚拟列

-- 为json中的手机号建立索引
ALTER TABLE UserLogin ADD COLUMN cellphone VARCHAR(255) AS (loginInfo->>"$.cellphone");
ALTER TABLE UserLogin ADD UNIQUE INDEX idx_cellphone(cellphone);

-- 也可以在建表时创建索引

CREATE TABLE UserLogin (
    userId BIGINT,
    loginInfo JSON,
    cellphone VARCHAR(255) AS (loginInfo->>"$.cellphone"),
    PRIMARY KEY(userId),
    UNIQUE KEY uk_idx_cellphone(cellphone)
);

-- 使用索引的虚拟列查询
SELECT  *  FROM UserLogin WHERE cellphone = '11'

mybatis自定义转换

mysql与java实体常见的字段映射不需要我们特别指定,json字段因为其灵活性,需要单独指定。
我这里列举出上例中photos表的json与java实体的map和list的转换,
如果你的java实体中json字段映射的其他类型直接在示例转换上修改类型即可。
(也可以用泛型写通用转换)

json == > Map<String,Object>

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

public class CustomJsonTypeHandler implements TypeHandler<Map<String, Object>> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void setParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType) throws SQLException {
        try {
            String json = objectMapper.writeValueAsString(parameter);
            ps.setString(i, json);
        } catch (JsonProcessingException e) {
            throw new SQLException("Error converting Map to JSON");
        }
    }

    @Override
    public Map<String, Object> getResult(ResultSet rs, String columnName) throws SQLException {
        String json = rs.getString(columnName);
        return fromJson(json);
    }

    @Override
    public Map<String, Object> getResult(ResultSet rs, int columnIndex) throws SQLException {
        String json = rs.getString(columnIndex);
        return fromJson(json);
    }

    @Override
    public Map<String, Object> getResult(CallableStatement cs, int columnIndex) throws SQLException {
        String json = cs.getString(columnIndex);
        return fromJson(json);
    }

    private Map<String, Object> fromJson(String json) throws SQLException {
        if (json == null || json.isEmpty()) {
            return null;
        }
        try {
            return objectMapper.readValue(json, Map.class);
        } catch (JsonProcessingException e) {
            throw new SQLException("Error converting JSON to Map");
        }
    }
}

json == > List

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class JsonArrayTypeHandler implements TypeHandler<List<String>> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void setParameter(PreparedStatement ps, int i, List<String> parameter, JdbcType jdbcType) throws SQLException {
        try {
            String json = objectMapper.writeValueAsString(parameter);
            ps.setString(i, json);
        } catch (JsonProcessingException e) {
            throw new SQLException("Error converting Integer[] to JSON");
        }
    }

    @Override
    public List<String> getResult(ResultSet rs, String columnName) throws SQLException {
        String json = rs.getString(columnName);
        return fromJson(json);
    }

    @Override
    public List<String> getResult(ResultSet rs, int columnIndex) throws SQLException {
        String json = rs.getString(columnIndex);
        return fromJson(json);
    }

    @Override
    public List<String> getResult(CallableStatement cs, int columnIndex) throws SQLException {
        String json = cs.getString(columnIndex);
        return fromJson(json);
    }

    private List<String> fromJson(String json) throws SQLException {
        if (json == null || json.isEmpty()) {
            return null;
        }
        try {
            return objectMapper.readValue(json, List.class);
        } catch (JsonProcessingException e) {
            throw new SQLException("Error converting JSON to Integer[]");
        }
    }
}

mapper.xml中的输入与输出

输入

	-- 新增信息
  	<insert id="insertPhotos" parameterType="Photos">
        insert into photos
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="tags != null">tags,</if>
            <if test="info != null">info,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="tags != null">#{tags, typeHandler=com.ruoyi.web.core.config.JsonArrayTypeHandler},</if>
            <if test="info != null">#{info,typeHandler=com.ruoyi.web.core.config.CustomJsonTypeHandler},</if>
        </trim>
    </insert>

	-- 修改信息
    <update id="updatePhotos" parameterType="Photos">
        update photos
        <trim prefix="SET" suffixOverrides=",">
            <if test="info  != null">info = #{info,typeHandler=com.ruoyi.web.core.config.CustomJsonTypeHandler},</if>
            <if test="tags != null">tags = #{tags,typeHandler=com.ruoyi.web.core.config.JsonArrayTypeHandler},</if>
        </trim>
        where id= #{id}
    </update>

输出

    <resultMap type="Photos" id="PhotosResult">
        <result property="id" column="id"/>
        <result property="tags" column="tags" javaType="java.util.List" typeHandler="com.ruoyi.web.core.config.JsonArrayTypeHandler"/>
        <result property="info" column="tags" javaType="java.util.List" typeHandler="com.ruoyi.web.core.config.CustomJsonTypeHandler"/>
    </resultMap>

    <select id="PhotosList" parameterType="Photos" resultMap="PhotosResult">
        <include refid="selectPhotosVo"/>
        <where>
            <if test="id != null ">and id = #{id}</if>
            <if test="tags != null ">
                and JSON_CONTAINS(tags->"$",
                #{tags, typeHandler=com.ruoyi.web.core.config.JsonArrayTypeHandler}
                )
            </if>
        </where>
    </select>

部分json查询语法参考文章:https://mp.weixin.qq.com/s?src=11&timestamp=1706855643&ver=5055&signature=xH0-aTP0U1liqYAzXaSvvqAqto5UUiPrJY5P1-qizaGNTpmSTV7ZY7qkqyMujiUWM8dhenWhjDLecMoMgeXBwMYffwSSbLKe2UrQ308ZaDBUaVJ4ku29U0X05XZh&new=1

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[1\]中提供了一些关于MySQL中使用JSON字段进行查询的示例代码。其中,可以使用json_extract函数来查询JSON字段中的特定值,使用json_contains函数来判断JSON字段是否包含某个值。另外,还可以使用JSON_REPLACE函数来更新JSON字段的值。需要注意的是,如果字段不存在,则返回null;如果JSON字段中的值为null,则返回字符串"null"。在引用\[2\]中也提供了一些使用对象类型和数组类型进行查询的示例代码。例如,可以使用JSON_UNQUOTE和JSON_EXTRACT函数来查询对象类型的字段,使用JSON_CONTAINS函数来判断数组类型的字段是否包含某个值。在引用\[3\]中提供了一个JsonVo对象的示例,可以使用该对象来映射JSON字段的结构。综上所述,可以根据具体的需求使用MySQL提供的相关函数和对象来进行JSON字段查询操作。 #### 引用[.reference_title] - *1* [mysql 查询json字段](https://blog.csdn.net/zk_jy520/article/details/126028017)[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^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [mybatis mysql json 字段查询操作](https://blog.csdn.net/weixin_44604118/article/details/128252131)[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^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [MySQL JSON字段使用](https://blog.csdn.net/nics1993/article/details/125597245)[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^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

占星安啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值