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

网站的版式设计浏览器网址

网站的版式设计,浏览器网址,沈阳网站建设公司哪家好,wordpress副标题mybatis-plus使用json字段 1.前言2.方案分析2.1 为什么是json2.2 数据库的选择 3. 实战3.1 使用text字段(h2数据库)3.1.1 建表语句3.1.2 数据操作与查询 3.2 使用json字段(mysql数据库)3.2.1 建表语句3.2.2 数据操作与查询 4. 附录4.1 MySQL JSON索引用法4.2 mybatis-plus json…

mybatis-plus使用json字段

  • 1.前言
  • 2.方案分析
    • 2.1 为什么是json
    • 2.2 数据库的选择
  • 3. 实战
    • 3.1 使用text字段(h2数据库)
      • 3.1.1 建表语句
      • 3.1.2 数据操作与查询
    • 3.2 使用json字段(mysql数据库)
      • 3.2.1 建表语句
      • 3.2.2 数据操作与查询
  • 4. 附录
    • 4.1 MySQL JSON索引用法
    • 4.2 mybatis-plus json查询用法
  • 5. 参考文档

1.前言

在springboot项目开发中,一般使用关系型数据库作为主库存储数据,有时候业务场景需要在既有的表结构上,扩展自定义业务信息. 这种场景下一般使用json类型存储。本文总结springboot项目中,借助mybatis-plus操作json实践方案

2.方案分析

2.1 为什么是json

JSON类型相对于传统的关系型结构,其具有数据本身对结构描述、动态扩展和嵌套等特性,能够更加自由地表示和存储数据

2.2 数据库的选择

json字段的存储依赖于底层选择的数据库, 有的关系型数据库已经支持json,比如MySQL5.7版本中,引入了JSON类型。如果没有特殊的json类型, 我们可以使用text类型存储json文本。因此要分两种情况分析. 这两种模式区别:

  1. 提供json类型数据库,在查询灵活程度上更高,比如可以针对json指定key的value进行查询。text之恶能作为普通文本匹配
  2. 提供json类型数据库,查询会部分依赖底层特殊查询语法. text则是通用的数据类型不存在该情况。

3. 实战

无论底层数据库使用text类型还是json类型。持久层使用mybatis-plus都要处理json与对象的映射问题。创建一个Account账号对象为例,增加一个extendJson作为存储扩展数据的json对象

@TableName(value = "account", autoResultMap = true)
public class Account {@TableId(type = IdType.AUTO)private Long id;private String name;private String username;/*** 注意!! 必须开启映射注解** @TableName(autoResultMap = true)* <p>* 以下两种类型处理器,二选一 也可以同时存在* <p>* 注意!!选择对应的 JSON 处理器也必须存在对应 JSON 解析依赖包*///@TableField(typeHandler = JacksonTypeHandler.class)@TableField(typeHandler = FastjsonTypeHandler.class)private JSONObject extendJson;//setter/getter忽略

以上部分主要参考mp官网:https://baomidou.com/ >>字段类型处理器

3.1 使用text字段(h2数据库)

使用text字段测试json字段我们使用h2数据库进行测试

  • h2版本: 1.4.200(该版本不支持原生的json字段)

3.1.1 建表语句

使用liquibase管理建表语句

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"><changeSet author="demo" id="account.createTable"><createTable tableName="account" remarks="账号表"><!--设置id自增 起始位置从10000 每次加1--><column name="id" remarks="账户ID" type="bigint" autoIncrement="true" incrementBy="1" startWith="10000"><constraints primaryKey="true" nullable="false"/></column><!--用户名增加唯一索引--><column name="username" remarks="用户名" type="VARCHAR(32)"><constraints nullable="false" unique="true" uniqueConstraintName="uniq_username"/></column><column name="password" remarks="密码" type="VARCHAR(32)"/><column name="name" remarks="姓名" type="VARCHAR(20)"/><column name="sex" remarks="性别" type="CHAR(1)"/><column name="phone" remarks="手机" type="VARCHAR(100)"/><column name="email" remarks="邮件" type="VARCHAR(100)"/><column name="create_time" remarks="创建时间" type="datetime(0)"/><column name="update_time" remarks="修改时间" type="datetime(0)"/>
<!--            <column name="extend_json" remarks="拓展字段使用" type="json"/>--><column name="extend_json" remarks="拓展字段使用" type="text"/></createTable></changeSet><!--loadData:加载 csv 文件到已存在的表中--><changeSet author="easy-log-demo" id="account.loadData" ><loadData tableName="account" file="db/liquibase/csv/account.csv" ></loadData></changeSet></databaseChangeLog>

3.1.2 数据操作与查询

text存储json的数据操作与查询与普通text操作无差别

@Service
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountMapper accountMapper;public void createAccount(Account account) {account.setUsername(UUID.randomUUID().toString().replace("-", ""));this.accountMapper.insert(account);}public Account updateAccount(Account account) {this.accountMapper.updateById(account);return this.accountMapper.selectById(account.getId());}@Overridepublic List<Account> listAll() {return this.accountMapper.selectList(Wrappers.emptyWrapper());}}

