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);
        }
      }
    }
    
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 默认开启了驼峰命名规则,但如果你的 Mapper 接口方法名或 SQL 语句中使用下划线命名法,则需要进行配置。 可以在 MyBatis-Plus 的配置文件中配置全局的映射规则,例如: ```java @Configuration public class MybatisPlusConfig { @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory()); } /** * 自定义 Map 换器 */ public static class MybatisMapWrapperFactory extends DefaultObjectWrapperFactory { @Override public boolean hasWrapperFor(Object o) { return o != null && o.getClass() == Map.class; } @Override public ObjectWrapper getWrapperFor(MetaObject metaObject, Object o) { return super.getWrapperFor(metaObject, new MybatisMap((Map<String, Object>) o)); } } /** * 自定义 Map 实现类 */ public static class MybatisMap extends HashMap<String, Object> { public MybatisMap(Map<String, Object> map) { super(map); } @Override public Object put(String key, Object value) { return super.put(StringUtils.camelToUnderline(key), value); } } } ``` 上述代码中,我们自定义了一个 Map 实现类 `MybatisMap`,用于将 Map 中的 key(字段名)进行驼峰下划线的操作。并在 MyBatis-Plus 配置文件中配置了一个全局的配置器 `ConfigurationCustomizer`,用于将 `MybatisMap` 作为默认的 `ObjectWrapper` 实现类。 这样,我们在 Mapper 中查询的时候,就可以使用驼峰命名规则了。例如: ```java @Select("select id, user_name as userName, password from user where id = #{id}") User selectById(@Param("id") Long id); ``` 注意:上述方式只是一种实现方式,还可以通过其他方式来实现,例如使用 MyBatis-Plus 提供的 `IKeyGenerator` 接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值