关于mybatis:mybatis操作Oracle数据库如何指定NLSLANGUAGE多语言

4次阅读

共计 1756 个字符,预计需要花费 5 分钟才能阅读完成。

一、背景
笔者碰到利用零碎连贯 Oracle 数据库时多语言须要对立应用英语,该配置不能在数据库 Server 端批改,因些须要在利用零碎端想方法进行配置。
二、解决形式
通过查阅数据源和 mybatis 相干源码,有两种形式反对批改:
1、应用 Druid 数据源的配置项 connectionInitSqls 定义连贯初始化语句:
connectionInitSqls: [“ALTER SESSION SET NLS_LANGUAGE=’AMERICAN'”]
2、不批改数据源配置,在利用零碎端减少 mybatis 的拦截器,拦截器代码如下:
(1)、Spring boot 的 Bean 配置
@Configuration
public class MybatisPlusConfig {

/**
 * 更改会话状态
 *
 * @return
 */
@Bean
public AlterSessionInterceptor alterSessionInterceptor() {return new AlterSessionInterceptor();
}

/**
 * 乐观锁插件
 *
 * @return
 */
@Bean
public CustomOptimisticLockerInterceptor optimisticLockerInterceptor() {return new CustomOptimisticLockerInterceptor();
}

/**
 * oracle Sequence 主键
 *
 * @return
 */
@Bean
public OracleKeyGenerator oracleKeyGenerator() {return new OracleKeyGenerator();
}

}

(2)、拦截器代码:
@Slf4j
@Intercepts({@Signature(type = StatementHandler.class, method = “prepare”, args = {Connection.class, Integer.class})})
public class AlterSessionInterceptor implements Interceptor {

@Override
public 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();}

@Override
public Object plugin(Object target) {if (target instanceof StatementHandler) {return Plugin.wrap(target, this);
    }
    return target;
}

@Override
public void setProperties(Properties properties) {// to do nothing}

三、计划比照
1、在数据源配置,一次性配置,不必批改代码,所有用户都全副看到的错误信息全部都是英文提醒
2、在代码进行拦挡解决,须要批改代码,可针对不同的用户显示不同语言版本的谬误提醒,然而每次执行语句时,都须要多执行一句设置语言版本语句,操作繁琐,影响性能

正文完
 0