项目架构
1.导入相关依赖
1
2 org.springframework.boot
3 spring-boot-starter-web
4
5
6
7 org.springframework.boot
8 spring-boot-starter-test
9 test
10
11
12
13
14 org.springframework.boot
15 spring-boot-starter-freemarker
16
17
18
19
20 mysql
21 mysql-connector-java
22
23
24
25
26 org.mybatis.spring.boot
27 mybatis-spring-boot-starter
28 1.1.1
29
2.application.properties
1 ## test1 数据源配置2 #spring.datasource.test1.driverClassName=com.mysql.jdbc.Driver3 #spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf84 #spring.datasource.test1.username=root5 #spring.datasource.test1.password=1236
7
8 ## test2 数据源配置9 #spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver10 #spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf811 #spring.datasource.test2.username=root12 #spring.datasource.test2.password=123
3.创建datasource包 下TestMyBatisConfig1
1 @Configuration2 @MapperScan(basePackages = "cn.happy.test1", sqlSessionFactoryRef = "test1SqlSessionFactory")3 public classDataSource1Config {4 @Bean(name = "test1DataSource")5 @ConfigurationProperties(prefix = "spring.datasource.test1")6 @Primary7 publicDataSource testDataSource() {8 returnDataSourceBuilder.create().build();9 }10
11 @Bean(name = "test1SqlSessionFactory")12 @Primary13 public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throwsException {14 SqlSessionFactoryBean bean = newSqlSessionFactoryBean();15 bean.setDataSource(dataSource);16 //读取mybatis小配置文件17 //bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
18 returnbean.getObject();19 }20
21 @Bean(name = "test1TransactionManager")22 @Primary23 public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {24 return newDataSourceTransactionManager(dataSource);25 }26
27 @Bean(name = "test1SqlSessionTemplate")28 @Primary29 public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throwsException {30 return newSqlSessionTemplate(sqlSessionFactory);31 }32 }
@Primary注解标识默认使用的数据源
如果不加启动会报如下错误:意思是有两个数据源 不知需要使用哪个数据源
3.创建datasource包 下TestMyBatisConfig2
1 @Configuration2 @MapperScan(basePackages = "cn.happy.test2", sqlSessionFactoryRef = "test2SqlSessionFactory")3 public classDataSource2Config {4 @Bean(name = "test2DataSource")5 @ConfigurationProperties(prefix = "spring.datasource.test2")6 publicDataSource testDataSource() {7 returnDataSourceBuilder.create().build();8 }9
10 @Bean(name = "test2SqlSessionFactory")11 public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throwsException {12 SqlSessionFactoryBean bean = newSqlSessionFactoryBean();13 bean.setDataSource(dataSource);14 //读取mybatis小配置文件15 //bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
16 returnbean.getObject();17 }18
19 @Bean(name = "test2TransactionManager")20 public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {21 return newDataSourceTransactionManager(dataSource);22 }23
24 @Bean(name = "test2SqlSessionTemplate")25 public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throwsException {26 return newSqlSessionTemplate(sqlSessionFactory);27 }28
29 }
4.entity层
1 /**
2 * author: 刘涛3 *4 * @create 2018-04-04 11:185 */
6 public classBook {7
8 privateInteger bookid;9
10 privateString bookname;11
12 privateInteger bookprice;13
14 get set省略。。15 }
5.test1下的dao中BookMapperTest1
1 public interfaceBookMapperTest1 {2
3 @Select("select * from book where bookname=#{bookname}")4 public Book findByName(@Param("bookname") String bookname);5
6 @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")7 public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice);8
9 @Select("select * from book")10 public Book findBook(@Param("Book") Book book);11 }
6.test2下的dao中BookMapperTest2
1 public interfaceBookMapperTest2 {2
3 @Select("select * from book where bookname=#{bookname}")4 public Book findByName(@Param("bookname") String bookname);5
6 @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")7 public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice);8 }
7.controller层
@RestControllerpublic classBookController {
@AutowiredprivateBookMapperTest1 bookMapperTest1;
@AutowiredprivateBookMapperTest2 bookMapperTest2;
@RequestMapping("insert001")publicString insert001(String bookname,Double bookprice){
bookMapperTest1.insertBook(bookname,bookprice);return "success";
}
@RequestMapping("insert002")publicString insert002(String bookname,Double bookprice){
bookMapperTest2.insertBook(bookname,bookprice);return "success";
}
}
8.启动项目
test1数据库
test2数据库