- springboot引入mybatis-plus,创建springboot项目省略
- pom文件
-
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3.4</version></dependency> <!--自动生成代码模版--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-extension</artifactId><version>3.4.3.4</version></dependency>
- 配置文件
-
#配置mybatis配置文件 mybatis-plus:#mappersmapper-locations: classpath:mappers/*Mapper.xmltype-handlers-package: com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler
- 备注信息
- springboot使用mybatis和mybatis-plus没有什么区别,需要注意的是配置文件跟配置名:mybatis-plus
- 使用mybatis-plus的有点在于,在mybatis的基础上记性了一系列的有效封装,节约了开发时间,有这方面兴趣额同学自行搜索区别就是了
- mybatis-plus操作mysql的json字段
- mysql在5.7以后出现了json字段类型,这样省去了代码中转换的不必要性,但是mybatis目前还不能直接支持json类型,需要额外的处理,也就是自定义jdbctype类型进行处理
- mybatis-plus可以支持json类型
- 实体类类名和对应属性上增加配置
-
package com.test.entity; import com.alibaba.fastjson.JSONObject; @Data @TableName(value = "user",autoResultMap = true) public class User implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;@TableField(value="content",typeHandler = JacksonTypeHandler.class)private JSONObject content; }
- xml文件的resultMap对应的字段增加配置
-
<resultMap id="baseMap" type="com.enric.payment.bin.ConfigTicketBO"><id column="uuid" property="uuid"/><result column="station_id" property="station_id"/><result column="pc_on" property="pc_on"/><result column="machine_on" property="machine_on"/><result column="machine_auto" property="machine_auto"/><result column="ticket_pages" property="ticket_pages"/><result column="coupon_rule_volume" property="coupon_rule_volume"/><result column="coupon_over_volume" property="coupon_over_volume"/><result column="coupon_expire_time" property="coupon_expire_time"/><result column="create_time" property="create_time"/><result column="update_time" property="update_time"/><result column="create_by" property="create_by"/><result column="update_by" property="update_by"/><result column="ticket_info" property="ticket_info"typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/><result column="coupon_info" property="coupon_info"typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/></resultMap>
- xml插入或更新语句增加配置
-
<update id="saveOrUpdate">INSERT INTO config_ticket(uuid, station_id, pc_on, machine_on, machine_auto, ticket_info, coupon_info,coupon_rule_volume,coupon_expire_time, coupon_over_volume, create_time, update_time, create_by)VALUES (#{uuid}, #{station_id}, #{pc_on}, #{machine_on}, #{machine_auto},#{ticket_info,jdbcType=OTHER,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler},#{coupon_info,jdbcType=OTHER,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler},#{coupon_rule_volume},#{coupon_expire_time}, #{coupon_over_volume}, now(), now(), #{create_by})ON DUPLICATE KEY UPDATE pc_on = #{pc_on},machine_on = #{machine_on},machine_auto = #{machine_auto},ticket_info = #{ticket_info,jdbcType=OTHER,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler},coupon_info = #{coupon_info,jdbcType=OTHER,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler},coupon_rule_volume = #{coupon_rule_volume},coupon_expire_time = #{coupon_expire_time},coupon_over_volume = #{coupon_over_volume},update_time = now(),update_by = #{update_by}</update>