zoukankan      html  css  js  c++  java
  • SpringBoot与MybatisPlus3.X整合之字段类型处理器(八)

    • pom.xml

      <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter</artifactId>
              </dependency>
              <dependency>
                  <groupId>com.baomidou</groupId>
                  <artifactId>mybatis-plus-boot-starter</artifactId>
                  <version>3.2.0</version>
              </dependency>
              <dependency>
                  <groupId>org.projectlombok</groupId>
                  <artifactId>lombok</artifactId>
              </dependency>
              <!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
              <dependency>
                  <groupId>p6spy</groupId>
                  <artifactId>p6spy</artifactId>
                  <version>3.8.0</version>
              </dependency>
              <dependency>
                  <groupId>com.h2database</groupId>
                  <artifactId>h2</artifactId>
                  <scope>runtime</scope>
              </dependency><dependency>
                  <groupId>com.alibaba</groupId>
                  <artifactId>fastjson</artifactId>
                  <version>1.2.49</version>
                  <scope>test</scope>
              </dependency>
              <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
              <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-databind</artifactId>
                  <version>2.10.0</version>
              </dependency><!-- for testing -->
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
              </dependency>
          </dependencies>
    • 配置类

      @Configuration
      @MapperScan("com.mp.typehandler.mapper")
      public class MybatisPlusConfig {
      ​
      }
    • application.yml

      spring:
        datasource:
          driver-class-name: com.p6spy.engine.spy.P6SpyDriver
          url: jdbc:p6spy:h2:tcp://192.168.180.115:19200/~/mem/test
          username: root
          password: test
    • 实体类

       1 @Data
       2 public class Currency {
       3     /**
       4      * 类型: 人民币 RMB , 美元 USD
       5      */
       6     private String type;
       7     /**
       8      * 金额
       9      */
      10     private Double amount;
      11 12 }
      13 @Data
      14 public class OtherInfo {
      15     /**
      16      * 性别
      17      */
      18     private String sex;
      19     /**
      20      * 居住城市
      21      */
      22     private String city;
      23 24 }
      25 26 Data
      27 @Accessors(chain = true)
      28 @TableName(value="user",autoResultMap = true)
      29 public class User {
      30     private Long id;
      31     private String name;
      32     private Integer age;
      33     private String email;
      34 35     /**
      36      * 注意!! 必须开启映射注解
      37      *
      38      * @TableName(autoResultMap = true)
      39      *
      40      * 以下两种类型处理器,二选一 也可以同时存在
      41      *
      42      * 注意!!选择对应的 JSON 处理器也必须存在对应依赖包
      43      */
      44     @TableField(typeHandler = JacksonTypeHandler.class)
      45     private Wallet wallet;
      46 47     @TableField(typeHandler = FastjsonTypeHandler.class)
      48     private OtherInfo otherInfo;
      49 50 }
      51 @Data
      52 public class Wallet {
      53     /**
      54      * 名称
      55      */
      56     private String name;
      57     /**
      58      * 各种货币
      59      */
      60     private List<Currency> currencyList;
      61 62 }
      63
    • mapper

      public interface UserMapper extends BaseMapper<User> {
      ​
      }
    • 数据库脚本

       1 DELETE FROM user;
       2  3 INSERT INTO user (id, name, age, email, wallet, other_info) VALUES
       4 (1, 'Jone', 18, 'test1@baomidou.com', '{
       5     "name": "支付宝钱包",
       6     "currencyList": [{
       7         "type": "USD",
       8         "amount": 999.19
       9     },{
      10         "type": "RMB",
      11         "amount": 1000.19
      12     }]
      13 }', '{
      14         "sex": "男",
      15         "city": "南昌"
      16 }'),
      17 (2, 'Jack', 20, 'test2@baomidou.com', '{
      18     "name": "微信钱包",
      19     "currencyList": [{
      20         "type": "USD",
      21         "amount": 888.18
      22     },{
      23         "type": "RMB",
      24         "amount": 1000.18
      25     }]
      26 }', '{
      27         "sex": "男",
      28         "city": "青岛"
      29 }');
      30 31 DROP TABLE IF EXISTS user;
      32 33 CREATE TABLE user
      34 (
      35     id BIGINT(20) NOT NULL COMMENT '主键ID',
      36     name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
      37     age INT(11) NULL DEFAULT NULL COMMENT '年龄',
      38     email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
      39     wallet VARCHAR(3000) NULL DEFAULT NULL COMMENT '钱包',
      40     other_info VARCHAR(3000) NULL DEFAULT NULL COMMENT '其他信息',
      41     PRIMARY KEY (id)
      42 );
      43
    • 测试类

      @SpringBootTest
      public class TypehandlerApplicationTests {
      ​
      ​
          @Autowired
          private UserMapper userMapper;
      ​
         
          @Test
          public void test() {
              User Jone = userMapper.selectById(1);
              System.err.println(Jone.getName());
              System.err.println(Jone.getOtherInfo().getSex());
      ​
              User Jack = userMapper.selectById(1);
              System.err.println(Jack.getName());
          }
      ​
      }
    • 测试结果

       Consume Time:7 ms 2019-10-30 20:13:03
       Execute SQL:SELECT * FROM user WHERE id=1 
      ​
      Jone
      男
       Consume Time:0 ms 2019-10-30 20:13:03
       Execute SQL:SELECT * FROM user WHERE id=1 
      ​
      Jone
  • 相关阅读:
    cf1043C. Smallest Word(贪心)
    洛谷P1081 开车旅行(倍增)
    NOI.AC NOIP2018 全国热身赛 第四场
    cf444E. DZY Loves Planting(并查集)
    NOI.AC NOIP模拟赛R3解题报告
    中国第一计算机编程高手横瓜的天才求职之路异常艰辛,天妒奇才呀
    C语言全局未初始化数据段分析
    js问题总结
    ios7新增基础类库以及OC新特性
    jquery.post用法
  • 原文地址:https://www.cnblogs.com/dalianpai/p/11767455.html
Copyright © 2011-2022 走看看