Search in sources :

Example 1 with MapperRegistry

use of org.apache.ibatis.binding.MapperRegistry in project citrus by Yiuman.

the class CrudHelper method getMapper.

/**
 * 根据实体以及基础Mapper接口获取Mapper
 *
 * @param entityClass     实体Class
 * @param baseMapperClass 基础Mapper基础的Class
 * @param <M>             Mapper泛型
 * @param <T>             实体泛型
 * @return 从Mybatis取出的Mapper代理对象(可能为动态字节码Mapper)
 */
@SuppressWarnings("unchecked")
public static <M extends BaseMapper<T>, T> M getMapper(Class<T> entityClass, Class<?> baseMapperClass) {
    try {
        // 多线程的使用 线程安全的SqlSessionTemplate
        SqlSessionTemplate sqlSessionTemplate = SpringUtils.getBean(SqlSessionTemplate.class);
        // 先看下Mapper与实体映射的缓存是否为空,为空则初始化已注册的实体缓存信息
        if (CollectionUtil.isEmpty(MAPPER_CACHE.keySet())) {
            // 找到Mapper注册器
            MapperRegistry mapperRegistry = sqlSessionTemplate.getConfiguration().getMapperRegistry();
            mapperRegistry.getMappers().stream().filter(mapperInterface -> {
                Class<?>[] interfaces = mapperInterface.getInterfaces();
                // 找到是CrudMapper的实现
                return ArrayUtil.isNotEmpty(interfaces) && interfaces[0].isAssignableFrom(CrudMapper.class);
            }).forEach(baseMapperInterface -> MAPPER_CACHE.put((Class<?>) TypeUtil.getTypeArgument(baseMapperInterface, 0), (Class<? extends BaseMapper<?>>) baseMapperInterface));
        }
        Class<? extends BaseMapper<?>> mapperClass = MAPPER_CACHE.get(entityClass);
        if (Objects.isNull(mapperClass)) {
            mapperClass = CrudUtils.getMapperInterface(entityClass, baseMapperClass);
            MAPPER_CACHE.put(entityClass, mapperClass);
        }
        M mapper;
        synchronized (mapperClass) {
            try {
                mapper = (M) sqlSessionTemplate.getMapper(mapperClass);
            } catch (BindingException e) {
                sqlSessionTemplate.getConfiguration().addMapper(mapperClass);
                mapper = (M) sqlSessionTemplate.getMapper(mapperClass);
            }
        }
        return mapper;
    } catch (Throwable throwable) {
        log.error("Cannot auto get mapper for entity {} and mapperInterface {}", entityClass, baseMapperClass, throwable);
        throw new RuntimeException(throwable);
    }
}
Also used : BindingException(org.apache.ibatis.binding.BindingException) CollectionUtil(cn.hutool.core.collection.CollectionUtil) SpringUtils(com.github.yiuman.citrus.support.utils.SpringUtils) MapperRegistry(org.apache.ibatis.binding.MapperRegistry) CrudUtils(com.github.yiuman.citrus.support.utils.CrudUtils) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate) Serializable(java.io.Serializable) TableInfoHelper(com.baomidou.mybatisplus.core.metadata.TableInfoHelper) Objects(java.util.Objects) CrudMapper(com.github.yiuman.citrus.support.crud.mapper.CrudMapper) Slf4j(lombok.extern.slf4j.Slf4j) TypeUtil(cn.hutool.core.util.TypeUtil) TableInfo(com.baomidou.mybatisplus.core.metadata.TableInfo) BaseMapper(com.baomidou.mybatisplus.core.mapper.BaseMapper) Map(java.util.Map) ArrayUtil(cn.hutool.core.util.ArrayUtil) Tree(com.github.yiuman.citrus.support.model.Tree) CrudService(com.github.yiuman.citrus.support.crud.service.CrudService) BaseService(com.github.yiuman.citrus.support.crud.service.BaseService) TreeMapper(com.github.yiuman.citrus.support.crud.mapper.TreeMapper) SqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate) MapperRegistry(org.apache.ibatis.binding.MapperRegistry) BaseMapper(com.baomidou.mybatisplus.core.mapper.BaseMapper) BindingException(org.apache.ibatis.binding.BindingException)

Aggregations

CollectionUtil (cn.hutool.core.collection.CollectionUtil)1 ArrayUtil (cn.hutool.core.util.ArrayUtil)1 TypeUtil (cn.hutool.core.util.TypeUtil)1 BaseMapper (com.baomidou.mybatisplus.core.mapper.BaseMapper)1 TableInfo (com.baomidou.mybatisplus.core.metadata.TableInfo)1 TableInfoHelper (com.baomidou.mybatisplus.core.metadata.TableInfoHelper)1 CrudMapper (com.github.yiuman.citrus.support.crud.mapper.CrudMapper)1 TreeMapper (com.github.yiuman.citrus.support.crud.mapper.TreeMapper)1 BaseService (com.github.yiuman.citrus.support.crud.service.BaseService)1 CrudService (com.github.yiuman.citrus.support.crud.service.CrudService)1 Tree (com.github.yiuman.citrus.support.model.Tree)1 CrudUtils (com.github.yiuman.citrus.support.utils.CrudUtils)1 SpringUtils (com.github.yiuman.citrus.support.utils.SpringUtils)1 Serializable (java.io.Serializable)1 Map (java.util.Map)1 Objects (java.util.Objects)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Slf4j (lombok.extern.slf4j.Slf4j)1 BindingException (org.apache.ibatis.binding.BindingException)1 MapperRegistry (org.apache.ibatis.binding.MapperRegistry)1