zoukankan      html  css  js  c++  java
  • spring boot 不使用@Transactional 配置事务和PageHelper分页

    @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;
    
        }
    }
    

      

  • 相关阅读:
    PAT顶级 1024 Currency Exchange Centers (35分)(最小生成树)
    Codeforces 1282B2 K for the Price of One (Hard Version)
    1023 Have Fun with Numbers (20)
    1005 Spell It Right (20)
    1092 To Buy or Not to Buy (20)
    1118 Birds in Forest (25)
    1130 Infix Expression (25)
    1085 Perfect Sequence (25)
    1109 Group Photo (25)
    1073 Scientific Notation (20)
  • 原文地址:https://www.cnblogs.com/read-acquisitions-docs/p/7636042.html
Copyright © 2011-2022 走看看