• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

mycnrc3iixf3876.png

设置数据库信息

#主数据源

春天。数据来源。初级。URL=JDBC :我的SQL ://127。0 .0 .1:3306/你的心?使用unicode=true字符编码=utf-8使用SSL=false

春天。数据来源。初级。用户名=测试001

春天。数据来源。初级。密码=测试001

春天。数据来源。初级。驱动程序类名=com。MySQL。JDBC。驾驶员

#副数据源

春天。数据来源。第二。URL=JDBC :我的SQL ://127。0 .0 .1:3306/你的心脏测试?使用unicode=true字符编码=utf-8使用SSL=false

春天。数据来源。第二。用户名=测试002

春天。数据来源。第二。密码=测试002

春天。数据来源。第二。驱动程序类名=com。MySQL。JDBC。驾驶员

设置砰的一声依赖

!跳羚队整合我的巴蒂斯

属国

groupIdorg.mybatis.spring.boot/groupId

artifactId my batis-spring-boot-starter/artifactId

版本2 .1 .2/版本

/依赖关系

!- mysql驱动-

属国

groupIdmysql/groupId

artifactId MySQL-连接器-java/artifactId

版本5 .1 .6/版本

/依赖关系

配置主数据源

包com。Java。配置;

导入com。zax xer。光。hikari配置;

导入com。zax xer。光。hikari数据源;

导入org。阿帕奇。伊巴提斯。会话。SQL会话工厂;

导入org。我的巴蒂斯。春天。sqlsessionfactorybean

导入org。我的巴蒂斯。春天。sqlsessiontemplate

导入org。我的巴蒂斯。春天。注释。映射器扫描;

导入org。spring框架。豆子。工厂。注释。限定符;

导入org。spring框架。豆子。工厂。注释。价值;

导入org。spring框架。语境。注释。豆;

导入org。spring框架。语境。注释。配置;

导入org。spring框架。语境。注释。初级;

导入组织。spring框架。核心。io。支持。pathmatchingresourcepatternresolver;

导入javax。SQL。数据来源;

/**

* @作者你的心

* @描述

* @create 2022-05-02 11:52

*/

@配置

@ mapper扫描(基础包=' com。Java。制图师。zero ',sqlSessionFactoryRef=' PrimarySqlSessionFactory ')//基本包:接口文件的包

路径 public class DruidConfiguration { @Value("${spring.datasource.primary.driverClassName}") private String driverClassName; @Value("${spring.datasource.primary.url}") private String url; @Value("${spring.datasource.primary.username}") private String userName; @Value("${spring.datasource.primary.password}") private String password; @Bean(name = "PrimaryDataSource") // 表示这个数据源是默认数据源 @Primary//这个一定要加,如果两个数据源都没有@Primary会报错 public DataSource getPrimaryDateSource() { HikariConfig config = new HikariConfig(); config.setDriverClassName(driverClassName); config.setJdbcUrl(url); config.setUsername(userName); config.setPassword(password); HikariDataSource dataSource = new HikariDataSource(config); return dataSource; } @Bean(name = "PrimarySqlSessionFactory") @Primary public SqlSessionFactory primarySqlSessionFactory(@Qualifier("PrimaryDataSource") DataSource datasource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); /** * 设置mybatis的xml所在位置 */ bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/zero/*.xml")); /** * 设置驼峰命名 */ org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); configuration.setMapUnderscoreToCamelCase(true); bean.setConfiguration(configuration); return bean.getObject(); } @Bean("PrimarySqlSessionTemplate") /** * 表示这个数据源是默认数据源 */ @Primary public SqlSessionTemplate primarySqlSessionTemplate( @Qualifier("PrimarySqlSessionFactory") SqlSessionFactory sessionfactory) { return new SqlSessionTemplate(sessionfactory); } }

  配置从数据库信息

package com.java.config;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 javax.sql.DataSource;
/**
 * @author yourheart
 * @Description
 * @create 2022-05-02 21:32
 */
@Configuration
@MapperScan(basePackages = "com.java.mapper.first",sqlSessionFactoryRef = "SecondarySqlSessionFactory")
public class SecondaryDataSourceConfig {
    @Value("${spring.datasource.second.driverClassName}")
    private String driverClassName;
    @Value("${spring.datasource.second.url}")
    private String url;
    @Value("${spring.datasource.second.username}")
    private String userName;
    @Value("${spring.datasource.second.password}")
    private String password;
    @Bean(name = "SecondaryDataSource")
    public DataSource getSecondaryDataSource() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(driverClassName);
        config.setJdbcUrl(url);
        config.setUsername(userName);
        config.setPassword(password);
        HikariDataSource dataSource = new HikariDataSource(config);
        return dataSource;
    }
    @Bean(name = "SecondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("SecondaryDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        /**
         * 设置mybatis的xml所在位置
         */
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/first/*.xml"));
        /**
         * 设置驼峰命名
         */
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(configuration);
        return bean.getObject();
    }
    @Bean("SecondarySqlSessionTemplate")
    public SqlSessionTemplate secondarySqlSessionTemplate(
            @Qualifier("SecondarySqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

  ll4uaum3vnc3877.png

 

wj0nqllixf13878.png

 

在DruidConfiguration中的basePackages = "com.java.mapper.zero"

表示的意思是会扫描com.java.mapper.zero目录下所有的文件,所以主数据库mapper文件和从数据库mapper文件,最好配置的是同一目录下的两个不同的文件夹

结果就是不然调用那个mapper下的文件,查询的数据都只会是主数据库的数据

 

Link to comment
Share on other sites