SpringBoot MyBatis使用多个数据源
1.src/main/resources/application.properties
#服务端口 server.port=8080 #应用名称 spring.application.name=appdemo spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html logging.path=./ #邮件配置 spring.mail.host=mail.qq.com spring.mail.username=test.qq.com spring.mail.password=mail_password spring.mail.default-encoding=utf-8 spring.mail.from=test.qq.com #oracle链接 spring.ds_oracle.driver-class-name=oracle.jdbc.driver.OracleDriver spring.ds_oracle.url=jdbc:oracle:thin:@192.168.1.10:1521/orc spring.ds_oracle.username=username spring.ds_oracle.password=password spring.ds_oracle.max-wait: 10000 spring.ds_oracle.max-active: 30 spring.ds_oracle.test-on-borrow: true spring.ds_oracle.test-while-idle: true spring.ds_oracle.validation-query: select 1 spring.ds_oracle.max-idle: 5 #mysql链接 spring.ds_mysql.driver-class-name=com.mysql.jdbc.Driver spring.ds_mysql.url=jdbc:mysql://192.168.1.10:3306/appdemo?characterEncoding=utf-8 spring.ds_mysql.username=username spring.ds_mysql.password=password spring.ds_mysql.max-wait: 10000 spring.ds_mysql.max-active: 30 spring.ds_mysql.test-on-borrow: true spring.ds_mysql.test-while-idle: true spring.ds_mysql.validation-query: select 1 spring.ds_mysql.max-idle: 5 #sqlserver链接 spring.ds_mssql.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.ds_mssql.url=jdbc:sqlserver://192.168.1.10:1433;DatabaseName=appdemo spring.ds_mssql.username=username spring.ds_mssql.password=password spring.ds_mssql.max-wait: 10000 spring.ds_mssql.max-active: 30 spring.ds_mssql.test-on-borrow: true spring.ds_mssql.test-while-idle: true spring.ds_mssql.validation-query: select 1 spring.ds_mssql.max-idle: 5
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com mydemo 1.0-SNAPSHOT jar mydemo org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 1.8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java org.springframework.boot spring-boot-starter-mail com.oracle ojdbc6 11.1.0.7.0 org.springframework.boot spring-boot-maven-plugin repackage maven-resources-plugin utf-8 true src/main/resources true
3.配置数据源
src/main/java/com/mydemo/conf/MSSQLDataSourceConfig.java
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.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.mydemo.dao.mssql", sqlSessionTemplateRef = "mssqlSessionTemplate") public class MSSQLDataSourceConfig { @Bean(name = "mssqlDataSource") @ConfigurationProperties(prefix = "spring.ds_mssql") public DataSource mssqlDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "mssqlSessionFactory") public SqlSessionFactory mssqlSessionFactory(@Qualifier("mssqlDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mssql/*.xml")); return bean.getObject(); } @Bean(name = "mssqlTransactionManager") public DataSourceTransactionManager mssqlTransactionManager(@Qualifier("mssqlDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "mssqlSessionTemplate") public SqlSessionTemplate mssqlSessionTemplate(@Qualifier("mssqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
src/main/java/com/mydemo/conf/MySqlDataSourceConfig.java
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.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.mydemo.dao.mysql", sqlSessionTemplateRef = "mysqlSessionTemplate") public class MySqlDataSourceConfig { @Primary @Bean(name = "mysqlDataSource") @ConfigurationProperties(prefix = "spring.ds_mysql") public DataSource mysqlDataSource() { return DataSourceBuilder.create().build(); } @Primary @Bean(name = "mysqlSessionFactory") public SqlSessionFactory mysqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql/*.xml")); return bean.getObject(); } @Primary @Bean(name = "mysqlTransactionManager") public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Primary @Bean(name = "mysqlSessionTemplate") public SqlSessionTemplate mysqlSessionTemplate(@Qualifier("mysqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
src/main/java/com/mydemo/conf/OracleDataSourceConfig.java
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.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.mydemo.dao.oracle", sqlSessionTemplateRef = "oracleSessionTemplate") public class OracleDataSourceConfig { @Bean(name = "oracleDataSource") @ConfigurationProperties(prefix = "spring.ds_oracle") public DataSource oracleDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "oracleSessionFactory") public SqlSessionFactory oracleSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/oracle/*.xml")); return bean.getObject(); } @Bean(name = "oracleTransactionManager") public DataSourceTransactionManager oracleTransactionManager(@Qualifier("oracleDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "oracleSessionTemplate") public SqlSessionTemplate oracleSessionTemplate(@Qualifier("oracleSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
4.创建映射xml
分别创建各链接对应的mybatis xml文件
src/main/resources/mapper/mysql/ProductMapper.xml
src/main/resources/mapper/oracle/MemberMapper.xml
src/main/resources/mapper/mssql/AdminMapper.xml
示例mssql对应的xml
<?xml version="1.0" encoding="UTF-8" ?> DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mydemo.dao.mssql.AdminMapper"> <resultMap id="BaseResultMap" type="com.mydemo.entities.Admin"> <id column="id" property="id" jdbcType="VARCHAR"/> <result column="name" property="name" jdbcType="VARCHAR"/> <result column="email" property="email" jdbcType="VARCHAR"/> <result column="joindate" property="joindate" jdbcType="VARCHAR"/> resultMap> <select id="findByEmail" parameterType="String" resultMap="BaseResultMap"> select top 1 id ,name, email ,joindate from admin select> mapper>
5.创建映射类
分别创建各链接对应的mybatis mapper类
src/main/com/mydemo/dao/mysql/ProductMapper.java
src/main/com/mydemo/dao/oracle/MemberMapper.java
src/main/com/mydemo/dao/mssql/AdminMapper.java
示例mssql对应的mapper类
import com.mydemo.entities.Admin; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface AdminMapper { Admin findByEmail(String email); }
6.在controller或service层使用
@RestController public class MailController { @Autowired ProductMapper productMapper; @Autowired MemberMapper memberMapper; @Autowired AdminMapper adminMapper; @Autowired ExamMapper examMapper; @Autowired private JavaMailSender javaMailSender; //发送邮件测试 @RequestMapping("/mail/test") public void test() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("昵称"); message.setTo("zhang3@qq.com"); message.setSubject("这是标题"); message.setText("这是内容"); javaMailSender.send(message); } @RequestMapping("/db/test") @ResponseBody public Object test2() { Admin admin = adminMapper.findByEmail("test@qq.com") } }
7.启动项目
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class); } }