创建maven项目并添加依赖 我这里用的是SQLserver mybatis
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies>
application.properties配置文件中添加 多个数据源
spring.datasource.test1.jdbcUrl=jdbc:sqlserver://ip:port;DatabaseName=xxglptcsDB spring.datasource.test1.username=sa spring.datasource.test1.password=123 spring.datasource.test1.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.test2.jdbcUrl=jdbc:sqlserver://ip:port;DatabaseName=xxglptcsDB1 spring.datasource.test2.username=sa spring.datasource.test2.password=123 spring.datasource.test2.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
项目结构
启动类
这里扫包除了controller层 service层 还要添加DataSource包 (博主因为没扫这个包找了两个小时的错……)
在网上找了两个小时也没有一个管用的 ,然后博主突然想到这配置文件跟数据源配置类好像也没有关联,于是就添加了扫DataSource包
如果没有扫数据源配置包 可能会报这个错Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
或者'url' attribute is not specified and no embedded datasource could be configured.
package com.vhukze.App; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; /** * @author zsz * @version * @创建时间:2019年9月2日 下午1:08:00 */ @SpringBootApplication @ComponentScan(basePackages= {"com.vhukze.controller","com.vhukze.test1.service","com.vhukze.test2.service","com.vhukze.datasource"}) @MapperScan({"com.vhukze.test1.mapper","com.vhukze.test2.mapper"}) public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
数据源配置类
test1
package com.vhukze.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.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager; /** * @author zsz * @version * @创建时间:2019年9月2日 上午11:19:09 */ @Configuration @MapperScan(basePackages="com.vhukze.test1.mapper",sqlSessionFactoryRef="test1SqlSessionFactory")//扫描对应的dao包 public class Datasource1 { @Bean(name="test1DataSource") @ConfigurationProperties(prefix="spring.datasource.test1")//这里是配置文件里定义的属性名 @Primary //默认优先使用 public DataSource testDataSource() { return DataSourceBuilder.create().build(); } //@Qualifier 从spring容器中查找名为test1DataSource的bean @Bean(name="test1SqlSessionFactory") @Primary //默认优先使用 public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //需要加载配置文件的话 // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:*.xml")); return bean.getObject(); } /** * @author zsz * @编辑时间 2019年9月2日上午11:45:07 * @编辑说明 事务管理 * @param dataSource * @return */ @Bean(name="test1TuansactionManager") @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); } }
test2
package com.vhukze.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.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager; /** * @author zsz * @version * @创建时间:2019年9月2日 上午11:53:15 */ @Configuration @MapperScan(basePackages="com.vhukze.test2.mapper",sqlSessionFactoryRef="test2SqlSessionFactory")//扫描对应的dao包 public class Datasource2 { @Bean(name="test2DataSource") @ConfigurationProperties(prefix="spring.datasource.test2")//这里是配置文件里定义的属性名 public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name="test2SqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //需要加载配置文件的话 // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:*.xml")); return bean.getObject(); } /** * @author zsz * @编辑时间 2019年9月2日上午11:54:08 * @编辑说明 事务管理 * @param dataSource * @return */ @Bean(name="test2TuansactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } @Bean(name="test2SqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } }
到此所有的配置都完成了
接下来就是走一下流程了
controller层
@RestController public class UserController { @Autowired private User1Service service1; @Autowired private User2Service service2; @RequestMapping("index") public String index() { service1.insert(); service2.insert(); return "success"; } }
dao层(1和2都是一样的 这里贴一个就可以了)
@Repository public interface User1Mapper{ @Insert("insert into user_demo values('1','1')") public void insert(); }
启动项目访问映射路径