SpringBoot 整合MyBatis
依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
配置数据库:
spring:
datasource:
boot:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/boot?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
boot2:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/boot2?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
配置数据源:
创建DataSourceConfig配置数据源,根据application.yml中的配置生成两个数据源
@Configuration
public class DataSourceConfig {
@Primary
@Bean
@ConfigurationProperties("spring.datasource.boot")
DataSource dsOne() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.boot2")
DataSource dsTwo() {
return DruidDataSourceBuilder.create().build();
}
}
创建MyBatis配置:
@Configuration
@MapperScan(value = "com.xc.xcspringboot.mapper", sqlSessionFactoryRef = "sqlSessionFactoryBean1")
public class MyBatisConfigOne {
@Autowired
@Qualifier("dsOne")
DataSource dsOne;
@Bean
SqlSessionFactory sqlSessionFactoryBean1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dsOne);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
return factoryBean.getObject();
}
@Bean
SqlSessionTemplate sqlSessionTemplate1() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryBean1());
}
}
@Configuration
@MapperScan(value = "com.xc.xcspringboot.mapper2", sqlSessionFactoryRef = "sqlSessionFactoryBean2")
public class MyBatisConfigTwo {
@Autowired
@Qualifier("dsTwo")
DataSource dsTwo;
@Bean
SqlSessionFactory sqlSessionFactoryBean2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dsTwo);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/**/*.xml"));
return factoryBean.getObject();
}
@Bean
SqlSessionTemplate sqlSessionTemplate2() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryBean2());
}
}
创建Mapper:
public interface BookMapper {
List<Book> getAllBooks();
}
public interface BookMapper2 {
List<Book> getAllBooks();
}
<mapper namespace="com.xc.xcspringboot.mapper.BookMapper">
<select id="getAllBooks" resultType="com.xc.xcspringboot.model.Book">
select *
from book;
</select>
</mapper>
<mapper namespace="com.xc.xcspringboot.mapper2.BookMapper2">
<select id="getAllBooks" resultType="com.xc.xcspringboot.model.Book">
select *
from book;
</select>
</mapper>
创建Controller:
@RestController
public class BookController {
@Autowired
BookMapper bookMapper;
@Autowired
BookMapper2 bookMapper2;
@GetMapping("/test1")
public void test1() {
List<Book> books1 = bookMapper.getAllBooks();
List<Book> books2 = bookMapper2.getAllBooks();
System.out.println("books1:"+books1);
System.out.println("books2:"+books2);
}
}
文章来源: Spring Boot+Vue全栈开发实战 5.4 多数据源