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

      

  • 相关阅读:
    CSS3自定义滚动条样式 -webkit-scrollbar
    仿flash的文字动画效果
    使用PowerDesigner导出MySQL数据库建模
    将博客搬至CSDN
    centos6.3安装MySQL 5.6(转)
    # mysql -u root -p -bash: mysql: command not found
    win8设置保护眼睛的颜色
    网关末尾要么是1要么是254
    虚机centos和本机Windows之间文件的拷贝无法用xftp时用FileZilla也行
    Java基础知识总结之基础数据类型
  • 原文地址:https://www.cnblogs.com/read-acquisitions-docs/p/7636042.html
Copyright © 2020-2023  润新知