17、通用枚举类型处理(二)

问题

要是我们一个项目特别大,有几十个甚至是上百个枚举呢,那怎么办,难道我要一个个在typeHandler里面去加,加一百几十行?!个人觉得非常麻烦,影响我们的开发速度,so,能不能实现我们想要的像扫描下枚举的所在的包目录就可以注册的枚举做自动转换呢?具体怎么实现?

typeHandler的注册流程

具体过程参考15

切入点

Cosnfiguration对象得到typeHandlerRegistry管理器,入手的方法就是typeHandlerElement方法,在里面我们添加多一个包扫描功能,为万能转换处理器注册所指定的包下面所有的枚举

方案一

SqlSessionFactoryBean

由于我们项目使用了Spring, 是用Spring集成的Mybatis(废话,大家都是这么干的)。Spring通过SqlSessionFactoryBean来初始化启动Mybatis。 因此,我们应该在它身上下手,然而,一切并不是那么顺利。

查看了一下SqlSessionFactoryBean的源码,发现SqlSessionFactoryBean并没有任何地方可以让我们切入, 进而来调用TypeHandlerRegistry进行注册我们的枚举。 更令人蛋疼的是其所有属性全是private, 这下不仅AOP切入不行,连通过继承偷懒都不行了。
只有老老实实的重写一遍SqlSessionFactoryBean的代码了

DefaultSqlSessionFactoryBean

  • DefaultSqlSessionFactoryBean继承SqlSessionFactoryBean。
  • 将SqlSessionFactoryBean中的代码全部copy到DefaultSqlSessionFactoryBean。
  • 调用以下方法。
package com.lf.sqlSessionFactoryBean;

import com.lf.BaseEnum;
import com.lf.typehandle.BaseEnumTypeHandle;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Set;

/**
 * Created by LF on 2017/6/12.
 */
public class DefaultSqlSessionFactoryBean extends SqlSessionFactoryBean {
    protected SqlSessionFactory buildSqlSessionFactory() throws IOException {

        Configuration configuration;

        XMLConfigBuilder xmlConfigBuilder = null;
        if (this.configuration != null) {
            configuration = this.configuration;
            if (configuration.getVariables() == null) {
                configuration.setVariables(this.configurationProperties);
            } else if (this.configurationProperties != null) {
                configuration.getVariables().putAll(this.configurationProperties);
            }
        } else if (this.configLocation != null) {
            xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
            configuration = xmlConfigBuilder.getConfiguration();
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
            }
            configuration = new Configuration();
            if (this.configurationProperties != null) {
                configuration.setVariables(this.configurationProperties);
            }
        }

        if (this.objectFactory != null) {
            configuration.setObjectFactory(this.objectFactory);
        }

        if (this.objectWrapperFactory != null) {
            configuration.setObjectWrapperFactory(this.objectWrapperFactory);
        }

        if (this.vfs != null) {
            configuration.setVfsImpl(this.vfs);
        }

        if (hasLength(this.typeAliasesPackage)) {
            String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
                    ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
            for (String packageToScan : typeAliasPackageArray) {
                configuration.getTypeAliasRegistry().registerAliases(packageToScan,
                        typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
                }
            }
        }

        if (!isEmpty(this.typeAliases)) {
            for (Class<?> typeAlias : this.typeAliases) {
                configuration.getTypeAliasRegistry().registerAlias(typeAlias);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Registered type alias: '" + typeAlias + "'");
                }
            }
        }

        if (!isEmpty(this.plugins)) {
            for (Interceptor plugin : this.plugins) {
                configuration.addInterceptor(plugin);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Registered plugin: '" + plugin + "'");
                }
            }
        }

        if (hasLength(this.typeHandlersPackage)) {
            String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
                    ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
            for (String packageToScan : typeHandlersPackageArray) {
                configuration.getTypeHandlerRegistry().register(packageToScan);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers");
                }
            }
        }

        if (!isEmpty(this.typeHandlers)) {
            for (TypeHandler<?> typeHandler : this.typeHandlers) {
                configuration.getTypeHandlerRegistry().register(typeHandler);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Registered type handler: '" + typeHandler + "'");
                }
            }
        }

        if (this.databaseIdProvider != null) {//fix #64 set databaseId before parse mapper xmls
            try {
                configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
            } catch (SQLException e) {
                throw new NestedIOException("Failed getting a databaseId", e);
            }
        }

        if (this.cache != null) {
            configuration.addCache(this.cache);
        }

        if (xmlConfigBuilder != null) {
            try {
                xmlConfigBuilder.parse();

                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
                }
            } catch (Exception ex) {
                throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
            } finally {
                ErrorContext.instance().reset();
            }
        }

        if (this.transactionFactory == null) {
            this.transactionFactory = new SpringManagedTransactionFactory();
        }

        configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));

        if (!isEmpty(this.mapperLocations)) {
            for (Resource mapperLocation : this.mapperLocations) {
                if (mapperLocation == null) {
                    continue;
                }

                try {
                    XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
                            configuration, mapperLocation.toString(), configuration.getSqlFragments());
                    TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
                    //重点
                    regist(typeHandlerRegistry, BaseEnumTypeHandle.class, "com.lf,dist");
                    xmlMapperBuilder.parse();
                } catch (Exception e) {
                    throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
                } finally {
                    ErrorContext.instance().reset();
                }

                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
                }
            }
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
            }
        }

        return this.sqlSessionFactoryBuilder.build(configuration);
    }

    private void regist(TypeHandlerRegistry typeHandlerRegistry, Class<?> typeHandlerClass, String... packageNames) {
        ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
        resolverUtil.findImplementations(BaseEnum.class, packageNames);
        Set<Class<? extends Class<?>>> mTypes = resolverUtil.getClasses();
        for (Class<?> javaTypeClass : mTypes) {
            typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
        }
    }

}

