maven引入:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>com.zsoltfabok</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-version}</version>
</dependency>
application.yml
server:
port: 8289
spring:
profiles:
active: dev
jpa:
database: MYSQL
hibernate:
show-sql: true
ddl-auto: update
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
# database-platform: org.hibernate.dialect.SQLiteDialect
datasource:
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite:cuv_selef.db
comment:
datasource:
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite:cgst_self.db
mysql:
datasource:
url: jdbc:mysql://localhost:3306/sqlite?useUnicode=true&useSSL=false&serverTimezone=UTC
testWhileIdle: true
validationQuery: SELECT 1 from dual
username: username
password: password
driverClassName: com.mysql.jdbc.Driver
加载数据库的配置(两种方式,一种是直接手动配置,一种是动态获取application.yml的配置数据):
@Configuration
public class DataSourceConfig {
@Primary //配置该数据源为主数据源
@Bean(name = "bibleDataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource bibleDataSource() {
// DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
// dataSourceBuilder.driverClassName("org.sqlite.JDBC");
// dataSourceBuilder.url("jdbc:sqlite:cuv_selef.db");
// dataSourceBuilder.type(SQLiteDataSource.class);
// return dataSourceBuilder.build();
return DataSourceBuilder.create().build();
}
@Bean(name = "commentDataSource")
@ConfigurationProperties(prefix = "comment.datasource")
public DataSource commentDataSource(){
// DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
// dataSourceBuilder.driverClassName("org.sqlite.JDBC");
// dataSourceBuilder.url("jdbc:sqlite:cgst_self.db");
// dataSourceBuilder.type(SQLiteDataSource.class);
// return dataSourceBuilder.build();
return DataSourceBuilder.create().build();
}
@Bean(name = "mysqlDataSource")
@ConfigurationProperties(prefix = "mysql.datasource")
public DataSource mysqlDataSource(){
return DataSourceBuilder.create().build();
}
}
数据源 事务+工厂(主要是针对jpa,若是使用mybatis):
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryBible", //EntityManagerFactory引用
transactionManagerRef="transactionManagerBible", //transactionManager引用
basePackages={"com.zhunei.bible.sqliteupdata.repository.bible"}) // repository 的位置
//mybatis 多数据源的配置
/*@MapperScan(basePackages = {"com.zhunei.church.churchchangedata.church.mapper"},
sqlSessionFactoryRef = "sqlSessionFactoryOne",
sqlSessionTemplateRef = "sqlSessionTemplateOne")*/
public class BibleSqliteDataSourceConfig {
/**
* 注入 sqlite数据源
*/
@Resource(name="DataSource")
private DataSource bibleDataSource;
@Primary
@Bean(name = "entityManagerBible")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryBible(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryBible")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBible(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(bibleDataSource)
.properties(getVendorProperties(bibleDataSource))
.packages("com.entiy.bible") //设置实体类所在位置
.persistenceUnit("biblePersistenceUnit")
.build();
}
@Autowired
private JpaProperties jpaProperties;
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Primary
@Bean(name = "transactionManagerBible")
public PlatformTransactionManager transactionManagerBibley(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryBible(builder).getObject());
}
/**
* mybatis
*
* @return
* @throws Exception
*/
/* @Bean
public SqlSessionFactory sqlSessionFactoryOne() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(primaryDataSource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/church/*.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplateOne() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryOne()); // 使用上面配置的Factory
return template;
}
*/
}
另外的一个sqlite和mysql的配置也差不多,但是其他的不使用@Primary。
注意:sqlite 访问数据对应的实体对象必须加上@Accessors(chain = true)
如实体表Copyright:
@Entity
@Table(name = "copyright")
@Accessors(chain = true)
@Data
@NoArgsConstructor
public class Copyright implements Serializable {
@Id
private String id;
private String summary;
private String copyright;
private String website;
private String pubblishTime;
}
其他 service,repository,controller都是正常配置即可
本文暂时没有评论,来添加一个吧(●'◡'●)