springboot+mybatis多数据源mysql+oracle

一、异常错误处理:

1.jdbcUrl is required with driverClassName这行错误,一直报我的mybatis的xml文件的问题,其实并没有关系,主要原因是jdbcUrl的问题
2019-04-25 13:50:12.644 ERROR 14660 — [nio-8080-exec-1] com.zaxxer.hikari.HikariConfig : HikariPool-1 - jdbcUrl is required with driverClassName.
2019-04-25 13:50:12.654 ERROR 14660 — [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
The error may exist in class path resource [mybatis/mapper/StudentMapper3.xml]
The error may involve cn.syp.databases.mapper.mapper3.StudentMapper3.findAll3
The error occurred while executing a query
Cause: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.] with root cause

java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.

解决方案:

在properties的配置文件中,将自定义的datasource的url之前加上jdbc即可

spring.datasource.secondary3.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.secondary3.jdbc-url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.secondary3.username=glmedical
spring.datasource.secondary3.password=glmedical

二、springboot+mybatis多数据源步骤如下:

提示:完全可以在已有springboot+mybatis项目的基础上进行,根本不和别的类有丁点冲突,在已有整合的项目基础上只需3小步骤。

application.properties需要将自动识别的spring.datasource改为自定义(就是加个自定义的字段给他改喽),例子在?
application.properties多加个自定义数据源,例子在?
有几个数据源就加几个数据源配置类,数据源配置类配置?

  1. 构建springboot项目,项目构建不再提…
    文件目录:
    springboot+mybatis多数据源mysql+oracle_第1张图片

  2. dependence依赖引入

    
     
     
         org.springframework.boot
         spring-boot-starter-web
     
     
     
         org.mybatis.spring.boot
         mybatis-spring-boot-starter
         2.0.1
     
    
     
     
         mysql
         mysql-connector-java
         runtime
     
    
     
     
         org.springframework.boot
         spring-boot-starter-test
         test
     
    
     
     
         com.alibaba
         druid
         1.1.10
     
    
  3. 实体类

mysql库创建的实体类
  public class Student implements Serializable {
            private int id;
            private String name;
            private String password;
            private int age;
            省略。。。
oracle库创建的实体类

public class Demo1 implements Serializable {
    private int id;
    private String name;
    private String password;
    省略。。。
  1. application.properties文件配置,yml为后缀也行,都一样
    刚学,先试了一下连一个库,2个数据源都是mysql的库,尝试成功了,自己在后边加的oracle的配置
#mybatis的配置文件路径
mybatis.config-locations=classpath:mybatis/config.xml
#mybatis的mapper映射文件地址路径
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
#mysql库1的配置
spring.datasource.primary.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test1?serverTimezone=GMT%2B8
spring.datasource.primary.username=root
spring.datasource.primary.password=123456

#mysal库2的配置
spring.datasource.secondary.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
spring.datasource.secondary.username=root
spring.datasource.secondary.password=123456

#oracle库的配置
spring.datasource.secondary3.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.secondary3.jdbc-url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.secondary3.username=root
spring.datasource.secondary3.password=123456
这个demo写完后,我启动后报了Bug,bug原因是因为time zone时区的问题,所以在这里的mysql的url后边加了时区参数,预防了后边很多问题。
  1. 创建第一个数据源配置类DataSource1Config
    这个类是第一个也就是默认的数据源配置类
@Configuration
@MapperScan(basePackages = "cn.syp.databases.mapper.mapper1", sqlSessionTemplateRef  = "primarySqlSessionTemplate")
public class DataSource1Config {
    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/StudentMapper1.xml"));
        return bean.getObject();
    }
    @Bean(name = "primaryTransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name = "primarySqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
  1. 创建第二个数据源配置类DataSource2Config
@Configuration
@MapperScan(basePackages = "cn.syp.databases.mapper.mapper2", sqlSessionTemplateRef  = "secondarySqlSessionTemplate")
public class DataSource2Config {
    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/StudentMapper2.xml"));
        return bean.getObject();
    }

    @Bean(name = "secondaryTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "secondarySqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    }
  1. 第一个mapper,StudentMapper1
public interface StudentMapper1 {
    List findAll();
    Student findOne(Integer id);
}

  1. 第二个mapper,StudentMapper2
public interface StudentMapper2 {
    List findAll2();
    Student findOne2(Integer id);
}
  1. mybatis的xml配置文件,StudentMapper1.xml


   
    
        
        
        
        
    
    id, name,password,age
    
    

  1. mybatis的xml配置文件,StudentMapper2.xml


   
    
        
        
        
        
    
    id, name,password,age
    
    

  1. mybatis下的config.xml加不加无所谓,我是用不上



    
        
        
        
        
        
        
    

  1. MainController
@Controller
public class MainController {
    @Autowired(required = false)
    private StudentMapper1 studentMapper1;
    @Autowired(required = false)
    private StudentMapper2 studentMapper2;
    @Autowired(required = false)
    private StudentMapper3 studentMapper3;
    @RequestMapping("/find")
    private @ResponseBody String findAll(){
        List lists = studentMapper1.findAll();
        System.out.println(lists);
        return lists.toString();
    }
    @RequestMapping("/find2")
    private @ResponseBody String findAll2(){
        List lists = studentMapper2.findAll2();
        System.out.println(lists);
        return lists.toString();
    }
    @RequestMapping("/find3")
    private @ResponseBody String findAll3(){
        List lists = studentMapper3.findAll3();
        System.out.println(lists);
        return lists.toString();
    }
}

  1. 就此没东西了已经,运行使用即可。
    忠告:别慌,有BUG先看配置路径都写对了吗,慢慢来。
    示例下载:spring+mybatis多数据源mysql+oracle源码

你可能感兴趣的:(springboot)