配置datasource
spring:
# profiles:
# active: dev
# 数据源配置
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@IP地址:1521:ora11
username: 用户名
password: 密码
hikari:
max-lifetime: 300000
#MySQL测试语句
# connection-test-query: SELECT 1
#oracle测试语句(这不改会报 ORA-00933: SQL 命令未正确结束)
connection-test-query: SELECT 1 from dual
# MyBatis-plus配置
mybatis-plus:
# 枚举扫描包
type-enums-package: com.**.enums
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapper-locations: classpath*:/com/**/mapper/xml/**/*.xml
#区分加载数据库环境配置(主要设置插件中数据库类型,同一套代码要同时兼容MySQL和oracle,没找到更好的配置方式,让使用的开发人员改动最少)
configLocation: classpath:mybatis/mybatis-config-oracle.xml
# configLocation: classpath:mybatis/mybatis-config-mysql.xml
mybatis-config-oracle.xml
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
<property name="@page" value="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"/>
<property name="page:dbType" value="oracle"/>
</plugin>
</plugins>
</configuration>
springBoot方式配置Mybatis
@Configuration
@MapperScan(value={"com.**.mapper*"})
public class MybatisConfig {
//在bootstrap中配置数据库类型,可以删除mybatis-config-oracle.xml及bootstrap中configLocation相关配置
@Value("${spring.datasource.db-type}")
private String dbTypeConfig;
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
//从bootstrap中获取数据库类型
DbType dbType = DbType.getDbType(dbTypeConfig);
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(dbType));
return interceptor;
}
@SuppressWarnings("deprecation")
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
//设置oracle自增,需要在oracle创建序列,及配合@TableId(type = IdType.INPUT),因为代码要同时支持MySQL和oracle,所以都用默认的IdType.NONE(雪花算法)
@Bean
public OracleKeyGenerator oracleKeyGenerator(){
return new OracleKeyGenerator();
}
}
遇到的问题
1、如果oracle驱动使用ojdbc6,会不支持JDK8日期新类型LocalDateTime可以更换为ojdbc8。
(MyBatis-Plus官网描述)
2、在未将上述分页插件的page:dbType改为oracle时,进行分页查询总是报ORA-00933: SQL 命令未正确结束,可是控制台打印的SQL在数据库执行是没问题的。将日志级别改为debug,发现分页查询用的limit,修改配置后能够正常分页查询。