Spring Boot与Mybatis多数据源
Spring Boot与Mybatis多数据源
Demo
项目结构
源码
Application
package com.supremepole.b02springbootmultimybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class B02SpringBootMultiMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(B02SpringBootMultiMybatisApplication.class, args);
}
}
WebsiteController
package com.supremepole.b02springbootmultimybatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author CodeCoderCoding
*/
@RestController
public class WebsiteController {
@Autowired
WebsiteService websiteService;
@GetMapping("/add-website")
public void addWebsite(){
Website website=new Website();
website.setId(1);
website.setName("supremepole");
website.setUrl("https://cs.supremepole.com");
websiteService.addWebsite(website);
Website website2=new Website();
website2.setId(2);
website2.setName("supremepole interview");
website2.setUrl("https://interview.supremepole.com");
websiteService.addWebsite(website2);
}
@GetMapping("/get-website")
public Website getWebsite(){
return websiteService.getWebsiteById(1);
}
@GetMapping("/get-website2")
public Website getWebsite2(){
return websiteService.getWebsiteById2(1);
}
@GetMapping("/update-website")
public void updateWebsite(){
Website website=new Website();
website.setId(1);
website.setName("supremepole algorithm");
website.setUrl("https://algorithm.supremepole.com");
websiteService.updateWebsite(website);
}
@GetMapping("/delete-website")
public void deleteWebsite(){
websiteService.deleteWebsiteById(2);
}
}
WebsiteService
package com.supremepole.b02springbootmultimybatis;
import com.supremepole.b02springbootmultimybatis.mapper1.WebsiteMapper;
import com.supremepole.b02springbootmultimybatis.mapper2.WebsiteMapper2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author CodeCoderCoding
*/
@Service
public class WebsiteService {
@Autowired
WebsiteMapper websiteMapper;
@Autowired
WebsiteMapper2 websiteMapper2;
public int addWebsite(Website website) {
return websiteMapper.addWebsite(website);
}
public int updateWebsite(Website website) {
return websiteMapper.updateWebsite(website);
}
public int deleteWebsiteById(Integer id) {
return websiteMapper.deleteWebsiteById(id);
}
public Website getWebsiteById(Integer id) {
return websiteMapper.getWebsiteById(id);
}
public List<Website> getAllWebsites() {
return websiteMapper.getAllWebsites();
}
public Website getWebsiteById2(Integer id) {
return websiteMapper2.getWebsiteById(id);
}
}
DataSourceConfig
package com.supremepole.b02springbootmultimybatis.config;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* CodeCoderCoding
*/
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.one")
DataSource dsOne() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.two")
DataSource dsTwo() {
return DruidDataSourceBuilder.create().build();
}
}
MyBatisConfigOne
package com.supremepole.b02springbootmultimybatis.config;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* CodeCoderCoding
*/
@Configuration
@MapperScan(value = "com.supremepole.b02springbootmultimybatis.mapper1", sqlSessionFactoryRef = "sqlSessionFactoryBean1")
public class MyBatisConfigOne {
@Autowired
@Qualifier("dsOne")
DataSource dsOne;
@Bean
SqlSessionFactory sqlSessionFactoryBean1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dsOne);
return factoryBean.getObject();
}
@Bean
SqlSessionTemplate sqlSessionTemplate1() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryBean1());
}
}
MyBatisConfigTwo
package com.supremepole.b02springbootmultimybatis.config;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* CodeCoderCoding
*/
@Configuration
@MapperScan(value = "com.supremepole.b02springbootmultimybatis.mapper2", sqlSessionFactoryRef = "sqlSessionFactoryBean2")
public class MyBatisConfigTwo {
@Autowired
@Qualifier("dsTwo")
DataSource dsTwo;
@Bean
SqlSessionFactory sqlSessionFactoryBean2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dsTwo);
return factoryBean.getObject();
}
@Bean
SqlSessionTemplate sqlSessionTemplate2() throws Exception {
return new SqlSessionTemplate(sqlSessionFactoryBean2());
}
}
WebsiteMapper.java
package com.supremepole.b02springbootmultimybatis.mapper1;
import com.supremepole.b02springbootmultimybatis.Website;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author CodeCoderCoding
*/
public interface WebsiteMapper {
int addWebsite(Website website);
int deleteWebsiteById(Integer id);
int updateWebsite(Website website);
Website getWebsiteById(Integer id);
List<Website> getAllWebsites();
}
WebsiteMapper.xml
<?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.supremepole.b02springbootmultimybatis.mapper1.WebsiteMapper">
<insert id="addWebsite" parameterType="com.supremepole.b02springbootmultimybatis.Website">
INSERT INTO website(id,name,url) VALUES (#{id},#{name},#{url})
</insert>
<delete id="deleteWebsiteById" parameterType="int">
DELETE FROM website WHERE id=#{id}
</delete>
<update id="updateWebsite" parameterType="com.supremepole.b02springbootmultimybatis.Website">
UPDATE website set name=#{name},url=#{url} WHERE id=#{id}
</update>
<select id="getWebsiteById" parameterType="int" resultType="com.supremepole.b02springbootmultimybatis.Website">
SELECT * FROM website WHERE id=#{id}
</select>
<select id="getAllWebsites" resultType="com.supremepole.b02springbootmultimybatis.Website">
SELECT * FROM website
</select>
</mapper>
WebsiteMapper2.java
package com.supremepole.b02springbootmultimybatis.mapper2;
import com.supremepole.b02springbootmultimybatis.Website;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author CodeCoderCoding
*/
public interface WebsiteMapper2 {
int addWebsite(Website website);
int deleteWebsiteById(Integer id);
int updateWebsite(Website website);
Website getWebsiteById(Integer id);
List<Website> getAllWebsites();
}
WebsiteMapper2.xml
<?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.supremepole.b02springbootmultimybatis.mapper2.WebsiteMapper2">
<insert id="addWebsite" parameterType="com.supremepole.b02springbootmultimybatis.Website">
INSERT INTO website(id,name,url) VALUES (#{id},#{name},#{url})
</insert>
<delete id="deleteWebsiteById" parameterType="int">
DELETE FROM website WHERE id=#{id}
</delete>
<update id="updateWebsite" parameterType="com.supremepole.b02springbootmultimybatis.Website">
UPDATE website set name=#{name},url=#{url} WHERE id=#{id}
</update>
<select id="getWebsiteById" parameterType="int" resultType="com.supremepole.b02springbootmultimybatis.Website">
SELECT * FROM website WHERE id=#{id}
</select>
<select id="getAllWebsites" resultType="com.supremepole.b02springbootmultimybatis.Website">
SELECT * FROM website
</select>
</mapper>
application.properties
server.port=8081
# data source 1
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.one.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.one.url=jdbc:mysql://localhost:3306/spring-demo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
spring.datasource.one.username=root
spring.datasource.one.password=123456
# data source 2
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.two.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.two.url=jdbc:mysql://localhost:3306/spring-demo2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
spring.datasource.two.username=root
spring.datasource.two.password=123456