spring boot 配置DataSource 和 事务管理

spring boot 采用两种方式配置:xml方式和java config方式。在此主要说明java config方式。

1. DataSource配置

@Configuration
@PropertySource("classpath:application.properties")
public class DataSourceConfiguration {
    @Value("${spring.datasource.driverClass:com.mysql.jdbc.Driver}")
    private String driverClass;
    //10.2.3.22
    @Value("${spring.datasource.jdbcUrl:jdbc:mysql:///?useUnicode=true&characterEncoding=UTF-8}")
    private String jdbcUrl;
    @Value("${spring.datasource.user:}")
    private String user;
    @Value("${spring.datasource.password:}")
    private String password;
    @Value("${spring.datasource.initialPoolSize:34}")
    private int initialPoolSize;
    @Value("${spring.datasource.maxActive:500}")
    private int maxActive;
    @Value("${spring.datasource.minIdle:100}")
    private int minIdle;
    @Value("${spring.datasource.maxIdle:500}")
    private int maxIdle;
    @Value("${spring.datasource.validateInterval:30}")
    private int validateInterval;


    @Bean
    public DataSource dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
//        org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setDriverClass(driverClass);
        dataSource.setInitialPoolSize(initialPoolSize);
        dataSource.setMaxAdministrativeTaskTime(maxActive);
        dataSource.setMaxIdleTime(maxIdle);

        return dataSource;
    }
}



上述是一个完整的java config类,该类使用@Configuration注解指明。其中,@PropertySource用于指明属性文件的路径,该属性文件存储的是类中的属性值信息。

@Value("${spring.datasource.driverClass:com.mysql.jdbc.Driver}") spring.datasource.driverClass,为properties文件中的key值,而com.mysql.jdbc.Driver为默认值,如果key值不存在或者是其对应的value为空就默认采用默认值。

在此,我们使用的是C3P0提供的DataSource类。也可以使用其他开源插件。


2. 配置事务。

@Configuration
@EnableTransactionManagement
public class JpaRepositoryConfiguration {

    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();

        hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
        hibernateJpaVendorAdapter.setGenerateDdl(true);

        return hibernateJpaVendorAdapter;
    }

    @Bean
    public EntityManagerFactory entityManagerFactory(JpaVendorAdapter jpaVendorAdapter) {

        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        localContainerEntityManagerFactoryBean.setDataSource(dataSource);
        localContainerEntityManagerFactoryBean.setPackagesToScan("com.kuaidi.bi");
        localContainerEntityManagerFactoryBean.afterPropertiesSet();
//        return builder.dataSource(dataSource).build();
        return localContainerEntityManagerFactoryBean.getObject();

    }

    @Bean
//    public PlatformTransactionManager annotationDrivenTransactionManager(EntityManagerFactory entityManagerFactory) {
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
        return jpaTransactionManager;
    }


}






转载于:https://my.oschina.net/u/1011659/blog/397620

你可能感兴趣的:(spring boot 配置DataSource 和 事务管理)