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

      

  • 相关阅读:
    selenium 学习(-)
    阿里百川SDK初始化失败 错误码是203
    sql-如何提高SQL查询的效率?
    索引失效原因总结
    索引失效原因总结
    sql-优化-大表关联小表
    sql-优化-大表关联小表
    C#-How to get series point info when mouse is over chartcontrol
    C#-How to get series point info when mouse is over chartcontrol
    C#-ILookup 初始化
  • 原文地址:https://www.cnblogs.com/read-acquisitions-docs/p/7636042.html
Copyright © 2011-2022 走看看