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

欧美在线网站设计教程宣威做网站推广的公司

欧美在线网站设计教程,宣威做网站推广的公司,thinkphp网站开发服务器,怎么切页面做网站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 为什么是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查询用法 5. 参考文档 1.前言 在springboot项目开发中,一般使用关系型数据库作为主库存储数据,有时候业务场景需要在既有的表结构上,扩展自定义业务信息. 这种场景下一般使用json类型存储。本文总结springboot项目中,借助mybatis-plus操作json实践方案 2.方案分析 2.1 为什么是json JSON类型相对于传统的关系型结构其具有数据本身对结构描述、动态扩展和嵌套等特性能够更加自由地表示和存储数据 2.2 数据库的选择 json字段的存储依赖于底层选择的数据库, 有的关系型数据库已经支持json,比如MySQL5.7版本中引入了JSON类型。如果没有特殊的json类型, 我们可以使用text类型存储json文本。因此要分两种情况分析. 这两种模式区别: 提供json类型数据库,在查询灵活程度上更高,比如可以针对json指定key的value进行查询。text之恶能作为普通文本匹配提供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 version1.1 encodingUTF-8 standaloneno? databaseChangeLog xmlnshttp://www.liquibase.org/xml/ns/dbchangelog xmlns:exthttp://www.liquibase.org/xml/ns/dbchangelog-ext xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://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.xsdchangeSet authordemo idaccount.createTablecreateTable tableNameaccount remarks账号表!--设置id自增 起始位置从10000 每次加1--column nameid remarks账户ID typebigint autoIncrementtrue incrementBy1 startWith10000constraints primaryKeytrue nullablefalse//column!--用户名增加唯一索引--column nameusername remarks用户名 typeVARCHAR(32)constraints nullablefalse uniquetrue uniqueConstraintNameuniq_username//columncolumn namepassword remarks密码 typeVARCHAR(32)/column namename remarks姓名 typeVARCHAR(20)/column namesex remarks性别 typeCHAR(1)/column namephone remarks手机 typeVARCHAR(100)/column nameemail remarks邮件 typeVARCHAR(100)/column namecreate_time remarks创建时间 typedatetime(0)/column nameupdate_time remarks修改时间 typedatetime(0)/ !-- column nameextend_json remarks拓展字段使用 typejson/--column nameextend_json remarks拓展字段使用 typetext//createTable/changeSet!--loadData:加载 csv 文件到已存在的表中--changeSet authoreasy-log-demo idaccount.loadData loadData tableNameaccount filedb/liquibase/csv/account.csv /loadData/changeSet/databaseChangeLog3.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 ListAccount listAll() {return this.accountMapper.selectList(Wrappers.emptyWrapper());}} 效果: 3.2 使用json字段(mysql数据库) 3.2.1 建表语句 使用liquibase管理建表语句 MySQL使用版本: 大于等于5.7 ?xml version1.1 encodingUTF-8 standaloneno? databaseChangeLog xmlnshttp://www.liquibase.org/xml/ns/dbchangelog xmlns:exthttp://www.liquibase.org/xml/ns/dbchangelog-ext xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://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.xsdchangeSet authordemo idaccount.createTablecreateTable tableNameaccount remarks账号表!--设置id自增 起始位置从10000 每次加1--column nameid remarks账户ID typebigint autoIncrementtrue incrementBy1 startWith10000constraints primaryKeytrue nullablefalse//column!--用户名增加唯一索引--column nameusername remarks用户名 typeVARCHAR(32)constraints nullablefalse uniquetrue uniqueConstraintNameuniq_username//columncolumn namepassword remarks密码 typeVARCHAR(32)/column namename remarks姓名 typeVARCHAR(20)/column namesex remarks性别 typeCHAR(1)/column namephone remarks手机 typeVARCHAR(100)/column nameemail remarks邮件 typeVARCHAR(100)/column namecreate_time remarks创建时间 typedatetime(0)/column nameupdate_time remarks修改时间 typedatetime(0)/column nameextend_json remarks拓展字段使用 typejson//createTable/changeSet!--loadData:加载 csv 文件到已存在的表中--changeSet authoreasy-log-demo idaccount.loadData loadData tableNameaccount filedb/liquibase/csv/account.csv /loadData/changeSet/databaseChangeLog3.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 ListAccount listByJsonLike(String key, String value) { // QueryChainWrapperAccount queryWrapper new QueryChainWrapper(this.accountMapper);LambdaQueryWrapperAccount queryWrapper Wrappers.AccountlambdaQuery();//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 ListAccount listByJsonEquals(String key, String value) {LambdaQueryWrapperAccount queryWrapper Wrappers.AccountlambdaQuery();//json字段精确查询queryWrapper.apply(JSON_EXTRACT(extend_json, $. key ) {0}, value);return this.accountMapper.selectList(queryWrapper);}Overridepublic ListAccount 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) {QueryWrapperYourEntity queryWrapper Wrappers.YourEntitylambdaQuery().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. 参考文档 mybatis-plus字段类型处理器mybatis-plus
http://www.hkea.cn/news/14438367/

相关文章:

  • 网站如何添加百度统计怎样做php网站
  • 禹城网站建设东莞网站建设做网站
  • 做电商设计有什么好的网站推荐宜都网站建设
  • 龙岩公司做网站百度个人中心登录
  • 单位网站建设自查报告范文全自动三次元网站建设
  • 石家庄自己怎么做网站啊精品课程网站设计与实现开题报告
  • 仿站软件网站开发logo
  • 广东制冷设备网站建设费用只做男士衬衫的网站
  • 搭网站可以用自己电脑做服务器吗北京东城区 网站建设
  • 网站开发系统是什么网站设计前景
  • 信息技术 网站建设教案wordpress滑动相册
  • 加快公司网站建设武陵天下网站建设
  • 天猫商城网站风格南宁手机建站公司
  • 网站建设培训 店wordpress nginx伪静态
  • 自考在线做试卷的网站注册局官网
  • 淘宝不能发布网站源码做商品学做淘宝店的网站
  • 龙岗网站制作设计费用
  • 南宁希噢网站开发工作室聊城找个人做网站
  • 中企动力做网站怎么样北京 工业网站建设公司
  • 校园图书回收网站建设展示型网站制作公司
  • 合肥昱天建设有限公司网站常州网站价格
  • 建设网站的流程图乐清做网站的公司有哪些
  • 买了一个域名怎么做网站wordpress无法创建配置文件
  • 网站总体结构天津网站建设有哪些
  • 2019做网站seo行不行淘宝流量网站
  • 网站建设需要考虑的问题网站建设费用自建
  • 企业网站排名软件度智能优化公司官网建设方案
  • 深圳企业建设网站网站建设的 文献综述
  • wordpress文章时间轴seo优化是什么职业
  • 福州长乐网站建设瑞丽网站建设