我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

spring-boot2.0Mybatis多数据源配置-创新互联

spring-boot2.0 Mybatis多数据源配置

1.首先贴出pom.xml文件



    4.0.0

    com.example
    demoT002
    0.0.1-SNAPSHOT
    jar

    demoT002
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

         
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.1.1
        
         
        
            mysql
            mysql-connector-java
            5.1.21
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2.然后是application.properties

#数据源1

spring.datasource.titan-master.jdbc-url=jdbc:mysql://192.168.7.284:3306/account?useUnicode=true&characterEncoding=utf-8
spring.datasource.titan-master.username=uat_api_java
spring.datasource.titan-master.password=54GDfethu634tIi6778FjsB
spring.datasource.titan-master.driver-class-name=com.mysql.jdbc.Driver

#数据源2
spring.datasource.db2.jdbc-url=jdbc:mysql://192.168.7.284:3306/basic_business?useUnicode=true&characterEncoding=utf-8
spring.datasource.db2.username=uat_api_java
spring.datasource.db2.password=54GDfethurSDFvhyU5634tIFjsB
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver

注意这边不是url而使用的是jdbc-url

创新互联自2013年起,是专业互联网技术服务公司,拥有项目网站制作、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元仙桃做网站,已为上家服务,为仙桃各地企业和个人服务,联系电话:18980820575

3.配置文件

主文件中:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})//exclude 排除自动读取数据源 需要添加两个数据库
@ServletComponentScan
public class DemoT002Application {

    public static void main(String[] args) {
        SpringApplication.run(DemoT002Application.class, args);
    }
}

DataSource配置:

@Configuration
public class DataSourceConfig {
     @Bean(name = "ds1")
     @Primary 
        @ConfigurationProperties(prefix = "spring.datasource.titan-master") // application.properteis中对应属性的前缀
        public DataSource dataSource1() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "ds2")
        @ConfigurationProperties(prefix = "spring.datasource.db2") // application.properteis中对应属性的前缀
        public DataSource dataSource2() {
            return DataSourceBuilder.create().build();
        }
}

第一个连接配置:

@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.account"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {
    @Autowired
    @Qualifier("ds1")
    private DataSource ds1;

    @Bean
    @Primary 
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds1); // 使用titan数据源, 连接titan库
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/com/example/demo/mapper/account/*.xml"));
        return factoryBean.getObject();

    }

    @Bean
    @Primary 
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory
        return template;
    }
}

第二个连接配置:

@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.basicbusiness"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {
    @Autowired
    @Qualifier("ds2")
    private DataSource ds2;

    @Bean
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds2);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/com/example/demo/mapper/basicbusiness/*.xml"));

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
        return template;
    }
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


分享标题:spring-boot2.0Mybatis多数据源配置-创新互联
文章起源:http://shouzuofang.com/article/heihs.html

其他资讯