Spring boot 2.x 多数据源配置_包名及事务的实现

这是我的项目结构 :(因为我的项目引入了 tk.mapper、日志切面、lombok 所以有些在引入包扫描的时候会不一样,大家可以根据需要做处理)

Spring boot 2.x 多数据源配置_包名及事务的实现_第1张图片

 application.yml 文件

spring:
  # mysql x1
  datasource:
    hcg:
      jdbc-url: jdbc:mysql://localhost:3306/hcg?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
  # mysql x2
    cm:
      jdbc-url: jdbc:mysql://localhost:3306/cm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: mappers/*.xml
  type-aliases-package: com.hcg.mybatis.domain

mapper:
  mappers:
  - com.hcg.mybatis.common.business.CommonMapper
  notEmpty: false
  identity: MYSQL

数据源配置类 : DataSourceConfig。

package com.hcg.multiplydatasource.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;

//import org.mybatis.spring.annotation.MapperScan;

/**
 * 读取 DataSource 数据源
 */
@Configuration  // 注册到 spring 容器中
// 扫包范围
@MapperScan(basePackages = "com.hcg.multiplydatasource.mapper.hcmapper",sqlSessionFactoryRef = "hcgSqlSessionFactory")
public class HcgDataSourceConfig {

    @Bean(name = "hcgDataSource")
//    @Qualifier("hcgDataSource") // 依赖注入时候的名字
    @ConfigurationProperties(prefix="spring.datasource.hcg") // 扫描application 配置文件时的前缀
//    @Primary
    public DataSource hcgDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "hcgSqlSessionFactory")
//    @Primary
    public SqlSessionFactory hcgSqlSessionFactory(@Qualifier("hcgDataSource") DataSource hcgDataSource) throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(hcgDataSource);
        // 加载 mapper 对应的 xml 文件,如果没有可以注释掉
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mappers/hcg/*.xml")
        );
        return bean.getObject();
    }

    @Bean(name = "hcgTransactionManager")
//    @Primary
    public DataSourceTransactionManager hcgTransactionManager(@Qualifier("hcgDataSource") DataSource dataSource) throws Exception{
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "hcgSqlSessionTemplate")
//    @Primary
    public SqlSessionTemplate hcgSqlSessionTemplate(@Qualifier("hcgSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception{
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

service接口实现类

import com.hcg.multiplydatasource.domain.User;
import com.hcg.multiplydatasource.mapper.cmmapper.CmUserMapper;
import com.hcg.multiplydatasource.service.cmservice.CmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("cmUserService")
public class CmUserServiceImpl implements CmUserService {

    @Autowired
    private CmUserMapper cmUserMapper;

    @Override
    @Transactional(transactionManager = "cmTransactionManager")
    public void saveUser(User user) {
        cmUserMapper.insert(user);
        int i  = 1/0;
//        cmUserMapper.addUser(user.getUid(),user.getNickname(),user.getName(),user.getPassword(),user.getSalt(), user.getMobile());
    }

}

 

你可能感兴趣的:(Java,开发技术)