一、背景
笔者碰到利用零碎连贯Oracle数据库时多语言须要对立应用英语,该配置不能在数据库Server端批改,因些须要在利用零碎端想方法进行配置。
二、解决形式
通过查阅数据源和mybatis相干源码,有两种形式反对批改:
1、应用Druid数据源的配置项connectionInitSqls定义连贯初始化语句:
connectionInitSqls: ["ALTER SESSION SET NLS_LANGUAGE='AMERICAN'"]
2、不批改数据源配置,在利用零碎端减少mybatis的拦截器,拦截器代码如下:
(1)、Spring boot的Bean配置
@Configuration
public class MybatisPlusConfig {
/** * 更改会话状态 * * @return */@Beanpublic AlterSessionInterceptor alterSessionInterceptor() { return new AlterSessionInterceptor();}/** * 乐观锁插件 * * @return */@Beanpublic CustomOptimisticLockerInterceptor optimisticLockerInterceptor() { return new CustomOptimisticLockerInterceptor();}/** * oracle Sequence主键 * * @return */@Beanpublic OracleKeyGenerator oracleKeyGenerator() { return new OracleKeyGenerator();}
}
(2)、拦截器代码:
@Slf4j
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class AlterSessionInterceptor implements Interceptor {
@Overridepublic Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); Connection connection = (Connection) args[0]; if ("Oracle".equalsIgnoreCase(connection.getMetaData().getDatabaseProductName())) { Statement statement = null; try { statement = connection.createStatement(); String locale = RequestHelper.getCurrentLocale(); if ("en_GB".equalsIgnoreCase(locale)) { statement.execute("ALTER SESSION SET NLS_LANGUAGE='AMERICAN'"); } else { statement.execute("ALTER SESSION SET NLS_LANGUAGE='SIMPLIFIED CHINESE'"); } } finally { statement.close(); } } return invocation.proceed();}@Overridepublic Object plugin(Object target) { if (target instanceof StatementHandler) { return Plugin.wrap(target, this); } return target;}@Overridepublic void setProperties(Properties properties) { // to do nothing}
三、计划比照
1、在数据源配置,一次性配置,不必批改代码,所有用户都全副看到的错误信息全部都是英文提醒
2、在代码进行拦挡解决,须要批改代码,可针对不同的用户显示不同语言版本的谬误提醒,然而每次执行语句时,都须要多执行一句设置语言版本语句,操作繁琐,影响性能