mybatis-plus返回map自动转驼峰配置 | 我的日常分享

mybatis-plus返回map自动转驼峰配置

mybatis-plus返回map自动转驼峰配置

问题:接口中使用mybatis-plus返回Map格式

因为要关联楼层信息,不想更改实体类,以免后面数据库改变后,重新生成代码需要重新更改实体类,非必要就不更改实体类了。所以以Map格式返回数据,但是查看接口返回数据时发现字段名与数据库一致,没有跟实体类的驼峰命名一致,如下图:

1
2
3
4
5
6
7
8
9
10
@Override
public Page<Map<String, Object>> getPage(Integer current, Integer size) {
Page<Map<String, Object>> page = bmsSiteRoomMapper.selectMapsPage(new Page<>(current, size), Wrappers.emptyWrapper());
// 关联楼层信息
page.getRecords().forEach(item -> {
BmsSiteFloor floor = bmsSiteFloorMapper.selectById((Serializable) item.get("floorId"));
item.put("floor",floor);
});
return page;
}

image-20220903005605090

解决方式

方式1:配置application.yml

1
2
3
4
5
6
7
mybatis-plus.configuration.object-wrapper-factory=com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory



mybatis-plus:
configuration:
object-wrapper-factory: com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory

配置application后发现无法启动springboot应用:

image-20220903010611400

原因:没有找到能够把类型[java.lang.String]转换为[org.apache.ibatis.reflect .wrapper. objectwrapperfactory]的转换器

看字面意思,应该是缺少对应的converter,难道mybatis没有提供这个converter吗?简直有点坑。而且springboot也不提供用反射机制来构件对象的converter?是的,springboot没有这样做。通过查资料得知springboot提供了一种扩展机制,允许你来写一个converter来完成你想要的转换工作。于是,我又写了一个converter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package cn.yuencode.admin.config;

import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@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);
}
}
}

配置好转换器后启动运行正常。

方式2:Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package cn.yuencode.admin.config;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@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);
}
}

@Bean
public ConfigurationCustomizer mybatisConfigurationCustomizer(){
return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
}

}

推荐使用方式2,不用配置文件和代码都要写。

配置成功后:

image-20220903010250904

参考资料:

  1. https://blog.csdn.net/weixin_27230891/article/details/112151598
  2. https://www.pudn.com/news/62fdb7095425817ffc60af7b.html
  3. [https://www.cnblogs.com/yuangyuan/p/16451709.html](