mybatis-plus resultType映射map 转驼峰

mybatis-plus resultType映射map 转驼峰

实体类驼峰映射

  • 实体类
/**
 * 用户表
 *
 * @author xw
 * @date 2020/10/27 11:45
 */
@Accessors(chain = true)
@Entity
@Table(name = "sys_user")
@TableName("sys_user")
@EqualsAndHashCode(callSuper = true)
@Data
@org.hibernate.annotations.Table(appliesTo = "sys_user", comment = "用户表")
public class SysUser extends BaseEntity {

    /**
     * 用户名
     */
    @Column(name = "user_name", columnDefinition = ("varchar(50) not null comment '用户账号'"))
    @NotBlank(message = "账号不能为空")
    private String userName;
}
  • mysql 表结构
    在这里插入图片描述
    mybatis-plus添加配置即可map-underscore-to-camel-case: true
  • 完整配置
# mybatis-plus config
mybatis-plus:
  configuration:
    # 是否打印sql,dev使用,pro环境不开启
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 是否开启驼峰
    map-underscore-to-camel-case: true
    # map 映射value为null的情况
    call-setters-on-nulls: true
  global-config:
    db-config:
      # 主键插入策略
      id-type: ASSIGN_ID
      # 插入策略
      insert-strategy: not_empty
      # 更新策略
      update-strategy: not_empty
      # 逻辑删除
      logic-delete-value: 1
      logic-not-delete-value: 0
  # xml路径
  mapper-locations: classpath:mapper/*Mapper.xml
  # mapper.xml文件中resultType的type或者parameterType会返回自定义entity
  type-aliases-package: com.xxxx.xxx

resultType 为map的情况key不是驼峰

  • mapper
List<Map<String, String>> getUser(@Param("startDate") String startDate, @Param("endDate") String endDate);
  • xml
<select id="getUser" resultType="java.util.Map">
SELECT
    su.user_name ,
	su.real_name 
 FROM
    sys_user sur
</select>
 
  • 实际查询key非驼峰
    在这里插入图片描述
  • 修改key 驼峰
    1. 使用MybatisMapWrapperFactory 下划线转驼峰配置类
	/**
     * map类型key转驼峰
     *
     * @return {@link ConfigurationCustomizer}
     */
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
    }

注意:配置后null值查询不出来,新增call-setters-on-nulls: true即可
在这里插入图片描述

  • 最后的效果
    在这里插入图片描述
  1. 配置添加
    可参考:https://www.jb51.net/article/200757.htm

    1. 添加object-wrapper-factory: com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory
      在这里插入图片描述
    2. 自定义Converter注入
    @Component
    @ConfigurationPropertiesBinding
    public class ObjectWrapperFactoryConverter implements Converter<String,ObjectWrapperFactory> {
      @Override 
      public ObjectWrapperFactory convert(String source) {
        try {
          return (ObjectWrapperFactory) Class.forName(source).newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
          throw new RuntimeException(e);
        }
      }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值