码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot 不使用@Transactional 配置事务和PageHelper分页

时间:2017-10-07 22:52:03      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:primary   .property   poi   dial   throw   setenv   log   between   failed   

@Configuration
@PropertySource(value = "classpath:db.properties")
public class DatabaseConfiguration implements EnvironmentAware {
    private RelaxedPropertyResolver propertyResolver;
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(DatabaseConfiguration.class);

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.boot.druid.");
    }

    @Bean(name = "datasourceAccountW", initMethod = "init", destroyMethod = "close")
    @Scope(value = BeanDefinition.SCOPE_SINGLETON)
    @Primary 
    public DruidDataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();

        try {
            druidDataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
            druidDataSource.setUrl(propertyResolver.getProperty("url"));
            druidDataSource.setUsername(propertyResolver.getProperty("username"));
            druidDataSource.setPassword(propertyResolver.getProperty("password"));
            druidDataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("initialSize")));
            druidDataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("minIdle")));
            druidDataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("maxActive")));
            druidDataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("maxWait")));
            druidDataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(propertyResolver.getProperty("timeBetweenEvictionRunsMillis")));
            druidDataSource.setMinEvictableIdleTimeMillis(Long.parseLong(propertyResolver.getProperty("minEvictableIdleTimeMillis")));
            druidDataSource.setValidationQuery(propertyResolver.getProperty("validationQuery"));
            druidDataSource.setTestWhileIdle(Boolean.parseBoolean(propertyResolver.getProperty("testWhileIdle")));
            druidDataSource.setTestOnBorrow(Boolean.parseBoolean(propertyResolver.getProperty("testOnBorrow")));
            druidDataSource.setTestOnReturn(Boolean.parseBoolean(propertyResolver.getProperty("testOnReturn")));
            druidDataSource.setPoolPreparedStatements(Boolean.parseBoolean(propertyResolver.getProperty("poolPreparedStatements")));
            druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(propertyResolver.getProperty("maxPoolPreparedStatementPerConnectionSize")));
            druidDataSource.setFilters(propertyResolver.getProperty("filters"));
        } catch (Throwable throwable) {
            logger.error("datasource init failed.", throwable);
            throw throwable;
        }
        return druidDataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(ds);

        PageHelper pageHelper = new PageHelper();
        Properties props = new Properties();
        props.setProperty("dialect", "mysql");
        props.setProperty("reasonable", "true");

        props.setProperty("offsetAsPageNum", "true");
        props.setProperty("returnPageInfo", "check");
        props.setProperty("params", "count=countSql");
        pageHelper.setProperties(props);

        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }


    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage(propertyResolver.getProperty("project.mapper.package"));  //多个包用逗号分隔
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }


    @Bean(name = "txManager")
    public PlatformTransactionManager annotationDrivenTransactionManager(DataSource ds) throws SQLException {
        return new DataSourceTransactionManager(ds);
    }


    @Bean(name = "txInterceptor")
    @Primary
    public TransactionInterceptor transactionInterceptor(PlatformTransactionManager transactionManager) throws SQLException {
        Properties props = new Properties();
        props.setProperty("select*", "PROPAGATION_SUPPORTS,-Throwable,readOnly");
//
//        props.setProperty("insert*", "PROPAGATION_REQUIRED,-java.lang.Exception");
//        props.setProperty("update*", "PROPAGATION_REQUIRED,-java.lang.Exception");
//        props.setProperty("delete*", "PROPAGATION_REQUIRED,-java.lang.Exception");

        props.setProperty("*", "PROPAGATION_REQUIRED");

        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, props);

        AspectJExpressionPointcutAdvisor pointcutAdvisor = new AspectJExpressionPointcutAdvisor();
        pointcutAdvisor.setAdvice(txAdvice);
        pointcutAdvisor.setExpression("execution (* o.k.s.impl.*.*(..))");

        return txAdvice;
    }


    @Bean
    public BeanNameAutoProxyCreator transProxy() {
        BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
        creator.setProxyTargetClass(true);
        creator.setBeanNames("*Impl");
        creator.setInterceptorNames("txInterceptor");
        return creator;

    }
}

  

spring boot 不使用@Transactional 配置事务和PageHelper分页

标签:primary   .property   poi   dial   throw   setenv   log   between   failed   

原文地址:http://www.cnblogs.com/read-acquisitions-docs/p/7636042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!