一、在mysql中创建两个数据库 ,并在两个库中创建相同的表
二、添加依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency>
三、创建两个MyBatis配置
1.MyBatisConfigOne
@Configuration @MapperScan(value = "com.kxg.springboot.mapper",sqlSessionFactoryRef = "sqlSessionFactoryBeanOne") public class MyBatisConfigOne { @Autowired @Qualifier("dsOne") DataSource dsOne; @Bean SqlSessionFactory sqlSessionFactoryBeanOne()throws Exception{ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dsOne); return factoryBean.getObject(); } @Bean SqlSessionTemplate sqlSessionTemplateOne() throws Exception{ return new SqlSessionTemplate(sqlSessionFactoryBeanOne()); } }
2.MyBatisConfigTwo
@Configuration @MapperScan(value = "com.kxg.springboot.mappertwo",sqlSessionFactoryRef = "sqlSessionFactoryBeanTwo") public class MyBatisConfigTwo { @Autowired @Qualifier("dsTwo") DataSource dsTwo; @Bean SqlSessionFactory sqlSessionFactoryBeanTwo()throws Exception{ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dsTwo); return factoryBean.getObject(); } @Bean SqlSessionTemplate sqlSessionTemplateTwo() throws Exception{ return new SqlSessionTemplate(sqlSessionFactoryBeanTwo()); } }
三、创建Mapper
1.BookMapper
@Mapper public interface BookMapper { int addBook(Book book); int deleteBookById(Integer id); int updateBookById(Book book); Book getBookById(Integer id); 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"> <!-- namespace 表示命名空间 --> <mapper namespace="com.kxg.springboot.mapper.BookMapper"> <insert id="addBook" parameterType="com.kxg.springboot.po.Book"> INSERT INTO t_book(name,author) VALUES(#{name},#{author}) </insert> <delete id="deleteBookById" parameterType="Integer"> DELETE from t_book where id=#{id} </delete> <update id="updateBookById" parameterType="com.kxg.springboot.po.Book"> UPDATE t_book set name=#{name},author=#{author} where id=#{id} </update> <select id="getBookById" parameterType = "Integer" resultType="com.kxg.springboot.po.Book"> select * from t_book where id =#{id} </select> <select id="getAllBooks" resultType="com.kxg.springboot.po.Book"> select * from t_book </select> </mapper>
2.BookMapperTwo
public interface BookMapperTwo { 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"> <!-- namespace 表示命名空间 --> <mapper namespace="com.kxg.springboot.mappertwo.BookMapperTwo"> <select id="getAllBooks" resultType="com.kxg.springboot.po.Book"> select * from t_book </select> </mapper>
四、创建Service
public interface BookService { default int addBook(Book book){ return -1; } default int updateBook(Book book){ return -1; } default int deleteBookById(Integer id) { return -1; } @Nullable default Book getBookById(Integer id){ return null; } @Nullable default List<Book> getAllBooks(){ return null; } }
@Service public class BookServiceOne implements BookService{ @Autowired BookMapper bookMapper; public int addBook(Book book) { return bookMapper.addBook(book); } public int updateBook(Book book) { return bookMapper.updateBookById(book); } public int deleteBookById(Integer id) { return bookMapper.deleteBookById(id); } public Book getBookById(Integer id) { return bookMapper.getBookById(id); } public List<Book> getAllBooks() { return bookMapper.getAllBooks(); } }
@Service public class BookServiceTwo { @Autowired BookMapperTwo bookMapper; public List<Book> getAllBooks() { return bookMapper.getAllBooks(); } }
五、创建Controller
@RestController @RequestMapping("/book") public class BookController { @Autowired BookServiceOne bookService; @Autowired BookServiceTwo bookServiceTwo; @GetMapping("/test") public String test1() { List<Book> books1 = bookService.getAllBooks(); List<Book> books2 = bookServiceTwo.getAllBooks(); return books1.toString() + books1.toString(); } }
六、测试
实体类在https://blog.csdn.net/kangguang/article/details/104278674中创建