1、pom.xml中添加依赖
<!--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>
</dependency>
2、在application.yml中配置数据源信息
spring:
datasource:
#数据源1
one:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/xice?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
#数据源2
two:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/codestock?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
3、配置DataSource
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.one")
DataSource dataSourceOne(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.two")
DataSource dataSourceTwo(){
return DruidDataSourceBuilder.create().build();
}
}
4、配置mybatis,主要提供SqlSessionFactory实例和SqlSessionTemplate实例
@Configuration
@MapperScan(value ="com.xice.mapper1",sqlSessionFactoryRef = "sqlSessionFactoryBean1")
public class MybatisConfigOne {
@Autowired
@Qualifier("dataSourceOne")
DataSource dataSourceOne;
@Bean
SqlSessionFactory sqlSessionFactoryBean1() throws Exception{
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSourceOne);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/xice/mapper1/*.xml"));
return sqlSessionFactory.getObject();
}
@Bean
SqlSessionTemplate sqlSessionTemplate1() throws Exception{
return new SqlSessionTemplate(sqlSessionFactoryBean1());
}
}
@Configuration
@MapperScan(value = "com.xice.mapper2",sqlSessionFactoryRef = "sqlSessionFactoryBean2")
public class MybatisConfigTwo {
@Autowired
@Qualifier("dataSourceTwo")
DataSource dataSourceTwo;
@Bean
SqlSessionFactory sqlSessionFactoryBean2() throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSourceTwo);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/xice/mapper2/*.xml"));
return sqlSessionFactory.getObject();
}
@Bean
SqlSessionTemplate sqlSessionTemplate2() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryBean2());
}
}
5、分别在com.xice下建mapper1和mapper2文件夹,并创建BookMapper.java、BookMapper.xml和BookMapper2.java、BookMapper.xml文件
@Mapper
public interface BookMapper {
int addBook(Book book);//添加book
int deleteBookById(Integer id);//根据id删除book
int updateBook(Book book);//根据id更新book
Book getBookById(Integer id);//根据id获取book
List<Book> getAllBooks();
}
<?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.xice.mapper1.BookMapper">
<insert id="addBook" parameterType="com.xice.model.Book">
insert into book(name,author) values (#{name},#{author})
</insert>
<delete id="deleteBookById" parameterType="int" >
delete from book where id=#{id}
</delete>
<update id="updateBook" parameterType="com.xice.model.Book" >
update book set name=#{name},author=#{author} where id=#{id}
</update>
<select id="getBookById" parameterType="com.xice.model.Book" resultType="com.xice.model.Book">
select * from book where id=#{id}
</select>
<select id="getAllBooks" resultType="com.xice.model.Book">
select * from book
</select>
</mapper>
6、加入service层,注入想要使用的数据源的mapper接口
@Service
public class BookService {
@Autowired
BookMapper bookMapper;
public int addBook(Book book){
return bookMapper.addBook(book);
}
/**
* 获取所有book列表
* @return
*/
public List<Book> getAllBooks(){
return bookMapper.getAllBooks();
}
/**
* 根据id获取book
* @param id
* @return
*/
public Book getBookById(Integer id){
return bookMapper.getBookById(id);
}
/**
* 根据id更新指定book
*/
public int updateBook(Book book){
return bookMapper.updateBook(book);
}
/**
* 删除指定id的book
* @param id
* @return
*/
public int deleteBookById(Integer id){
return bookMapper.deleteBookById(id);
}
}
7、在Controller中调用service,即可操作不同的数据源
@RestController
public class BookController {
@Autowired
BookService bookService;
/**
* 获取所有book
* @return
*/
@RequestMapping("getAllBooks")
public String getAllBooks(){
List<Book> books = bookService.getAllBooks();
return books.toString();
}
}
8、终于写完了,运行一下
啥???找不到???我不是都已经配置好了吗???
(1)在MybatisConfigOne.java中
@MapperScan(value ="com.xice.mapper1",sqlSessionFactoryRef = "sqlSessionFactoryBean1")
(2)配置里mapper接口的位置,在下面的
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/xice/mapper1/*.xml"));
中配置了mapper.xml的路径,为毛找不到???
(3)在application.yml中指定
mybatis:
mapper-locations: classpath:com/xice/mapper*/*.xml
type-aliases-package: com.xice.mapper*
然并卵!!!
(4)在pom.xml中添加
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
OK,终于成功了!