springboot整合多个mybatis数据源

1.idea,选择mybatis,mysql,web…

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

2.程序启动类加上exclude去掉boot的默认数据源配置;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class SpringBootMybtais2Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMybtais2Application.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.项目结构如下: 
这里写图片描述

4.application.properties配置如下:

server.port=8081
logging.config=./config/logback.xml

#==================多个数据源============
spring.datasource.test1.url=jdbc:mysql://192.168.10.215:3306/gexin_aus?useUnicode=true&characterEncoding=utf8
spring.datasource.test1.username=remote
spring.datasource.test1.password=hangzhoumeiri
spring.datasource.test1.driver-class-name=com.mysql.jdbc.Driver
mybatis.test1.typeAliasesPackage=com.pkx.model.test1
mybatis.test1.mapperLocations=classpath:mapper.test1/*.xml


spring.datasource.test2.url=jdbc:mysql://192.168.10.41:3308/gexin_dmp_dms?useUnicode=true&characterEncoding=utf8
spring.datasource.test2.username=remote
spring.datasource.test2.password=hangzhoumeiri
spring.datasource.test2.driver-class-name=com.mysql.jdbc.Driver
mybatis.test2.typeAliasesPackage=com.pkx.model.test2
mybatis.test2.mapperLocations=classpath:mapper.test2/*.xml

#============== kafka ===================
kafka.consumer.zookeeper.connect=192.168.11.2:2181,192.168.10.218:2181
kafka.consumer.servers=192.168.10.221:9093
kafka.consumer.enable.auto.commit=true
kafka.consumer.session.timeout=6000
kafka.consumer.auto.commit.interval=100
kafka.consumer.auto.offset.reset=latest
kafka.consumer.topic=test
kafka.consumer.group.id=group-id-test
kafka.consumer.concurrency=10

kafka.producer.servers=192.168.10.221:9093
kafka.producer.retries=0
kafka.producer.batch.size=4096
kafka.producer.linger=1
kafka.producer.buffer.memory=40960
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

5.controller层代码

controller1

@RequestMapping("user")
@RestController
public class GdmpUserController {
    @Autowired
    private GdmpUserMapper gdmpUserMapper;

    @RequestMapping("/list")
    public List<GdmpUser> listAll() {
        return gdmpUserMapper.selectByExample(new GdmpUserExample());
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

controller2

@RequestMapping("account")
@RestController
public class AccountController {
    @Autowired
    private AccountMapper accountMapper;

    @RequestMapping("/list")
    public List<Account> listAll() {
        return accountMapper.selectByExample(new AccountExample());
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

==6.数据源的配置类;==

数据源1:先注入数据源,再SqlSessionFactory,再DataSourceTransactionManager事务,再SqlSessionTemplate;

@Configuration
@MapperScan(basePackages = "com.pkx.dao.test1", sqlSessionTemplateRef  = "test1SqlSessionTemplate")
public class DataSource1Config {

    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/test1/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "test1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

数据源2:

@MapperScan注解扫描dao层

@Configuration
@MapperScan(basePackages = "com.pkx.dao.test2", sqlSessionTemplateRef  = "test2SqlSessionTemplate")
public class DataSource2Config {

    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/test2/*.xml"));
        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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

至于model,xml,mapper可以用generator自动生成;

阅读更多
个人分类: mybatis
上一篇springboot整合多个mybatis数据源
下一篇Spring Boot扩展机制:图文深入分析Spring Factories机制
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