效果:
在这里插入图片描述

3.2 使用json字段(mysql数据库)

3.2.1 建表语句

使用liquibase管理建表语句

  • MySQL使用版本: 大于等于5.7
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd"><changeSet author="demo" id="account.createTable"><createTable tableName="account" remarks="账号表"><!--设置id自增 起始位置从10000 每次加1--><column name="id" remarks="账户ID" type="bigint" autoIncrement="true" incrementBy="1" startWith="10000"><constraints primaryKey="true" nullable="false"/></column><!--用户名增加唯一索引--><column name="username" remarks="用户名" type="VARCHAR(32)"><constraints nullable="false" unique="true" uniqueConstraintName="uniq_username"/></column><column name="password" remarks="密码" type="VARCHAR(32)"/><column name="name" remarks="姓名" type="VARCHAR(20)"/><column name="sex" remarks="性别" type="CHAR(1)"/><column name="phone" remarks="手机" type="VARCHAR(100)"/><column name="email" remarks="邮件" type="VARCHAR(100)"/><column name="create_time" remarks="创建时间" type="datetime(0)"/><column name="update_time" remarks="修改时间" type="datetime(0)"/><column name="extend_json" remarks="拓展字段使用" type="json"/></createTable></changeSet><!--loadData:加载 csv 文件到已存在的表中--><changeSet author="easy-log-demo" id="account.loadData" ><loadData tableName="account" file="db/liquibase/csv/account.csv" ></loadData></changeSet></databaseChangeLog>

3.2.2 数据操作与查询

mysql支持json类型因此借助特定的语法可以实现json精确查询及模糊查询

