hibernate mysql datasource_springboot2.0.3使用jpa和hibernate的方式配置多数据源(sqlite和mysql)...
时间: 2024-05-10 11:18:28 浏览: 216
首先需要在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.30.1</version>
</dependency>
```
然后配置application.yml文件,如下所示:
```yaml
spring:
datasource:
mysql:
url: jdbc:mysql://localhost:3306/mysql_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
sqlite:
url: jdbc:sqlite:/data/sqlite_db.db
driver-class-name: org.sqlite.JDBC
username: root
password: root
jpa:
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
```
接下来需要创建两个数据源的配置类,分别为MysqlConfig和SqliteConfig,代码如下所示:
```java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "mysqlEntityManagerFactory",
transactionManagerRef = "mysqlTransactionManager",
basePackages = {"com.example.mysql.repository"})
public class MysqlConfig {
@Primary
@Bean(name = "mysqlDataSource")
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "mysqlEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("mysqlDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.example.mysql.entity").persistenceUnit("mysql")
.build();
}
@Primary
@Bean(name = "mysqlTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("mysqlEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
```
```java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "sqliteEntityManagerFactory",
transactionManagerRef = "sqliteTransactionManager",
basePackages = {"com.example.sqlite.repository"})
public class SqliteConfig {
@Bean(name = "sqliteDataSource")
@ConfigurationProperties(prefix = "spring.datasource.sqlite")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sqliteEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("sqliteDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.example.sqlite.entity").persistenceUnit("sqlite")
.build();
}
@Bean(name = "sqliteTransactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("sqliteEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
```
注意事项:
1. 在MysqlConfig和SqliteConfig中需要使用`@EnableJpaRepositories`注解来指定数据源的repository所在包。
2. 在MysqlConfig和SqliteConfig中创建的EntityManagerFactory和TransactionManager需要使用`@Primary`注解来标识默认数据源。
3. application.yml中的`hibernate.dialect`需要根据不同的数据库进行修改。
4. 在repository中需要使用`@Qualifier`注解来指定使用的数据源。
使用多数据源时,需要在service或controller中使用`@Transactional("mysqlTransactionManager")`或`@Transactional("sqliteTransactionManager")`注解来指定使用的数据源。
至此,一个使用JPA和Hibernate配置多数据源的示例就完成了。
阅读全文