Springboot整合Mybatis,连接多个数据库(Mysql+Oracle)
时间:2022-01-09 作者:lwl80
maven依赖,需要注意的是mysql使用的版本
1 <dependencies> 2 <dependency> 3 <groupId>域名域名</groupId> 4 <artifactId>ojdbc8</artifactId> 5 <scope>runtime</scope> 6 </dependency> 7 <dependency> 8 <groupId>mysql</groupId> 9 <artifactId>mysql-connector-java</artifactId> 10 <version>域名</version> 11 </dependency> 12 <!--Druid依赖代码 收藏代码--> 13 <dependency> 14 <groupId>域名aba</groupId> 15 <artifactId>druid</artifactId> 16 <version>域名</version> 17 </dependency> 18 </dependencies>
域名erties配置文件
1 #Spring boot视图配置 2 域名.prefix=/WEB-INF/view/ 3 域名.suffix=.jsp 4 #静态文件访问配置 5 域名ic-path-pattern=/static/** 6 7 #加载Mybatis的xml 在:resources下 8 域名er-locations=classpath:/com/spring/login/mapper/mysql/*.xml 9 10 #设置运行的端口 11 域名=8080 12 13 #配置字符编码 14 域名域名led=true 15 域名域名e=true 16 域名域名set=UTF-8 17 18 域名-encoding=UTF-8 19 20 #取消thymeleaf 严格检查 21 域名=LEGACYHTML5 22 23 #连接Oracle数据库 24 域名er-class-name= 域名域名leDriver 25 域名= jdbc:oracle:thin:@你的地址:1521/orcl 26 域名name=账号 27 域名word=密码 28 29 #配置连接mySql数据库 30 域名=jdbc:mysql://你的地址:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8 31 域名name=账号 32 域名word=密码 33 域名er-class-name=域名.Driver
看一下目录结构,既,不同的数据库我放在了DAO里不同的包里了,主要是为了区分
在config里新建了两个config类,分别是配置mysql以及oracle数据源使用的,需要注意的是,两个或者多个数据源时,一定要制定一个(只有一个)为主数据源,下面时两个数据源代码
1 package 域名域名ig; 2 3 import 域名域名dDataSource; 4 import 域名域名essionFactory; 5 import 域名域名essionFactoryBean; 6 import 域名域名essionTemplate; 7 import 域名域名erScan; 8 import 域名域名域名ifier; 9 import 域名域名域名e; 10 import 域名域名; 11 import 域名域名iguration; 12 import 域名域名ary; 13 import 域名.域名MatchingResourcePatternResolver; 14 import 域名.域名SourceTransactionManager; 15 16 import 域名Source; 17 18 @SuppressWarnings("all") 19 @Configuration 20 @MapperScan(basePackages = "域名域名l", sqlSessionTemplateRef = "mysqlSqlSessionTemplate") 21 public class MysqlDataSourceConfig { 22 @Value("${域名}") 23 private String url; 24 @Value("${域名name}") 25 private String user; 26 @Value("${域名word}") 27 private String password; 28 @Value("${域名er-class-name}") 29 private String driverClass; 30 31 @Bean(name = "mysqlDataSource") 32 @Primary 33 public DataSource mysqlDataSource() { 34 DruidDataSource dataSource = new DruidDataSource(); 35 域名rl(url); 36 域名sername(user); 37 域名assword(password); 38 域名riverClassName(driverClass); 39 return (DataSource) dataSource; 40 } 41 42 @Bean(name = "mysqlTransactionManager") 43 @Primary 44 public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) { 45 return new DataSourceTransactionManager(dataSource); 46 } 47 48 @Bean(name = "mysqlSqlSessionFactory") 49 @Primary 50 public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception { 51 final SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 52 域名ataSource(dataSource); 53 域名apperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/com/spring/login/mapper/mysql/*.xml")); 54 域名域名iguration configuration = new 域名域名iguration(); 55 域名apUnderscoreToCamelCase(true); 56 域名allSettersOnNulls(true); 57 域名ogImpl(域名域名域名s); 58 域名onfiguration(configuration); 59 return 域名bject(); 60 } 61 62 @Bean(name = "mysqlSqlSessionTemplate") 63 @Primary 64 public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 65 return new SqlSessionTemplate(sqlSessionFactory); 66 } 67 }
oracle连接数据源
1 package 域名域名ig; 2 3 import 域名域名dDataSource; 4 import 域名域名essionFactory; 5 import 域名域名essionFactoryBean; 6 import 域名域名essionTemplate; 7 import 域名域名erScan; 8 import 域名域名域名ifier; 9 import 域名域名域名e; 10 import 域名域名; 11 import 域名域名iguration; 12 import 域名.域名MatchingResourcePatternResolver; 13 import 域名.域名SourceTransactionManager; 14 15 import 域名Source; 16 17 @SuppressWarnings("all") 18 @Configuration 19 @MapperScan(basePackages = "域名域名le", sqlSessionTemplateRef = "oracleSqlSessionTemplate") 20 public class OracleDataSourceConfig { 21 @Value("${域名}") 22 private String url; 23 24 @Value("${域名name}") 25 private String user; 26 27 @Value("${域名word}") 28 private String password; 29 30 @Value("${域名er-class-name}") 31 private String driverClass; 32 33 @Bean(name = "oracleDataSource") 34 public DataSource oracleDataSource() { 35 DruidDataSource dataSource = new DruidDataSource(); 36 域名riverClassName(driverClass); 37 域名rl(url); 38 域名sername(user); 39 域名assword(password); 40 return dataSource; 41 } 42 43 @Bean(name = "oracleTransactionManager") 44 public DataSourceTransactionManager oracleTransactionManager() { 45 return new DataSourceTransactionManager(oracleDataSource()); 46 } 47 48 @Bean(name = "oracleSqlSessionFactory") 49 public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource oracleDataSource) throws Exception { 50 final SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); 51 域名ataSource(oracleDataSource); 52 域名apperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:域名域名le/*.xml")); 53 域名域名iguration configuration = new 域名域名iguration(); 54 域名apUnderscoreToCamelCase(true); 55 域名allSettersOnNulls(true); 56 域名ogImpl(域名域名域名s); 57 域名onfiguration(configuration); 58 return 域名bject(); 59 } 60 61 @Bean(name = "oracleSqlSessionTemplate") 62 public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { 63 return new SqlSessionTemplate(sqlSessionFactory); 64 } 65 }
其他的调用和单数据源一致我这里就不多讲了,后面在启动类上加载注入需要的文件
1 package 域名ng; 2 3 import 域名域名erScan; 4 import 域名.SpringApplication; 5 import 域名.域名ngBootApplication; 6 import 域名.域名SourceAutoConfiguration; 7 import 域名域名roller; 8 import 域名.域名estMapping; 9 10 /** 11 * 表示取消数据源的自动配置 12 */ 13 @Controller 14 @RequestMapping("/") 15 @SpringBootApplication(exclude = {域名s},scanBasePackages = {"com.*"}) 16 @MapperScan({"域名域名l"}) 17 public class WebApplication { 18 19 public static void main(String[] args) { 20 域名(域名s, args); 21 域名tln("项目启动成功!"); 22 } 23 24 }
数据库查询信息
Mybatis查询结果