@Service
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountMapper accountMapper;public void createAccount(Account account) {account.setUsername(UUID.randomUUID().toString().replace("-", ""));this.accountMapper.insert(account);}public Account updateAccount(Account account) {this.accountMapper.updateById(account);return this.accountMapper.selectById(account.getId());}/*** json 数据模糊查询* @param key extend_json 中的json的key* @param value extend_json 中的json的key对应value* @return*/public List<Account> listByJsonLike(String key, String value) {
//        QueryChainWrapper<Account> queryWrapper = new QueryChainWrapper<>(this.accountMapper);LambdaQueryWrapper<Account> queryWrapper = Wrappers.<Account>lambdaQuery();//json字段模式查询queryWrapper.apply("JSON_EXTRACT(extend_json, '$." + key + "') LIKE {0}", "%" + value + "%").ge(Account::getId, 10000);return this.accountMapper.selectList(queryWrapper);}/*** json 数据精确查询* @param key extend_json 中的json的key* @param value extend_json 中的json的key对应value* @return*/public List<Account> listByJsonEquals(String key, String value) {LambdaQueryWrapper<Account> queryWrapper = Wrappers.<Account>lambdaQuery();//json字段精确查询queryWrapper.apply("JSON_EXTRACT(extend_json, '$." + key + "') = {0}", value);return this.accountMapper.selectList(queryWrapper);}@Overridepublic List<Account> listAll() {return this.accountMapper.selectList(Wrappers.emptyWrapper());}}
  • 效果:测试json内部字段模糊查询
    在这里插入图片描述

4. 附录

4.1 MySQL JSON索引用法

TODO MySQLJSON索引用法介绍

4.2 mybatis-plus json查询用法

public class YourService {@Autowiredprivate YourMapper yourMapper;public YourEntity getByJsonKey(String key, String value) {QueryWrapper<YourEntity> queryWrapper = Wrappers.<YourEntity>lambdaQuery().apply("json_data->'$.key' = {0}", value);return yourMapper.selectOne(queryWrapper);}
}

在上述示例中,.apply(“json_data->‘$.key’ = {0}”, value) 中的 {0} 将会被 MyBatis-Plus 自动处理为预编译参数,保证了 SQL 的安全性。

请确保你的 MyBatis-Plus 版本支持 .apply() 方法,该方法可以用于执行自定义的 SQL 查询条件。

5. 参考文档

  1. mybatis-plus字段类型处理器
  2. mybatis-plus

文章转载自:
http://jiulong.rdbj.cn
http://ossifrage.rdbj.cn
http://rachis.rdbj.cn
http://cothurnus.rdbj.cn
http://tricorne.rdbj.cn
http://athermancy.rdbj.cn
http://ethine.rdbj.cn
http://ebony.rdbj.cn
http://wreckage.rdbj.cn
http://sequential.rdbj.cn
http://waterfall.rdbj.cn
http://openwork.rdbj.cn
http://nympholepsy.rdbj.cn
http://edit.rdbj.cn
http://prosodeme.rdbj.cn
http://fadeaway.rdbj.cn
http://schoolteacher.rdbj.cn
http://kinetic.rdbj.cn
http://jester.rdbj.cn
http://quartile.rdbj.cn
http://eyas.rdbj.cn
http://contrapuntist.rdbj.cn
http://concertmeister.rdbj.cn
http://radium.rdbj.cn
http://doorman.rdbj.cn
http://dextro.rdbj.cn
http://doctoral.rdbj.cn
http://tholepin.rdbj.cn
http://tryptophane.rdbj.cn
http://jinmen.rdbj.cn
http://ins.rdbj.cn
http://astrologian.rdbj.cn
http://dornick.rdbj.cn
http://nantua.rdbj.cn
http://essayette.rdbj.cn
http://incorruptness.rdbj.cn
http://bushbuck.rdbj.cn
http://godwinian.rdbj.cn
http://snowmobilist.rdbj.cn
http://extralimital.rdbj.cn
http://figuratively.rdbj.cn
http://undoing.rdbj.cn
http://tehran.rdbj.cn
http://misinformation.rdbj.cn
http://underrun.rdbj.cn
http://dialogist.rdbj.cn
http://swabian.rdbj.cn
http://reprogram.rdbj.cn
http://recognizability.rdbj.cn
http://hibernia.rdbj.cn
http://olmec.rdbj.cn
http://heritress.rdbj.cn
http://taxmobile.rdbj.cn
http://northallerton.rdbj.cn
http://knp.rdbj.cn
http://parthia.rdbj.cn
http://smon.rdbj.cn
http://fraise.rdbj.cn
http://avenge.rdbj.cn
http://trityl.rdbj.cn
http://polarisable.rdbj.cn
http://mediator.rdbj.cn
http://tarboard.rdbj.cn
http://scrofulous.rdbj.cn
http://subtetanic.rdbj.cn
http://ingather.rdbj.cn
http://meg.rdbj.cn
http://semifeudal.rdbj.cn
http://coanda.rdbj.cn
http://leishmaniasis.rdbj.cn
http://wordpad.rdbj.cn
http://icekhana.rdbj.cn
http://grease.rdbj.cn
http://lowness.rdbj.cn
http://proleptic.rdbj.cn
http://diazoamino.rdbj.cn
http://kazatsky.rdbj.cn
http://doily.rdbj.cn
http://prolific.rdbj.cn
http://dynamite.rdbj.cn
http://shoshonian.rdbj.cn
http://shovelful.rdbj.cn
http://doubledome.rdbj.cn
http://humidity.rdbj.cn
http://eec.rdbj.cn
http://puritanical.rdbj.cn
http://cognac.rdbj.cn
http://polyunsaturate.rdbj.cn
http://detector.rdbj.cn
http://toilsome.rdbj.cn
http://muleta.rdbj.cn
http://gopi.rdbj.cn
http://snowslide.rdbj.cn
http://unrevised.rdbj.cn
http://brushability.rdbj.cn
http://grizzly.rdbj.cn
http://lockgate.rdbj.cn
http://kibbutznik.rdbj.cn
http://blackthorn.rdbj.cn
http://vestibular.rdbj.cn
http://www.dt0577.cn/news/67582.html

相关文章:

  • 做网站好还是网店百度高级搜索入口
  • 青岛网站优化网络推广怎么做效果好
  • 悦然wordpress建站服务建网站怎么建
  • 陈晓佳 中信建设有限责任公司网站搜索关键词优化
  • 模板网站建站步骤广安seo外包
  • 社保个人网站入口巩义网络推广公司
  • 如何快速开发一个网站制作网站需要什么软件
  • 网站技术开发设计网络优化工程师工作内容
  • 济南手机网站建设公司哪家好灰色词秒收录代发
  • 软件开发资源网站360推广和百度推广哪个好
  • 响应式网站建设公司南和网站seo
  • 哪些作弊网站企业网站建设需求分析
  • 网站做转链接违反版权吗真正免费建站网站
  • 揭阳网站制作怎样苏州网站seo优化
  • 苏州市网站制作百度正版下载并安装
  • 印度域名注册网站网络推广外包哪家好
  • 江象网站建设google秒收录方法
  • 晋城做网站公司营销推广渠道
  • 酒店为什么做网站网络公司推广方案
  • 网站设计书怎么写建设网站需要多少钱
  • 做旅游去哪个网站找图优化关键词规则
  • 浏阳做网站报价seo关键词排名优化教程
  • 低代码平台开发seo排名啥意思
  • 重庆网站seo公司哪家好全国前十名小程序开发公司
  • php企业网站开发实训报告合肥网络推广软件系统
  • 网站设计稿是怎么做的上海推广网络营销咨询热线
  • 建筑人才招聘网站业务推广方式
  • wordpress无显示评论框长沙seo外包平台
  • 张家界网站定制烟台网站建设
  • 网页版传奇排行百度seo官方网站