1.新建一个CustomWrapper.class继承MapWrapper
重写findProperty方法
package com.example.demo.sys.config;
import java.util.Map;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;
public class CustomWrapper extends MapWrapper {
public CustomWrapper(MetaObject metaObject, Map<String, Object> map) {
super(metaObject, map);
}
/**
如果要返回驼峰的话自行更改返回的名称,此处统一返回字母大写
*/
@Override
public String findProperty(String name, boolean useCamelCaseMapping) {
return name == null ? "" : name.toUpperCase();
}
}
2.新建一个CustomWrapperFactory.class实现ObjectWrapperFactory
package com.example.demo.sys.config;
import java.util.Map;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
public class CustomWrapperFactory implements ObjectWrapperFactory {
/*
此处编写对于什么情况进行包装
*/
@Override
public boolean hasWrapperFor(Object object) {
return object != null && object instanceof Map;
}
/*
此处指定包装的方法
*/
@Override
public ObjectWrapper getWrapperFor(MetaObject metaObject, Object object){
return new CustomWrapper(metaObject, (Map) object);
}
}
3.新建一个CustomWrapperAutoConfiguration.class作为SpringBoot自动配置加载
此处基于mybatis自动化配置,如果使用程序配置则直接在配置SqlSessionFactory时直接替换
package com.example.demo.sys.config;
import javax.annotation.PostConstruct;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter(value = MybatisAutoConfiguration.class)
public class CustomWrapperAutoConfiguration {
@Autowired
private SqlSessionFactory sqlSessionFactory;
@PostConstruct
public void changeWrapperFactory() {
sqlSessionFactory.getConfiguration().setObjectWrapperFactory(new CustomWrapperFactory());
}
}