spring boot项目自定义数据源,mybatisplus分页…

2019-05-18 07:07:59来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Spring Boot项目中数据源的配置可以通过两种方式实现:

1.application.yml或者application.properties配置

2.注入DataSource及SqlSessionFactory两个Bean

通过第二种方式配置数据源则按照MybatisPlus官方文档使用分页及逻辑删除插件会无效,解决思路是在初始化SqlSessionFactory将插件设置进去

    /**
     * 逻辑删除插件
     */
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setLogicDeleteValue("Y");
        dbConfig.setLogicNotDeleteValue("N");
        globalConfig.setDbConfig(dbConfig);
        globalConfig.setSqlInjector(new LogicSqlInjector());
        return globalConfig;
    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType(DbType.MYSQL.getDb());
        return paginationInterceptor;
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        logger.info("初始化SqlSessionFactory");
        String mapperLocations = "classpath:mybatis/mapper/**/*.xml";
        String configLocation = "classpath:mybatis/mybatis-config.xml";
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource()); //数据源
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactory.setMapperLocations(resolver.getResources(mapperLocations));
        sqlSessionFactory.setConfigLocation(resolver.getResource(configLocation));
        sqlSessionFactory.setTypeAliasesPackage("com.innjoy.pms.order.infrastructure.domain.model");
        sqlSessionFactory.setGlobalConfig(globalConfig());
        sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor()});
        return sqlSessionFactory.getObject();
    }

 


原文链接:https://www.cnblogs.com/loptis/p/10879417.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:hibernate dynamic-update="true"属性不起作用原因(转

下一篇:【Java】NIO中Channel的注册源码分析