SpringBoot与MybatisPlus3.X整合之字段类型处理器(八)

  • pom.xml

    
            
                org.springframework.boot
                spring-boot-starter
            
            
                com.baomidou
                mybatis-plus-boot-starter
                3.2.0
            
            
                org.projectlombok
                lombok
            
            
            
                p6spy
                p6spy
                3.8.0
            
            
                com.h2database
                h2
                runtime
            
                com.alibaba
                fastjson
                1.2.49
                test
            
            
            
                com.fasterxml.jackson.core
                jackson-databind
                2.10.0
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
        

     

  • 配置类

    @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 currencyList;
    61 62 }
    63

     

  • mapper

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

     1 DELETE FROM user;
     2  3 INSERT INTO user (id, name, age, email, wallet, other_info) VALUES
     4 (1, 'Jone', 18, '[email protected]', '{
     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, '[email protected]', '{
    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

     

你可能感兴趣的:(SpringBoot与MybatisPlus3.X整合之字段类型处理器(八))