赞
踩
1、开启mybatis驼峰命名规则
- mybatis:
- configuration:
- #使用mybatis驼峰功能将达梦返回字段转小写
- map-underscore-to-camel-case: true
2、继承MapWrapper
-
- import org.apache.ibatis.reflection.MetaObject;
- import org.apache.ibatis.reflection.wrapper.MapWrapper;
-
- import java.util.Map;
-
- public class SqlFieldToLowerCase extends MapWrapper {
-
- public SqlFieldToLowerCase(MetaObject metaObject, Map<String, Object> map) {
- super(metaObject, map);
- }
-
- @Override
- public String findProperty(String name, boolean useCamelCaseMapping) {
- if (useCamelCaseMapping) {
- //查询字段转小写
- return name == null ? null : name.toLowerCase();
- }
- return name;
- }
-
-
- }

3、实现ObjectWrapperFactory
-
- import com.alibaba.fastjson.JSONObject;
- import org.apache.ibatis.reflection.MetaObject;
- import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
- import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;
- import org.apache.ibatis.session.Configuration;
- import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
- import org.springframework.context.annotation.Bean;
- import org.springframework.stereotype.Component;
-
- import java.util.Map;
-
- @Component
- public class SqlFieldToLowerCaeFactory implements ObjectWrapperFactory {
- @Override
- public boolean hasWrapperFor(Object o) {
- return o instanceof Map;
- }
-
- @Override
- public ObjectWrapper getWrapperFor(MetaObject metaObject, Object o) {
- return new SqlFieldToLowerCase(metaObject, (Map) o);
- }
-
- /**
- * 使用该方式object-wrapper-factory: com.demo.config.SqlFieldToLowerCaeFactory
- * 会报类型转换异常所以使用下面这种方式
- * @return
- */
- @Bean
- public ConfigurationCustomizer mybatisConfigurationCustomizer() {
- return new ConfigurationCustomizer() {
- @Override
- public void customize(Configuration configuration) {
-
- configuration.setObjectWrapperFactory(new SqlFieldToLowerCaeFactory());
- }
- };
- }
-
- }

注意事项:mybatis返回结果为fastjson:JSONObject类型时,使用该方式将返回字段转小写可能会在遇见达梦数据库字段类型为CLOB、TEXT等类型字段报JSON解析内存溢出
可以尝试在数据库链接上拼接clobAsString=1 或者将字段类型改为varchar
jdbc:dm://127.0.0.1:5236/DEMO?clobAsString=1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。