当前位置: 首页 > news >正文

什么网站做玩具的比较多百度云登陆首页

什么网站做玩具的比较多,百度云登陆首页,张家界网络,公司产品展示网站源码前言: mysql5.7版本之后支持json字段类型,推荐mysql8版本,适用于属性不确定的个性化字段,比如: 身份信息{“职业”,“学生”,“兴趣”:“打乒乓球”,“特长”:“跳高,书法”}; 图片信息{“日期”:“2023-12-12 22:12”…

前言:

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);

查询

字段回显
SELECTuserId,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();@Overridepublic 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");}}@Overridepublic Map<String, Object> getResult(ResultSet rs, String columnName) throws SQLException {String json = rs.getString(columnName);return fromJson(json);}@Overridepublic Map<String, Object> getResult(ResultSet rs, int columnIndex) throws SQLException {String json = rs.getString(columnIndex);return fromJson(json);}@Overridepublic 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();@Overridepublic 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");}}@Overridepublic List<String> getResult(ResultSet rs, String columnName) throws SQLException {String json = rs.getString(columnName);return fromJson(json);}@Overridepublic List<String> getResult(ResultSet rs, int columnIndex) throws SQLException {String json = rs.getString(columnIndex);return fromJson(json);}@Overridepublic 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

http://www.hkea.cn/news/812887/

相关文章:

  • 页网站无锡网站制作推广
  • 一流的龙岗网站建设目前最靠谱的推广平台
  • 企业营销型网站费用短视频推广引流
  • 化妆品可做的团购网站有哪些seo研究中心南宁线下
  • 网站空间域名是什么做电商必备的几个软件
  • 软件公司运营是做什么的seo公司运营
  • 专业云南做网站福州短视频seo服务
  • 网站开发技术期中试题电商培训机构排名
  • 网站设计连接数据库怎么做如何进行百度推广
  • 日本网站图片做淘宝代购网络营销促销方案
  • 网站开发导航栏网站制作的费用
  • 盐城网站设计网站流量统计工具
  • 网站上如何做相关推荐郑州建网站的公司
  • 漂亮大气的装潢室内设计网站模板 单页式html5网页模板包前端优化
  • 论坛网站开发开题报告青岛百度推广多少钱
  • 文山做网站如何优化百度seo排名
  • 上海展陈设计公司有哪些成都网站seo性价比高
  • 小韩网站源码360广告投放平台
  • 网站地图的重要性短信广告投放软件
  • 搭建直播网站需要怎么做教育培训机构平台
  • 濮阳网站网站建设网络营销策划是什么
  • 做新闻网站需要什么手续河北软文搜索引擎推广公司
  • 广州网站建设联系电话seo推广的公司
  • 一起做网店一样的网站关键词歌曲免费听
  • 负责网站建设推广本周热点新闻事件
  • 快速做网站优化谷歌在线浏览入口
  • 苏州企业网站建设开发与制作2023年6月份又封城了
  • 用java做网站可以吗吉林seo刷关键词排名优化
  • 网站建设面试google广告投放技巧
  • 整形网站整站源码如何让关键词排名靠前