后端动态列设计与实现三部曲,这是最后一步,使用java语言,结合mybatis-plus神技操作json字段。
简单介绍下mybatis-plus,大厂中mybatis使用的非常多,而mybatis-plus是基于mybatis做了扩展,进一步增强,在不影响数据存储的情况下,简化操作方式。有兴趣的朋友可以去官网了解:https://www.baomidou.com/
1、架构图
2、功能
3、表结构
DROPTABLE IF EXISTS user;CREATETABLE user( id BIGINT(20)NOTNULL COMMENT '主键ID', name VARCHAR(30)NULL DEFAULT NULL COMMENT '姓名', age INT(11)NULL DEFAULT NULL COMMENT '年龄', email VARCHAR(50)NULL DEFAULT NULL COMMENT '邮箱', wallet VARCHAR(3000)NULL DEFAULT NULL COMMENT '钱包', other_info VARCHAR(3000)NULL DEFAULT NULL COMMENT '其他信息', PRIMARY KEY (id));INSERTINTO user (id, name, age, email, wallet, other_info)VALUES(1,'Jone',18,'test1@baomidou.com','{"name": "支付宝钱包","currencyList": [{"type": "USD","amount": 999.19},{"type": "RMB","amount": 1000.19}]}','{"sex": "男","city": "南昌"}'),(2,'Jack',20,'test2@baomidou.com','{"name": "微信钱包","currencyList": [{"type": "USD","amount": 888.18},{"type": "RMB","amount": 1000.18}]}','{"sex": "男","city": "青岛"}');
4、实体定义
packagecom.baomidou.mybatisplus.samples.typehandler.entity; importcom.baomidou.mybatisplus.annotation.TableField; importcom.baomidou.mybatisplus.annotation.TableName; importcom.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler; importcom.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; importlombok.Data; importlombok.experimental.Accessors; /**用户实体对应表 user* @author hubin* @since 2018-08-11*/chain=true) (autoResultMap=true) (publicclassUser { privateLongid; privateStringname; privateIntegerage; privateStringemail; /*** 注意!!必须开启映射注解** @TableName(autoResultMap = true)** 以下两种类型处理器,二选一 也可以同时存在** 注意!!选择对应的 JSON 处理器也必须存在对应依赖包*/typeHandler=JacksonTypeHandler.class) (privateWalletwallet; typeHandler=FastjsonTypeHandler.class) (privateOtherInfootherInfo; }
packagecom.baomidou.mybatisplus.samples.typehandler.entity; importjava.util.List; importlombok.Data; /*** 钱包*/publicclassWallet { /*** 名称*/privateStringname; /*** 各种货币*/privateList<Currency>currencyList; }
packagecom.baomidou.mybatisplus.samples.typehandler.entity; importlombok.Data; /*** 货币*/publicclassCurrency { /*** 类型: 人民币 RMB , 美元 USD*/privateStringtype; /*** 金额*/privateDoubleamount; }
packagecom.baomidou.mybatisplus.samples.typehandler.entity; importlombok.Data; /*** 其他信息*/publicclassOtherInfo { /*** 性别*/privateStringsex; /*** 居住城市*/privateStringcity; }
5、Dao定义
packagecom.baomidou.mybatisplus.samples.typehandler.mapper; importcom.baomidou.mybatisplus.core.mapper.BaseMapper; importcom.baomidou.mybatisplus.samples.typehandler.entity.User; /*** <p>* MP 支持不需要 UserMapper.xml 这个模块演示内置 CRUD 咱们就不要 XML 部分了* </p>** @author hubin* @since 2018-08-11*/publicinterfaceUserMapperextendsBaseMapper<User> { }
6、类型转换器
packagecom.baomidou.mybatisplus.samples.typehandler.config; importcom.baomidou.mybatisplus.extension.handlers.GsonTypeHandler; importcom.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; importcom.fasterxml.jackson.databind.ObjectMapper; importcom.google.gson.Gson; importorg.springframework.boot.CommandLineRunner; importorg.springframework.stereotype.Component; /*** @author miemie* @since 2019-11-28*/publicclassMpJsonConfigimplementsCommandLineRunner { /*** 可以set进去自己的*/publicvoidrun(String... args) throwsException { JacksonTypeHandler.setObjectMapper(newObjectMapper()); GsonTypeHandler.setGson(newGson()); } }
7、测试
packagecom.baomidou.mybatisplus.samples.typehandler; importjavax.annotation.Resource; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.boot.test.context.SpringBootTest; importorg.springframework.test.context.junit4.SpringRunner; importcom.baomidou.mybatisplus.samples.typehandler.entity.User; importcom.baomidou.mybatisplus.samples.typehandler.mapper.UserMapper; /*** <p>* 内置 类型处理器 演示* </p>** @author hubin* @since 2018-08-11*/SpringRunner.class) (publicclassSampleTest { privateUserMapperuserMapper; /*** 自定义类型处理器演示参考 mybatis-plus-sample-deluxe 模块*/publicvoidtest() { UserJone=userMapper.selectById(1); System.out.println(Jone); System.err.println(Jone.getName()); UserJack=userMapper.selectById(1); System.err.println(Jack.getName()); } }
结果如下:
2020-11-2212:46:58.164INFO3168--- [ main] c.b.m.samples.typehandler.SampleTest : StartedSampleTestin4.125seconds (JVMrunningfor5.707) 2020-11-2212:46:58.477DEBUG3168--- [ main] c.b.m.s.t.mapper.UserMapper.selectById : ==>Preparing: SELECTid,name,age,email,wallet,other_infoFROMuserWHEREid=?2020-11-2212:46:58.509DEBUG3168--- [ main] c.b.m.s.t.mapper.UserMapper.selectById : ==>Parameters: 1(Integer) 2020-11-2212:46:58.713DEBUG3168--- [ main] c.b.m.s.t.mapper.UserMapper.selectById : <==Total: 1User(id=1, name=Jone, age=18, email=test1 .com, wallet=Wallet(name=支付宝钱包, currencyList=[Currency(type=USD, amount=999.19), Currency(type=RMB, amount=1000.19)]), otherInfo=OtherInfo(sex=男, city=南昌)) 2020-11-2212:46:58.715DEBUG3168--- [ main] c.b.m.s.t.mapper.UserMapper.selectById : ==>Preparing: SELECTid,name,age,email,wallet,other_infoFROMuserWHEREid=?2020-11-2212:46:58.715DEBUG3168--- [ main] c.b.m.s.t.mapper.UserMapper.selectById : ==>Parameters: 2(Integer) 2020-11-2212:46:58.716DEBUG3168--- [ main] c.b.m.s.t.mapper.UserMapper.selectById : <==Total: 1User(id=2, name=Jack, age=20, email=test2 .com, wallet=Wallet(name=微信钱包, currencyList=[Currency(type=USD, amount=888.18), Currency(type=RMB, amount=1000.18)]), otherInfo=OtherInfo(sex=男, city=青岛)) 2020-11-2212:46:58.736INFO3168--- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1-Shutdowninitiated... 2020-11-2212:46:58.747INFO3168--- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1-Shutdowncompleted.
总结,使用mybatis-plus可以提高应用程序操作数据库的效率,让开发人员专注于业务逻辑实现。在使用mybatis-plus操作json字段的要点主要有:
1、在需要处理的字段上使用@TableField(typeHandler = JacksonTypeHandler.class),同时实体开启@TableName(autoResultMap = true)
2、注册工具类,MpJsonConfig.
mybatis-plus 还有许多很好用的功能,感兴趣的朋友可以自己去官网上下看,也可能从github或者gitee上拉取最新代码,了解它的工作原理,结合自己的业务做一些增强。欢迎留言交流。