项目中MyBatis配置、MyBatisPlus的配置以及Mapper的扫描

 项目中MyBatis配置

@Slf4j
@ConditionalOnResource(resources = {"classpath:mapper"})
@ConditionalOnClass({SqlSessionFactory.class})
public class MyBatisAutoConfiguration {

    @Autowired
    private Interceptor[] interceptors;

    @Bean(name = "sqlSessionFactory")
    @ConditionalOnMissingBean(value = SqlSessionFactory.class, name = {"sqlSessionFactory", "SqlSessionFactory"})
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource,
                                                   MapperHelper mapperHelper) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // 设置mybatis XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        //配置通用mapper
        Configuration configuration = new Configuration();
        configuration.setMapperHelper(mapperHelper);
        bean.setConfiguration(configuration);
        //配置插件
        bean.setPlugins(interceptors);
        log.info("db plugins is set");
        SqlSessionFactory sqlSessionFactory = bean.getObject();
        //-自动使用驼峰命名属性映射字段   userId    user_id
        assert sqlSessionFactory != null;
        sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
        log.info("SqlSessionFactory is ready to inject.");
        return sqlSessionFactory;
    }

    /**
     * 通用mapper,配置说明详见https://github.com/abel533/Mapper/wiki/3.config
     */
    @Bean
    public MapperHelper mapperHelper() {
        Config config = new Config();
        config.setIDENTITY("MYSQL");
        config.setNotEmpty(false);
        config.setOrder("AFTER");
        config.setEnableMethodAnnotation(true);
        config.setCheckExampleEntityClass(true);
        config.setUseSimpleType(true);
        config.setEnumAsSimpleType(true);
        config.setWrapKeyword("`{0}`");
        config.setSafeDelete(true);
        config.setSafeUpdate(true);
        MapperHelper mapperHelper = new MapperHelper();
        mapperHelper.setConfig(config);
        log.info("tk.db.mapper is ready to inject.");
        return mapperHelper;
    }

    @Bean(name = "SqlSessionTemplateForFramework")
    @ConditionalOnMissingBean(value = SqlSessionTemplate.class, name = {"sqlSessionTemplateForFramework", "SqlSessionTemplateForFramework"})
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}

mapper扫描

@Slf4j
@ConditionalOnResource(resources = {"classpath:mapper"})
@ConditionalOnClass({SqlSessionFactory.class})
public class MapperScannerAutoConfiguration {

    @Bean(name = "MapperScannerConfigurerForFramework")
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        // 设置mybatis mapper接口目录
        mapperScannerConfigurer.setBasePackage("com.**.*.mapper");
        return mapperScannerConfigurer;
    }

}

MyBatisPlus配置 

@Slf4j
@Configuration
@ConditionalOnBean(MysqlAutoConfiguration.class)
public class MyBatisPluginAutoConfiguration {


    /**
     * 分页插件pageHelper 配置
     */
    @Bean
    public PageInterceptor pageHelper() {
        log.info("PageInterceptor is ready to inject.");
        Properties properties = new Properties();
        //reasonable:分页合理化参数,默认值为false。
        // 当该参数设置为 true 时,pageNum<=0 时会查询第一页, p
        // ageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
        properties.setProperty("reasonable", "true");
        //supportMethodsArguments:支持通过 Mapper 接口参数来传递分页参数,默认值false,
        // 分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。
        properties.setProperty("supportMethodsArguments", "true");
        //指定为MySQL数据库
        properties.setProperty("helperDialect", "mysql");
        PageInterceptor pageInterceptor = new PageInterceptor();
        pageInterceptor.setProperties(properties);
        return pageInterceptor;
    }
}

一些条件注解的使用,配置文件位置指定注解。如需更多了解,请百度之。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值