1.文件结构:
2.pom:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itmayido</groupId>
<artifactId>SpringBoot-data</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 必备web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql数据库连接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
3.application.properties:
server.port是设置端口
4.DataSource1Config.java:
package com.itmayidu.datasource;
import javax.sql.DataSource;
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.jdbc.datasource.DataSourceTransactionManager;
@Configuration//注册到springboot容器,相当于原来xml文件里的<beans>
//下面要进行扫包,目的是标清楚为谁添加的数据源,这样对应的包里函数执行数据库操作的时候,就知道要执行的数据库账号
//密码,表,以及事务处理之类的。
@MapperScan(basePackages= {"com.itmayidu.test01"},sqlSessionFactoryRef="test1SqlSessionFactory")
public class DataSource1Config {
@Bean(name="test1DataSource")//注入到这个容器
@ConfigurationProperties(prefix="spring.datasource.test1")//表示取application.properties配置文件中的前缀
@Primary#primary是设置优先,因为有多个数据源,在没有明确指定用哪个的情况下,会用带有primary的,这个注解必须有一个数据源要添加
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name="test1SqlSessionFactory")
@Primary
//@Qualifier("xxx")的含义是告诉他使用哪个DataSource
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name="test1TransactionManager")//配置事务
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
5.对应的mapper和service:
为了方便用了注解,也可以用mybatis写对应mapper的xml文件来执行更高级的数据库操作
下面附带事务回滚的测试:@Transactional(transactionManager。。。。)
6.Controller:
还是提一嘴吧,这个@RestController的意思是返回的是json数据,并不是jsp网页。
7.测试:
这时数据库里的test01表里就会多这一行数据,如果测试另一个数据源,那么数据库的另一表里就会多一行数据,这也就验证了,当我们调用不同的函数,进行不同的数据源操作时,数据库也确实做出了不同的更改,也就证明,多数据源验证成功。