一、异常错误处理:
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多加个自定义数据源,例子在?
有几个数据源就加几个数据源配置类,数据源配置类配置?
-
构建springboot项目,项目构建不再提…
文件目录:
-
dependence依赖引入
<dependencies> <!--web相关依赖,--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mybatis和springboot整合依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <!--mysql依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--springboot测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--最好的数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
-
实体类
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;
省略。。。
- 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后边加了时区参数,预防了后边很多问题。
- 创建第一个数据源配置类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);
}
}
- 创建第二个数据源配置类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);
}
}
- 第一个mapper,StudentMapper1
public interface StudentMapper1 {
List<Student> findAll();
Student findOne(Integer id);
}
- 第二个mapper,StudentMapper2
public interface StudentMapper2 {
List<Student> findAll2();
Student findOne2(Integer id);
}
- mybatis的xml配置文件,StudentMapper1.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.syp.databases.mapper.mapper1.StudentMapper1" > <!--指定mapper位置-->
<resultMap id="studentMap" type="cn.syp.databases.entity.Student" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List" >id, name,password,age</sql>
<select id="findAll" resultMap="studentMap" >
SELECT
<include refid="Base_Column_List" />
FROM student
</select>
<select id="findOne" resultMap="studentMap">
SELECT * FROM student WHERE id=#{id}
</select>
</mapper>
- mybatis的xml配置文件,StudentMapper2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.syp.databases.mapper.mapper2.StudentMapper2" > <!--指定mapper位置-->
<resultMap id="studentMap" type="cn.syp.databases.entity.Student" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
</resultMap>
<sql id="Base_Column_List" >id, name,password,age</sql>
<select id="findAll2" resultMap="studentMap" >
SELECT
<include refid="Base_Column_List" />
FROM student
</select>
<select id="findOne2" resultMap="studentMap">
SELECT * FROM student WHERE id=#{id}
</select>
</mapper>
- 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>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
- 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<Student> lists = studentMapper1.findAll();
System.out.println(lists);
return lists.toString();
}
@RequestMapping("/find2")
private @ResponseBody String findAll2(){
List<Student> lists = studentMapper2.findAll2();
System.out.println(lists);
return lists.toString();
}
@RequestMapping("/find3")
private @ResponseBody String findAll3(){
List<Demo1> lists = studentMapper3.findAll3();
System.out.println(lists);
return lists.toString();
}
}
- 就此没东西了已经,运行使用即可。
忠告:别慌,有BUG先看配置路径都写对了吗,慢慢来。
示例下载:spring+mybatis多数据源mysql+oracle源码