MyBatis对于Oracle数据库与MySQL数据库查询返回Map中大小写问题优化

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

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值