Spring+SpringMVC 整合 Mybatis 之多数据源

4 篇文章 0 订阅
4 篇文章 0 订阅

自动注入方式

// 所在的包
import org.springframework.beans.factory.annotation.Value;

@Value("${xxx.xxx}") 

属性上,自动注入配置文件的配置值到某个属性

// 在 springboot 中
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "xxx.xxx") 

放在类上,将本类中的所有属性和配置文件中的相关配置进行绑定
prefix:配置文件配置的前缀

spring-context 配置文件

<description>Spring公共配置</description>
    ...
<!-- 开启properties文件解析支持 -->
<context:property-placeholder file-encoding="UTF-8" location="classpath:/system.properties,classpath:/config/*/*-spring.properties" ignore-unresolvable="false"/>

mybatis-config.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 开启驼峰命名自动映射 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="cacheEnabled" value="true"/>
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="jdbcTypeForNull" value="VARCHAR"/>
        <!-- 日志 -->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <!-- 别名 -->
    <typeAliases>
        <package name="com.nuts.xxx.entity"/>
    </typeAliases>
    
</configuration>

.properties 配置文件

# 数据源默认配置,可以被 spring 识别自动
spring.datasource.url=jdbc:mysql://172.16.100.100:3306/platform?useUnicode=true&amp;characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456

# 使用自己的 key,不会被 spring 是被自动识别
# 需要自己配置 DataSource 和 MybatisConfig ==> 注入:SqlSessionFactory() 和 sqlSessionTemplate() 
# 数据源1
xxx.datasource.url=jdbc:mysql://172.16.100.100:3306/platform?useUnicode=true&amp;characterEncoding=UTF-8
xxx.datasource.username=root
xxx.datasource.password=123456
# 数据源2
zzz.datasource.url=jdbc:mysql://172.16.100.100:3306/platform?useUnicode=true&amp;characterEncoding=UTF-8
zzz.datasource.username=root
zzz.datasource.password=123456

# xxx Mybatis配置
xxx.mybatis.config.path=config/xxx/mybatis-config.xml
xxx.mybatis.mapper.path=classpath:mapper/xxx/*.xml

# zzz Mybatis配置
zzz.mybatis.config.path=config/xxx/mybatis-config.xml
zzz.mybatis.mapper.path=classpath:mapper/zzz/*.xml

数据源配置

/**
 * @desc 数据源配置
 * @auth llp
 * @date 2022/6/29 9:49
 */
@Configuration
public class DataSourceConfig {

    /**-------------- netguns ---------------------*/
    @Value("${xxx.datasource.url}")
    private String xxxUrl;
    @Value("${xxx.datasource.username}")
    private String xxxUsername;
    @Value("${xxx.datasource.password}")
    private String xxxPassword;

    /**-------------- nbpf ---------------------*/
    @Value("${zzz.datasource.url}")
    private String zzzfUrl;
    @Value("${zzz.datasource.username}")
    private String zzzUsername;
    @Value("${zzz.datasource.password}")
    private String zzzPassword;

    /**
     * @desc 
     * @auth llp
     * @date 2022/6/29 11:30
     * @return javax.sql.DataSource
     */
    @Bean(name = "xxxDataSource")
    // @ConfigurationProperties(prefix = "xxx.xxx") springboot 用法
    public DataSource xxxDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(xxxUrl);
        druidDataSource.setUsername(xxxUsername);
        druidDataSource.setPassword(xxxPassword);
        return druidDataSource;
    }

    /**
     * @desc 
     * @auth llp
     * @date 2022/6/29 11:30
     * @return javax.sql.DataSource
     */
    @Bean(name = "zzzDataSource")
    // @ConfigurationProperties(prefix = "zzz.zzz") springboot 用法
    public DataSource zzzDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(zzzUrl);
        druidDataSource.setUsername(zzzUsername);
        druidDataSource.setPassword(zzzPassword);
        return druidDataSource;
    }
}

MybatisConfig

XxxMyBatisConfig

/**
 * @desc
 * @author llp
 * @date 2022/6/22 10:41
 */
@Configuration
@MapperScan(
        basePackages = {"com.xxx.xxx.mapper.xxx"},
        sqlSessionFactoryRef = "xxx_sqlSessionFactory",
        sqlSessionTemplateRef = "xxx_sqlSessionTemplate"
)
public class XxxMyBatisConfig {

    /** mybatis 配置文件地址 */
    @Value("${xxx.mybatis.config.path}")
    private String configLocation;
    /** mybatis mapper文件地址 */
    @Value("${xxx.mybatis.mapper.path}")
    private String mapperLocation;
    
    @Autowired
    @Qualifier("xxxDataSource")
    private DataSource dataSource;
    
    @Bean(name = "xxx_sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(){
        ...
        factoryBean.setDataSource(dataSource);
        ...
    }

    ...
    
    @Bean(name = "xxx_transactionManager")
    public DataSourceTransactionManager  transactionManager(){
        return new DataSourceTransactionManager(dataSource);
    }
}

ZzzMyBatisConfig

/**
 * @desc
 * @author llp
 * @date 2022/6/22 10:41
 */
@Configuration
@MapperScan(
        basePackages = {"com.xxx.xxx.mapper.zzz"},
        sqlSessionFactoryRef = "zzz_sqlSessionFactory",
        sqlSessionTemplateRef = "zzz_sqlSessionTemplate"
)
public class ZzzMyBatisConfig {
    /** mybatis 配置文件地址 */
    @Value("${zzz.mybatis.config.path}")
    private String configLocation;
    /** mybatis mapper文件地址 */
    @Value("${zzz.mybatis.mapper.path}")
    private String mapperLocation;
    
    @Autowired
    @Qualifier("zzzDataSource")
    private DataSource dataSource;
    
    @Bean(name = "zzz_sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(){
        ...
        factoryBean.setDataSource(dataSource);
        ...
    }
    
    ...

    @Bean(name = "zzz_transactionManager")
    public DataSourceTransactionManager  transactionManager(){
        return new DataSourceTransactionManager(dataSource);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值