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
application.properties需要将自动识别的spring.datasource改为自定义(就是加个自定义的字段给他改喽),例子在?
application.properties多加个自定义数据源,例子在?
有几个数据源就加几个数据源配置类,数据源配置类配置?
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
实体类
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;
省略。。。
#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
@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);
}
}
@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);
}
}
public interface StudentMapper1 {
List findAll();
Student findOne(Integer id);
}
public interface StudentMapper2 {
List findAll2();
Student findOne2(Integer id);
}
id, name,password,age
id, name,password,age
@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();
}
}