包结构如下图所示
重要的依赖如下
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
数据库相关配置如下
spring:
application:
name: mybatis-multidatasource
datasource:
test1:
druid:
db-type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&noAccessToProcedureBodies=true
username: root
password:
initial-size: 20
min-idle: 20
max-active: 50
max-wait: 60000
validation-query: SELECT 1 FROM DUAL
test2:
druid:
db-type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&noAccessToProcedureBodies=true
username: root
password:
initial-size: 20
min-idle: 20
max-active: 50
max-wait: 60000
validation-query: SELECT 1 FROM DUAL
注意test1和tes2标记数据源的名称,在配置数据源的时候需要用到。
接下来多数据源配置类
数据源test1的配置类,在com.wpw.mybatisplus.multidatasource.config.datasource包下,其中
@MapperScan(basePackages = “com.wpw.mybatisplus.multidatasource.mapper.test1”)配置表示在该包下扫描mapper类
package com.wpw.mybatisplus.multidatasource.config.datasource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @Description: test1
* @Title:
* @author:
* @CreateDate: 2020-02-21 17:24
* @return
* @throws
*/
@Configuration
@MapperScan(basePackages = "com.wpw.mybatisplus.multidatasource.mapper.test1", sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class Test1DataSourceConfig {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1.druid")
public DataSource test1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test1SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
//SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "test1TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
同理,test2的配置类如下
package com.wpw.mybatisplus.multidatasource.config.datasource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @Description: test2
* @Title:
* @author:
* @CreateDate: 2020-02-21 17:24
* @return
* @throws
*/
@Configuration
@MapperScan(basePackages = "com.wpw.mybatisplus.multidatasource.mapper.test2", sqlSessionTemplateRef = "test2SqlSessionTemplate")
public class Test2DataSourceConfig {
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2.druid")
public DataSource test2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
//SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
Mapper如下,位置在数据源配置类的扫描路径下
数据源test1的mapper
package com.wpw.mybatisplus.multidatasource.mapper.test1;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wpw.mybatisplus.multidatasource.model.User;
public interface Test1Mapper extends BaseMapper<User> {
}
数据源test2的mapper
package com.wpw.mybatisplus.multidatasource.mapper.test2;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wpw.mybatisplus.multidatasource.model.User;
public interface Test2Mapper extends BaseMapper<User> {
}
实体类:
package com.wpw.mybatisplus.multidatasource.model;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @Description:
* @ClassName: User
* @CreateDate: 2020-02-28 23:59
* @author: wupengwei
*/
@Data
@TableName("t_user")
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private String name;
private Integer sex;
}
接下来进行测试,测试代码:
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class UsetMapperTest {
@Autowired
private Test1Mapper test1Mapper;
@Autowired
private Test2Mapper test2Mapper;
@Test
public void insertTest() {
User user1 = new User();
user1.setName("test1");
user1.setSex(1);
test1Mapper.insert(user1);
User user2 = new User();
user2.setName("test2");
user2.setSex(2);
test2Mapper.insert(user2);
}
@Test
public void selecttTest() {
User user1 = test1Mapper.selectById(1L);
log.info("user1 is:{}", JSON.toJSONString(user1));
User user2 = test2Mapper.selectById(1L);
log.info("user2 is:{}", JSON.toJSONString(user2));
}
}
最后,建表sql:
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(64) DEFAULT NULL,
`sex` tinyint(2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
码云地址:
https://gitee.com/ivwpw/springboot-mybatisplus-multi-datasource