application config file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| server.port: 8080
spring: datasource: hikari: sqlite: jdbc-url: jdbc:sqlite:database/sqlite.db username: password: driver-class-name: org.sqlite.JDBC mysql: jdbc-url: jdbc:mysql://127.0.0.1:3306/db0314?useUnicode=true&characterEncoding=UTF-8 username: root password: 123456 driverClassName: com.mysql.cj.jdbc.Driver
|
sqlite datasource config
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 59 60 61 62 63 64 65 66 67 68 69
| package net.wchar.sample.mds.config;
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@MapperScan(basePackages = {"net.wchar.sample.mds.dao.sqlite"}, sqlSessionFactoryRef = "sqliteSqlSessionFactory") @Configuration public class SqliteDataSourceConfiguration { @Bean @Primary @ConfigurationProperties(prefix = "spring.datasource.hikari.sqlite") public HikariConfig sqliteHikariConfig() { return new HikariConfig(); }
@Bean @Primary public DataSource sqliteDataSource(@Qualifier("sqliteHikariConfig") HikariConfig sqliteHikariConfig) { return new HikariDataSource(sqliteHikariConfig); }
@Primary @Bean public SqlSessionFactory sqliteSqlSessionFactory(@Qualifier("sqliteDataSource") DataSource sqliteDataSource, ApplicationContext applicationContext) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(sqliteDataSource);
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class); configuration.setMapUnderscoreToCamelCase(true); sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath*:mappers/sqlite/**/*.xml")); return sqlSessionFactoryBean.getObject(); }
@Primary @Bean public SqlSessionTemplate sqliteSqlSessionTemplate(@Qualifier("sqliteSqlSessionFactory") SqlSessionFactory sqliteSqlSessionFactory) { return new SqlSessionTemplate(sqliteSqlSessionFactory); }
@Primary @Bean public DataSourceTransactionManager sqliteTransactionManager(@Qualifier("sqliteDataSource") DataSource sqliteDataSource) { return new DataSourceTransactionManager(sqliteDataSource); } }
|
mysql datasource config
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 59 60 61 62 63
| package net.wchar.sample.mds.config;
import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@MapperScan(basePackages = {"net.wchar.sample.mds.dao.mysql"}, sqlSessionFactoryRef = "mysqlSqlSessionFactory") @Configuration public class MysqlDataSourceConfiguration {
@Bean @ConfigurationProperties(prefix = "spring.datasource.hikari.mysql") public HikariConfig mysqlHikariConfig() { return new HikariConfig(); }
@Bean public DataSource mysqlDataSource(@Qualifier("mysqlHikariConfig") HikariConfig mysqlHikariConfig) { return new HikariDataSource(mysqlHikariConfig); }
@Bean public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource mysqlDataSource, ApplicationContext applicationContext) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(mysqlDataSource);
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class); configuration.setMapUnderscoreToCamelCase(true); sqlSessionFactoryBean.setConfiguration(configuration);
sqlSessionFactoryBean.setMapperLocations(applicationContext.getResources("classpath*:mappers/mysql/**/*.xml")); return sqlSessionFactoryBean.getObject(); }
@Bean public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory mysqlSqlSessionFactory) { return new SqlSessionTemplate(mysqlSqlSessionFactory); }
@Bean public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource mysqlDataSource) { return new DataSourceTransactionManager(mysqlDataSource); } }
|
This project github address
https://github.com/wchar-net/multiple-data-sources-sqlite-and-mysql