zoukankan      html  css  js  c++  java
  • mybatis-plus

    mybatis 的通用maper, 其实有很多, mybatis-plus 算是其中做的比较好的一款了. 这里就来看一下 mybatis-plus 是怎么实现这个通用mapper功能的.

    一. BaseMapper

    mybatis中 Mapper interface 的时候, 并没有继承什么接口. 所以想要什么方法, 得自己添加. 

    在mybatis-plus 中, 让 Mapper interface 继承了 BaseMapper 接口, 直接的结果, 就是 Mapper.java 多了很多方法:

    public interface BaseMapper<T> {
        /**
         * <p>
         * 插入一条记录
         * </p>
         *
         * @param entity 实体对象
         */
        int insert(T entity);
    
        /**
         * <p>
         * 根据 ID 删除
         * </p>
         *
         * @param id 主键ID
         */
        int deleteById(Serializable id);
    
        /**
         * <p>
         * 根据 columnMap 条件,删除记录
         * </p>
         *
         * @param columnMap 表字段 map 对象
         */
        int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
    
        /**
         * <p>
         * 根据 entity 条件,删除记录
         * </p>
         *
         * @param queryWrapper 实体对象封装操作类(可以为 null)
         */
        int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
    
        /**
         * <p>
         * 删除(根据ID 批量删除)
         * </p>
         *
         * @param idList 主键ID列表(不能为 null 以及 empty)
         */
        int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
    
        /**
         * <p>
         * 根据 ID 修改
         * </p>
         *
         * @param entity 实体对象
         */
        int updateById(@Param(Constants.ENTITY) T entity);
    
        /**
         * <p>
         * 根据 whereEntity 条件,更新记录
         * </p>
         *
         * @param entity        实体对象 (set 条件值,不能为 null)
         * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
         */
        int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
    
        /**
         * <p>
         * 根据 ID 查询
         * </p>
         *
         * @param id 主键ID
         */
        T selectById(Serializable id);
    
        /**
         * <p>
         * 查询(根据ID 批量查询)
         * </p>
         *
         * @param idList 主键ID列表(不能为 null 以及 empty)
         */
        List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
    
        /**
         * <p>
         * 查询(根据 columnMap 条件)
         * </p>
         *
         * @param columnMap 表字段 map 对象
         */
        List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
    
        /**
         * <p>
         * 根据 entity 条件,查询一条记录
         * </p>
         *
         * @param queryWrapper 实体对象
         */
        T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
    
        /**
         * <p>
         * 根据 Wrapper 条件,查询总记录数
         * </p>
         *
         * @param queryWrapper 实体对象
         */
        Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
    
        /**
         * <p>
         * 根据 entity 条件,查询全部记录
         * </p>
         *
         * @param queryWrapper 实体对象封装操作类(可以为 null)
         */
        List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
    
        /**
         * <p>
         * 根据 Wrapper 条件,查询全部记录
         * </p>
         *
         * @param queryWrapper 实体对象封装操作类(可以为 null)
         */
        List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
    
        /**
         * <p>
         * 根据 Wrapper 条件,查询全部记录
         * 注意: 只返回第一个字段的值
         * </p>
         *
         * @param queryWrapper 实体对象封装操作类(可以为 null)
         */
        List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
    
        /**
         * <p>
         * 根据 entity 条件,查询全部记录(并翻页)
         * </p>
         *
         * @param page         分页查询条件(可以为 RowBounds.DEFAULT)
         * @param queryWrapper 实体对象封装操作类(可以为 null)
         */
        IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
    
        /**
         * <p>
         * 根据 Wrapper 条件,查询全部记录(并翻页)
         * </p>
         *
         * @param page         分页查询条件
         * @param queryWrapper 实体对象封装操作类
         */
        IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
    }

    二. MapperScan

    MapperScan这块, 还是 mybatis 的功能. 对 Mapper.java 进行扫描注册 : MapperFactoryBean 

    具体过程见: MapperScan

    三. MybatisPlusAutoConfiguration

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setVfs(SpringBootVFS.class);
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }
        applyConfiguration(factory);
        if (this.properties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(this.properties.getConfigurationProperties());
        }
        if (!ObjectUtils.isEmpty(this.interceptors)) {
            factory.setPlugins(this.interceptors);
        }
        if (this.databaseIdProvider != null) {
            factory.setDatabaseIdProvider(this.databaseIdProvider);
        }
        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        }
        // TODO 自定义枚举包
        if (StringUtils.hasLength(this.properties.getTypeEnumsPackage())) {
            factory.setTypeEnumsPackage(this.properties.getTypeEnumsPackage());
        }
        if (this.properties.getTypeAliasesSuperType() != null) {
            factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
        }
        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
            factory.setMapperLocations(this.properties.resolveMapperLocations());
        }
        // TODO 此处必为非 NULL
        GlobalConfig globalConfig = this.properties.getGlobalConfig();
        //注入填充器
        if (this.applicationContext.getBeanNamesForType(MetaObjectHandler.class,
            false, false).length > 0) {
            MetaObjectHandler metaObjectHandler = this.applicationContext.getBean(MetaObjectHandler.class);
            globalConfig.setMetaObjectHandler(metaObjectHandler);
        }
        //注入主键生成器
        if (this.applicationContext.getBeanNamesForType(IKeyGenerator.class, false,
            false).length > 0) {
            IKeyGenerator keyGenerator = this.applicationContext.getBean(IKeyGenerator.class);
            globalConfig.getDbConfig().setKeyGenerator(keyGenerator);
        }
        //注入sql注入器
        if (this.applicationContext.getBeanNamesForType(ISqlInjector.class, false,
            false).length > 0) {
            ISqlInjector iSqlInjector = this.applicationContext.getBean(ISqlInjector.class);
            globalConfig.setSqlInjector(iSqlInjector);
        }
        factory.setGlobalConfig(globalConfig);
        return factory.getObject();
    }
    
    
    private void applyConfiguration(MybatisSqlSessionFactoryBean factory) {
        MybatisConfiguration configuration = this.properties.getConfiguration();
        if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
            configuration = new MybatisConfiguration();
        }
        if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
            for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
                customizer.customize(configuration);
            }
        }
        factory.setConfiguration(configuration);
    }
    
    
    @Bean
    @ConditionalOnMissingBean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        ExecutorType executorType = this.properties.getExecutorType();
        if (executorType != null) {
            return new SqlSessionTemplate(sqlSessionFactory, executorType);
        } else {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }

    关键类对应表:

    mybatis mybatis-plus
    SqlSessionFactoryBean
    MybatisSqlSessionFactoryBean 
    Configuration
    MybatisConfiguration
    SqlSessionTemplate
    SqlSessionTemplate
    1. MybatisSqlSessionFactoryBean 并不是 SqlSessionFactoryBean 的继承类, 他是一个新类. 其中有很多属性和方法都是从 SqlSessionFactoryBean 中拷贝过来的,然后加了一些东西.
    2. MybatisConfiguration 继承自 Configuration 类, 其中加入了一个非常重要的属性: 
        /**
         * Mapper 注册
         */
        public final MybatisMapperRegistry mybatisMapperRegistry = new MybatisMapperRegistry(this);

    与之对应的是  Configuration  中的:

    protected final MapperRegistry mapperRegistry = new MapperRegistry(this);
  • 相关阅读:
    iOS9 HTTP 不能正常使用的解决办法
    IOS UIWebView的一些用法总结
    顺序查找
    循环队列
    队列的链式存储实现
    栈的链式存储实现
    顺序表的实现
    MessageBox函数
    二分法查找
    冒泡排序
  • 原文地址:https://www.cnblogs.com/elvinle/p/12320581.html
Copyright © 2011-2022 走看看