• Spring+SpringMVC 整合 Mybatis 之多数据源


    自动注入方式

    // 所在的包
    import org.springframework.beans.factory.annotation.Value;
    
    @Value("${xxx.xxx}") 
    
    • 1
    • 2
    • 3
    • 4

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

    // 在 springboot 中
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "xxx.xxx") 
    
    • 1
    • 2
    • 3
    • 4

    放在类上,将本类中的所有属性和配置文件中的相关配置进行绑定
    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"/>
    
    • 1
    • 2
    • 3
    • 4

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    .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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    数据源配置

    /**
     * @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;
        }
    }
    
    • 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    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);
        }
    }
    
    • 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
    • 36
    • 37
    • 38

    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);
        }
    }
    
    • 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
    • 36
    • 37
  • 相关阅读:
    MYSQLg高级-----SQL注入的理解(初级篇)以及如何防止注入
    SpringBoot(五) - Java8 新特性
    iPhone 15 Pro有5项重大设计升级,让iPhone 15看起来很无聊
    【洛谷 P2678】[NOIP2015 提高组] 跳石头 题解(二分答案+递归)
    常见排序算法详解
    游戏开发应该关注质量而不是数量
    大连大学计算机考研资料汇总
    基于stm32单片机的超声波测距显示倒车雷达提醒报警系统Proteus仿真
    解读Redis常见命令
    用PHP实现极验验证功能
  • 原文地址:https://blog.csdn.net/weixin_43989102/article/details/125534886