最近開辟了一個(gè)新項(xiàng)目,因?yàn)槌跗诳紤]到可能會(huì)調(diào)整數(shù)據(jù)庫的風(fēng)險(xiǎn),所以orm,在設(shè)計(jì)之初就考慮為Spring Data Jpa, 以下是工程data層數(shù)據(jù),整體是參照配置多數(shù)據(jù)源的方案,進(jìn)行配置的
目錄
- 因?yàn)榘⒗飻?shù)據(jù)源
Druid
- 整合數(shù)據(jù)源及其他事務(wù)配置
- pom依賴
整合事務(wù)
@EnableAutoConfiguration
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.inn.developer"})
public class CodeApplication {
public static void main(String[] args) {
new SpringApplicationBuilder().web(true).sources(CodeApplication.class).run(args);
}
}
創(chuàng)建DruidProperties
配置
@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "druid")
public class DruidProperties {
...
數(shù)據(jù)庫參數(shù)可以參考:
參數(shù) | 默認(rèn)值 | 解釋 |
---|---|---|
initialSize | 3 | 初始化配置 |
minIdle | 3 | 最小連接數(shù) |
maxActive | 15 | 最大連接數(shù) |
maxWait | 5000 | 獲取連接超時(shí)時(shí)間(單位:ms) |
timeBetweenEvictionRunsMillis | 90000 | 連接有效性檢測(cè)時(shí)間(單位:ms) |
testOnBorrow | false | 獲取連接檢測(cè) |
testOnReturn | false | 歸還連接檢測(cè) |
minEvictableIdleTimeMillis | 1800000 | 最大空閑時(shí)間(單位ms) |
testWhileIdle | true | 在獲取連接后阳仔,確定是否要進(jìn)行連接空間時(shí)間的檢查 |
- 配置說明:
1:minEvictableIdleTimeMillis(最大空閑時(shí)間):默認(rèn)為30分鐘笼才,配置里面不進(jìn)行設(shè)置食绿。
2:testOnBorrow ,testOnReturn 默認(rèn)為關(guān)閉贵白,可以設(shè)置為不配置。
3:testWhileIdle(在獲取連接后诚些,確定是否要進(jìn)行連接空閑時(shí)間的檢查)汤踏。默認(rèn)為true涝涤。配置里面不再進(jìn)行設(shè)置谚赎。
- 流程說明:
1:在第一次調(diào)用connection的時(shí)候淫僻,才會(huì)進(jìn)行 initialSize的初始化。
2:心跳檢測(cè)時(shí)間線程壶唤,會(huì)休眠timeBetweenEvictionRunsMillis時(shí)間雳灵,然后只對(duì)(沒有borrow的線程 減去 minIdle)的線程進(jìn)行檢查,如果空閑時(shí)間大于minEvictableIdleTimeMillis則進(jìn)行close闸盔。
3:testWhileIdle必須設(shè)置為true悯辙,在獲取到連接后,先檢查testOnBorrow迎吵,然后再判定testwhileIdle躲撰,如果連接空閑時(shí)間大于timeBetweenEvictionRunsMillis,則會(huì)進(jìn)行心跳檢測(cè)钓觉。
4:不需要配置validationQuery茴肥,如果不配置的情況下會(huì)走ping命令坚踩,性能更高荡灾。
5:連接保存在數(shù)組里面,獲取連接的時(shí)候瞬铸,獲取數(shù)組的最后一位批幌。在imeBetweenEvictionRunsMillis時(shí)是從前往后進(jìn)行檢查連接的有效性。
配置數(shù)據(jù)源及hibernate適配
數(shù)據(jù)源對(duì)象創(chuàng)建還是和之前一樣,
筆者不太喜歡xml的方式,所以還是采用配置類
DruidAutoJpaConfiguration
@Configuration
@EnableConfigurationProperties(DruidProperties.class)//開啟屬性注入,通過@autowired注入
@ConditionalOnClass(DruidDataSource.class)//表示對(duì)應(yīng)的類在classpath目錄下存在時(shí)嗓节,才會(huì)去解析對(duì)應(yīng)的配置文件
@ConditionalOnProperty(prefix = "druid", name = "url")
@EnableJpaRepositories(basePackages = "com.inn.developer.model.dao",transactionManagerRef = "jpaTransactionManager", entityManagerFactoryRef = "localContainerEntityManagerFactoryBean")
public class DruidAutoJpaConfiguration {
@Autowired
private DruidProperties properties;
@Bean(name = "druidDataSource")
@Primary
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUsername());
dataSource.setPassword(properties.getPassword());
dataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
if (properties.getInitialSize() > 0) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > 0) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > 0) {
dataSource.setMaxActive(properties.getMaxActive());
}
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
dataSource.setValidationQuery("select version()");
try {
dataSource.init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataSource;
}
/**
* hibernate 適配器,定制方言為mysql,并打印sql
*
* @return
*/
@Bean(name = "hibernateJpaVendorAdapter")
@Primary
public HibernateJpaVendorAdapter hibernateJpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
return hibernateJpaVendorAdapter;
}
@Bean(name = "localContainerEntityManagerFactoryBean")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(@Qualifier("druidDataSource") DataSource dataSource
,@Qualifier("hibernateJpaVendorAdapter") HibernateJpaVendorAdapter hibernateJpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean local = new LocalContainerEntityManagerFactoryBean();
local.setDataSource(dataSource);
local.setJpaVendorAdapter(hibernateJpaVendorAdapter);
local.setPackagesToScan("com.inn.developer.model.domain");
Properties properties = new Properties();
properties.put("hibernate.format_sql", true);
properties.put("hibernate.hbm2ddl.auto", "update");
local.setJpaProperties(properties);
return local;
}
@Bean(name = "jpaTransactionManager")
@Primary
public JpaTransactionManager jpaTransactionManager(@Qualifier("localContainerEntityManagerFactoryBean") LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
EntityManagerFactory object = entityManagerFactoryBean.getObject();
jpaTransactionManager.setEntityManagerFactory(object);
return jpaTransactionManager;
}
pom依賴
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!--依賴Spring 4.3.6之core荧缘、context、aop拦宣、beans截粗、tx信姓、orm和spring data commons -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.3.RELEASE</version>
</dependency>
<!--hibernate 實(shí)現(xiàn)JPA的框架 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.5.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.11.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-annotations -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>