Springboot整合Mybatis,连接多个数据库(Mysql+Oracle)
Springboot整合Mybatis,连接多个数据库,其实和mybatis连接单个数据库基本一致,只是在对应包下面引入对应的数据源即可,下面是maven依赖,需要注意的是mysql使用的版本
1 <dependencies> 2 <dependency> 3 <groupId>com.oracle.database.jdbc</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>5.1.30</version> 11 </dependency> 12 <!--Druid依赖代码 收藏代码--> 13 <dependency> 14 <groupId>com.alibaba</groupId> 15 <artifactId>druid</artifactId> 16 <version>0.2.15</version> 17 </dependency> 18 </dependencies>
application.properties配置文件
1 #Spring boot视图配置 2 spring.mvc.view.prefix=/WEB-INF/view/ 3 spring.mvc.view.suffix=.jsp 4 #静态文件访问配置 5 spring.mvc.static-path-pattern=/static/** 6 7 #加载Mybatis的xml 在:resources下 8 mybatis.mapper-locations=classpath*:/com/spring/login/mapper/mysql/*.xml,classpath*:/com/spring/login/mapper/oracle/*.xml
9 10 #设置运行的端口 11 server.port=8080 12 13 #配置字符编码 14 server.servlet.encoding.enabled=true 15 server.servlet.encoding.force=true 16 server.servlet.encoding.charset=UTF-8 17 18 server.tomcat.uri-encoding=UTF-8 19 20 #取消thymeleaf 严格检查 21 spring.thymeleaf.mode=LEGACYHTML5 22 23 #连接Oracle数据库 24 oracle.datasource.driver-class-name= oracle.jdbc.driver.OracleDriver 25 oracle.datasource.url= jdbc:oracle:thin:@你的地址:1521/orcl 26 oracle.datasource.username=账号 27 oracle.datasource.password=密码 28 29 #配置连接mySql数据库 30 spring.datasource.url=jdbc:mysql://你的地址:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8 31 spring.datasource.username=账号 32 spring.datasource.password=密码 33 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
看一下目录结构,既,不同的数据库我放在了DAO里不同的包里了,主要是为了区分
在config里新建了两个config类,分别是配置mysql以及oracle数据源使用的,需要注意的是,两个或者多个数据源时,一定要制定一个(只有一个)为主数据源,下面时两个数据源代码
1 package com.spring.login.config; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import org.apache.ibatis.session.SqlSessionFactory; 5 import org.mybatis.spring.SqlSessionFactoryBean; 6 import org.mybatis.spring.SqlSessionTemplate; 7 import org.mybatis.spring.annotation.MapperScan; 8 import org.springframework.beans.factory.annotation.Qualifier; 9 import org.springframework.beans.factory.annotation.Value; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Configuration; 12 import org.springframework.context.annotation.Primary; 13 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 14 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 15 16 import javax.sql.DataSource; 17 18 @SuppressWarnings("all") 19 @Configuration 20 @MapperScan(basePackages = "com.spring.login.dao.mysql", sqlSessionTemplateRef = "mysqlSqlSessionTemplate") 21 public class MysqlDataSourceConfig { 22 @Value("${spring.datasource.url}") 23 private String url; 24 @Value("${spring.datasource.username}") 25 private String user; 26 @Value("${spring.datasource.password}") 27 private String password; 28 @Value("${spring.datasource.driver-class-name}") 29 private String driverClass; 30 31 @Bean(name = "mysqlDataSource") 32 @Primary 33 public DataSource mysqlDataSource() { 34 DruidDataSource dataSource = new DruidDataSource(); 35 dataSource.setUrl(url); 36 dataSource.setUsername(user); 37 dataSource.setPassword(password); 38 dataSource.setDriverClassName(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 bean.setDataSource(dataSource); 53 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/com/spring/login/mapper/mysql/*.xml")); 54 org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); 55 configuration.setMapUnderscoreToCamelCase(true); 56 configuration.setCallSettersOnNulls(true); 57 configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class); 58 bean.setConfiguration(configuration); 59 return bean.getObject(); 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 com.spring.login.config; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 import org.apache.ibatis.session.SqlSessionFactory; 5 import org.mybatis.spring.SqlSessionFactoryBean; 6 import org.mybatis.spring.SqlSessionTemplate; 7 import org.mybatis.spring.annotation.MapperScan; 8 import org.springframework.beans.factory.annotation.Qualifier; 9 import org.springframework.beans.factory.annotation.Value; 10 import org.springframework.context.annotation.Bean; 11 import org.springframework.context.annotation.Configuration; 12 import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 13 import org.springframework.jdbc.datasource.DataSourceTransactionManager; 14 15 import javax.sql.DataSource; 16 17 @SuppressWarnings("all") 18 @Configuration 19 @MapperScan(basePackages = "com.spring.login.dao.oracle", sqlSessionTemplateRef = "oracleSqlSessionTemplate") 20 public class OracleDataSourceConfig { 21 @Value("${oracle.datasource.url}") 22 private String url; 23 24 @Value("${oracle.datasource.username}") 25 private String user; 26 27 @Value("${oracle.datasource.password}") 28 private String password; 29 30 @Value("${oracle.datasource.driver-class-name}") 31 private String driverClass; 32 33 @Bean(name = "oracleDataSource") 34 public DataSource oracleDataSource() { 35 DruidDataSource dataSource = new DruidDataSource(); 36 dataSource.setDriverClassName(driverClass); 37 dataSource.setUrl(url); 38 dataSource.setUsername(user); 39 dataSource.setPassword(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 bean.setDataSource(oracleDataSource); 52 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/com/spring/login/mapper/oracle/*.xml")); 53 org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); 54 configuration.setMapUnderscoreToCamelCase(true); 55 configuration.setCallSettersOnNulls(true); 56 configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class); 57 bean.setConfiguration(configuration); 58 return bean.getObject(); 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 com.spring; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 10 /** 11 * 表示取消数据源的自动配置 12 */ 13 @Controller 14 @RequestMapping("/") 15 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class},scanBasePackages = {"com.*"}) 16 @MapperScan({"com.spring.login.mapper.mysql","com.spring.login.mapper.oracle"})17 public class WebApplication { 18 19 public static void main(String[] args) { 20 SpringApplication.run(WebApplication.class, args); 21 System.out.println("项目启动成功!"); 22 } 23 24 }
Mybatis查询mysql结果
Mybatis查询oracle结果
备注,如果有三个或者更多个数据源,@Bean(name = "mysqlDataSource"),这个name不相同即可
原创文章,转载请说明出处,谢谢合作