1. 查看Oracle数据库字符集编码
select userenv('language') from dual;

SpringBoot查询Oracle数据库,中文乱码问题解决_bc

  1. 将要查询的记录
  2. SpringBoot连接Oracle数据库(版本是11g)
  • maven依赖
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
  • yml配置
spring:
datasource:
url: jdbc:oracle:thin:@//172.*.*.*:1521/orcl
username: *
password: *
driver-class-name: oracle.jdbc.driver.OracleDriver
  1. 使用MybatisPlus查询,发现中文乱码
  2. 解决方式有两种,动态和全局转换(推荐)字符编码
  • 动态
//存=>客户端字符串转ISO-8859-1后插入数据库
new String(str.getBytes("GBK"), "ISO-8859-1");

//取=>数据库字符串转为GBK后查看
new String(str.getBytes("ISO-8859-1"), "GBK");
  • 全局
  1. 引入druid连接池
<dependency><!--druid-->
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
  1. 修改yml配置
#1.url前添加jdbc:wrap-jdbc:filters=encoding:
#2.添加type: com.alibaba.druid.pool.DruidDataSource
#3.添加druid.connection-properties: serverEncoding=ISO-8859-1;clientEncoding=GBK;defaultRowPrefetch=50;bigStringTryClob=true
#注意第3.条后面的defaultRowPrefetch和bigStringTryClob可以不加
spring:
datasource:
url: jdbc:wrap-jdbc:filters=encoding:jdbc:oracle:thin:@//172.16.100.102:1521/orcl
username: wangyh
password: wangyh
driver-class-name: oracle.jdbc.driver.OracleDriver
type: com.alibaba.druid.pool.DruidDataSource
druid:
connection-properties: serverEncoding=ISO-8859-1;clientEncoding=GBK
  1. 之后查询中文乱码问题正常