方案二

没有使用spring的时候
其他的配置和正常使用的时候一样

package com.lf;

import com.lf.typehandle.BaseEnumTypeHandle;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.type.TypeHandlerRegistry;

import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

public class App {
    public static void main(String[] args) throws IOException {
        String resouce = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resouce);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        TypeHandlerRegistry typeHandlerRegistry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry();
        regist(typeHandlerRegistry, BaseEnumTypeHandle.class,"com.lf.dist");
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Object one = sqlSession.selectOne("com.lf.dao.BlogPostMapper.selectByPrimaryKey", "1");
        System.err.println(one);
        sqlSession.commit();
    }
    private static void regist(TypeHandlerRegistry typeHandlerRegistry, Class<?> typeHandlerClass, String... packageNames) {
        ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
        resolverUtil.findImplementations(BaseEnum.class, packageNames);
        Set<Class<? extends Class<?>>> mTypes = resolverUtil.getClasses();
        for (Class<?> javaTypeClass : mTypes) {
            typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
        }
    }
}

方案三

修改mybatis的源码
修改注解MappedTypes,添加扫描包的功能


package org.apache.ibatis.type;

import java.lang.annotation.*;

/**
 * @author Eduardo Macarron
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MappedTypes {

    Class<?>[] value() default {};

    String[] basePackage() default {};
}

修改register源码


    // Only handler type

    public void register(Class<?> typeHandlerClass) {
        boolean mappedTypeFound = false;
        MappedTypes mappedTypes = typeHandlerClass.getAnnotation(MappedTypes.class);
        if (mappedTypes != null) {
            for(String basePackage:mappedTypes.basePackage()){
                ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
                resolverUtil.find(new ResolverUtil.IsA(BaseEnum.class), basePackage);
                Set<Class<? extends Class<?>>> mTypes = resolverUtil.getClasses();
                for (Class<?> javaTypeClass : mTypes) {
                    register(javaTypeClass, typeHandlerClass);
                    mappedTypeFound = true;
                }
            }

            for (Class<?> javaTypeClass : mappedTypes.value()) {
                register(javaTypeClass, typeHandlerClass);
                mappedTypeFound = true;
            }
        }
        if (!mappedTypeFound) {
            register(getInstance(null, typeHandlerClass));
        }
    }

配置

    <typeAliases>
        <package name="com.lf.entity"/>
    </typeAliases>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** 1. 功能:枚举操作类,枚举类型创建,转换 * 2. 作者:杨磊 * 3. 创建日期:2008-1-30 * 4. 最后修改日期:2008-11-10 **/ using System; using System.Collections.Generic; using System.Text; using System.Collections; /// /// 枚举操作类,枚举类型创建,转换 /// public class EnumHelper { #region 通过字符串获取枚举成员实例 /// /// 通过字符串获取枚举成员实例 /// /// 枚举名,比如Enum1 /// 枚举成员的常量名或常量值, /// 范例:Enum1枚举有两个成员A=0,B=1,则传入"A"或"0"获取 Enum1.A 枚举类型 public static T GetInstance(string member) { return CommFun.ConvertTo(Enum.Parse(typeof(T), member, true)); } #endregion #region 获取枚举成员名称和成员值的键值对集合 /// /// 获取枚举成员名称和成员值的键值对集合 /// /// 枚举名,比如Enum1 public static Hashtable GetMemberKeyValue() { //创建哈希表 Hashtable ht = new Hashtable(); //获取枚举所有成员名称 string[] memberNames = GetMemberNames(); //遍历枚举成员 foreach (string memberName in memberNames) { ht.Add(memberName, GetMemberValue(memberName)); } //返回哈希表 return ht; } #endregion #region 获取枚举所有成员名称 /// /// 获取枚举所有成员名称 /// /// 枚举名,比如Enum1 public static string[] GetMemberNames() { return Enum.GetNames(typeof(T)); } #endregion #region 获取枚举成员的名称 /// /// 获取枚举成员的名称 /// /// 枚举名,比如Enum1 /// 枚举成员实例或成员值, /// 范例:Enum1枚举有两个成员A=0,B=1,则传入Enum1.A或0,获取成员名称"A" public static string GetMemberName(object member) { //转成基础类型的成员值 Type underlyingType = GetUnderlyingType(typeof(T)); object memberValue = CommFun.ConvertTo(member,underlyingType); //获取枚举成员的名称 return Enum.GetName(typeof(T), memberValue); } #endregion #re

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值