欢迎光临
感受代码之美

Spring Boot将Mybatis返回结果转为驼峰的三种实现方式

我们通常获取Mybatis返回的数据结果时想要将字段以驼峰的形式返回,比如ORGAN_NAME数据库字段,然后以organName的形式返回,具体实现方式有以下三种:

一、使用Mybatis内置配置

具体yml文件配置项:

mybatis:
  mapper-locations: classpath:mapping/**/*.xml
  type-aliases-package: com.inspur.inspection.portrait
  configuration:
      map-underscore-to-camel-case: true

响应的我们需要定义一个实体类来接收结果,例如:

<select id="officerSex" parameterType="java.util.HashMap" resultType="com.portrail.city.model.JwOfficerSex">
    SELECT T.* FROM jw_officer_sex T
    WHERE T.AREA_CODE = #{areaCode}
    AND T.YEAR_NUM IN
    <foreach collection="yearArr" index="index" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
    ORDER BY T.YEAR_NUM DESC
</select>

注意: 如果你的返回结果不是实体类Java Bean,而是Map,此种方式无法实现,参考第二种。

二、定义一个resultMap

通常我们可以在Mybatis的mapping文件里这样定义:

<resultMap id="struViewResultMap" type="com.inspur.common.bsp.organ.view.StruView">
    <result property="struId" column="STRU_ID"/>
    <result property="parentId" column="PARENT_ID"/>
    <result property="organId" column="ORGAN_ID"/>
</resultMap>

<resultMap id="extOrganMap" type="java.util.HashMap">
    <result property="organId" column="TARGET_ID"/>
    <result property="organName" column="ORGAN_NAME"/>
    <result property="organCode" column="ORGAN_CODE"/>
    <result property="organType" column="ORGAN_TYPE"/>
    <result property="type" column="TYPE"/>
    <result property="srcId" column="SRC_ID"/>
    <result property="struType" column="STRU_TYPE"/>
    <result property="struId" column="STRU_ID"/>
</resultMap>

其中,resultMap标签的type属性可以为Java Class类名,或Mybatis内建类型别名。

这种方式,对普通Java Bean和Map等类型都适用。

使用方式参考如下代码:

 <select id="getStruExtAndOrgan" parameterType="java.util.HashMap" resultMap="extOrganMap">
    SELECT
    ext.ID,
    ext.TYPE,
    ext.SRC_ID,
    ext.TARGET_ID,
    ext.STRU_TYPE,
    ext.STRU_ID,
    O.ORGAN_NAME,
    O.ORGAN_CODE,
    O.ORGAN_TYPE
    FROM
    pub_stru_ext ext
    LEFT JOIN pub_organ O ON ext.TARGET_ID = O.ORGAN_ID
</select>

三、重实现ObjectWrapperFactory接口

代码1:CustomWrapper.java

package com.portrail.config;

import org.apache.commons.text.CaseUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.MapWrapper;

import java.util.Map;


/**
 * @author 四个空格:https://www.4spaces.org
 */
public class CustomWrapper extends MapWrapper {
    public CustomWrapper(MetaObject metaObject, Map<String, Object> map) {
        super(metaObject, map);
    }


    @Override
    public String findProperty(String name, boolean useCamelCaseMapping) {
        if (useCamelCaseMapping) {
            return CaseUtils.toCamelCase(name, false, new char[]{'_'});
        }
        return name;
    }
}

代码2:MapWrapperFactory.java

package com.portrail.config;

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.wrapper.ObjectWrapper;
import org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;

import java.util.Map;

/**
 * @author 四个空格:https://www.4spaces.org
 */
public class MapWrapperFactory 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:MybatisConfig.java

package com.portrail.config;

import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 四个空格:https://www.4spaces.org
 */
@Configuration
public class MybatisConfig {
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer() {
        return new ConfigurationCustomizer() {
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                configuration.setObjectWrapperFactory(new MapWrapperFactory());
            }
        };
    }
}

这种方式,默认对按照方式一设置了开启驼峰的配置后的Java BeanMap都返回驼峰。

参考文章:

  1. Mybatis-mapUnderscoreToCamelCase
  2. Mybatis返回结果转驼峰配置项
  3. https://mybatis.org/mybatis-3/sqlmap-xml.html#Result_Maps
  4. mybatis在resultMap的type使用Map来接收值,避免建立DTO实体类
  5. SpringBoot+Mybatis,返回Map的时候,将Map内的Key转换为驼峰的命名表达式
赞(0)
未经允许禁止转载:四个空格 » Spring Boot将Mybatis返回结果转为驼峰的三种实现方式

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址