数据源配置
BoneCp配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "primaryEntityManagerFactory", transactionManagerRef = "primaryTransactionManager", basePackages = {"com.july.jpa.config.dao.parimary"}) @PropertySource(value = "classpath:jdbc.properties") @ConfigurationProperties(prefix = "primary") public class PrimaryDataSourceConfig extends BoneCPConfig { @Value("${primary.show-sql}") private boolean showSql; @Value("${primary.hbm2ddl.auto}") private String hbm2ddl; @Value("${primary.database-platform}") private String platform; @Bean(name = "primaryDataSource") @Primary public DataSource primaryDataSource() throws SQLException { return new BoneCPDataSource(this); }
@Primary @Bean(name = "primaryEntityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory( EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) { return builder .dataSource(dataSource) .properties(getVendorProperties(dataSource)) .packages("com.july.jpa.config.dao.parimary.domain") .persistenceUnit("primary") .build(); }
@Autowired private JpaProperties jpaProperties;
private Map<String, String> getVendorProperties(DataSource dataSource) { jpaProperties.setShowSql(showSql); jpaProperties.getHibernate().setDdlAuto(hbm2ddl); jpaProperties.setDatabasePlatform(platform); return jpaProperties.getHibernateProperties(dataSource); }
@Bean(name = "primaryEntityManager") public EntityManager entityManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) { EntityManager manager = entityManagerFactory.createEntityManager(); return manager; }
@Primary @Bean(name = "primaryTransactionManager") public PlatformTransactionManager transactionManager( @Qualifier("primaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) { return new JpaTransactionManager(entityManagerFactory); }
}
|
Hikari配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| @Configuration @EnableTransactionManagement @EnableJpaRepositories( entityManagerFactoryRef = "ds1EntityManagerFactory", transactionManagerRef = "ds1TransactionManager", basePackages = {"com.july.jpa.config.dao.ds1"}) @PropertySource(value = "classpath:jdbc.properties") @ConfigurationProperties(prefix = "ds1") public class DataSourceOneConfig extends HikariConfig { @Value("${ds1.show-sql}") private boolean showSql; @Value("${ds1.hbm2ddl.auto}") private String hbm2ddl; @Value("${ds1.database-platform}") private String platform; @Bean(name = "ds1DataSource") public DataSource hikariDataSource() throws SQLException { return new HikariDataSource(this); }
@Bean(name = "ds1EntityManagerFactory") public LocalContainerEntityManagerFactoryBean ds1EntityManagerFactory( EntityManagerFactoryBuilder builder, @Qualifier("ds1DataSource") DataSource ds1DataSource) { return builder .dataSource(ds1DataSource) .properties(getVendorProperties(ds1DataSource)) .packages("com.july.jpa.config.dao.ds1.domain") .persistenceUnit("ds1") .build(); }
@Autowired private JpaProperties jpaProperties;
private Map<String, String> getVendorProperties(DataSource dataSource) { jpaProperties.setShowSql(showSql); jpaProperties.getHibernate().setDdlAuto(hbm2ddl); jpaProperties.setDatabasePlatform(platform); return jpaProperties.getHibernateProperties(dataSource); }
@Bean(name = "ds1EntityManager") public EntityManager entityManager(@Qualifier("ds1EntityManagerFactory") EntityManagerFactory entityManagerFactory) { EntityManager manager = entityManagerFactory.createEntityManager(); return manager; }
@Bean(name = "ds1TransactionManager") public PlatformTransactionManager ds1TransactionManager( @Qualifier("ds1EntityManagerFactory") EntityManagerFactory ds1EntityManagerFactory) { return new JpaTransactionManager(ds1EntityManagerFactory); }
}
|
数据源配置文件
1.注意ds1这类前缀,均要根据上文的配置类来指定。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ds1.jdbcUrl=jdbc:mysql://localhost:3306/test?autoReconnect=true&characterEncoding=UTF-8&failOverReadOnly=false&useSSL=false ds1.username=root ds1.password=root ds1.driverClassName=com.mysql.jdbc.Driver ds1.maxConnectionsPerPartition=20 ds1.minConnectionsPerPartition=10 ds1.partitionCount=2 ds1.acquireIncrement=2 ds1.type= com.zaxxer.hikari.HikariDataSource ds1.minimumIdle = 20 ds1.maximumPoolSize = 30 ds1.idleTimeout = 5000 ds1.show-sql=false ds1.hbm2ddl.auto=update ds1.database-platform=org.hibernate.dialect.MySQL5Dialect
|
2.多数据源注意使用@Primary指定主数据源,否则会出错
3.在配置类中指定JpaProperties,明确是否显示sql,是否自动创建表(hbm2ddl.auto)