SpringBoot整合阿里Druid数据源及Spring-Data-Jpa
最近开辟了一个新项目,因为初期考虑到可能会调整数据库的风险,所以orm,在设计之初就考虑为Spring Data Jpa, 以下是工程data层数据,整体是参照配置多数据源的方案,进行配置的
目录
-
因为阿里数据源
Druid -
整合数据源及其他事务配置
-
pom依赖
整合事务
-
@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); -
} -
}
创建 DruidProperties配置
-
@Data -
@AllArgsConstructor -
@NoArgsConstructor -
@ConfigurationProperties(prefix = "druid") -
public class DruidProperties { -
...
数据库参数可以参考:
| 参数 | 默认值 | 解释 |
|---|---|---|
| initialSize | 3 | 初始化配置 |
| minIdle | 3 | 最小连接数 |
| maxActive | 15 | 最大连接数 |
| maxWait | 5000 | 获取连接超时时间(单位:ms) |
| timeBetweenEvictionRunsMillis | 90000 | 连接有效性检测时间(单位:ms) |
| testOnBorrow | false | 获取连接检测 |
| testOnReturn | false | 归还连接检测 |
| minEvictableIdleTimeMillis | 1800000 | 最大空闲时间(单位ms) |
| testWhileIdle | true | 在获取连接后,确定是否要进行连接空间时间的检查 |
|
||
| 1:minEvictableIdleTimeMillis(最大空闲时间):默认为30分钟,配置里面不进行设置。 |
2:testOnBorrow ,testOnReturn 默认为关闭,可以设置为不配置。
3:testWhileIdle(在获取连接后,确定是否要进行连接空闲时间的检查)。默认为true。配置里面不再进行设置。
-
流程说明:
1:在第一次调用connection的时候,才会进行 initialSize的初始化。
2:心跳检测时间线程,会休眠timeBetweenEvictionRunsMillis时间,然后只对(没有borrow的线程 减去 minIdle)的线程进行检查,如果空闲时间大于minEvictableIdleTimeMillis则进行close。
3:testWhileIdle必须设置为true,在获取到连接后,先检查testOnBorrow,然后再判定testwhileIdle,如果连接空闲时间大于timeBetweenEvictionRunsMillis,则会进行心跳检测。
4:不需要配置validationQuery,如果不配置的情况下会走ping命令,性能更高。
5:连接保存在数组里面,获取连接的时候,获取数组的最后一位。在imeBetweenEvictionRunsMillis时是从前往后进行检查连接的有效性。
配置数据源及hibernate适配
数据源对象创建还是和之前一样, 笔者不太喜欢xml的方式,所以还是采用配置类
DruidAutoJpaConfiguration
-
@Configuration -
@EnableConfigurationProperties(DruidProperties.class)//开启属性注入,通过@autowired注入 -
@ConditionalOnClass(DruidDataSource.class)//表示对应的类在classpath目录下存在时,才会去解析对应的配置文件 -
@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 实现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>