Docker安装达梦M8
下载镜像tar包:https://eco.dameng.com/download/
#导入镜像
docker load -i dm8_20220822_rev166351_x86_rh6_64_ctm.tar
# 启动容器
docker run -d -p 5236:5236 --restart=always --name dm8 --privileged=true -e PAGE_SIZE=16 -e LD_LIBRARY_PATH=/opt/dmdbms/bin -e INSTANCE_NAME=dm8 -v /data/dm8:/opt/dmdbms/data dm8_single:v8.1.2.128_ent_x86_64_ctm_pack4
创建表
CREATE TABLE employee
(
employee_id INTEGER,
employee_name VARCHAR2(20) NOT NULL,
hire_date DATE,
salary INTEGER,
department_id INTEGER NOT NULL
);
INSERT INTO employee VALUES (9999, '王达梦','2008-05-30 00:00:00', 30000, 666);
commit;
客户端访问
导入依赖
<dependency>
<groupId>com.dameng</groupId>
<artifactId>DmJdbcDriver18</artifactId>
<version>8.1.2.192</version>
</dependency>
测试代码:
@Test
public void testMybatis(){
List<Employee> employees = employeeMapper.selectAll();
System.out.println(JSON.toJSONString(employees));
}
//输出结果:
[{"departmentId":666,"employeeId":9999,"employeeName":"鐜嬭揪姊�","hireDate":"2008-05-30","salary":30000}]
可以看到中文乱码了。
乱码的原因
创建DmdbConnection的时候,硬编码了serverEncoding:
当客户端收到服务端返回的数据的时候就是使用这个serverEncoding做字符串编码:
但是查看服务器的编码竟然是GB18030:
SQL> select unicode() from dual;
LINEID UNICODE()
---------- -----------
1 0
used time: 1.942(ms). Execute id is 501.
SQL>
0代表是GB18030
肯定是有地方出bug了。
解决办法
- 如果是使用springboot的连接池
重写HikariDataSource的getConnection()方法,在里面去修改serverEncoding即可:
public class HikariDataSource extends com.zaxxer.hikari.HikariDataSource {
@Override
public Connection getConnection() throws SQLException{
Connection proxyConnection = super.getConnection();
HikariProxyConnection conn = (HikariProxyConnection) proxyConnection;
DmdbConnection dmdbConnection = conn.unwrap(DmdbConnection.class);
dmdbConnection.setServerEncoding(StandardCharsets.UTF_8);
return proxyConnection;
}
}
@Configuration
public class DataSourceConfig {
@Autowired
private DataSourceProperties dataSourceProperties;
@Bean
public HikariDataSource hikariDataSource(){
com.github.xjs.dameng.config.HikariDataSource ds = new com.github.xjs.dameng.config.HikariDataSource();
ds.setDriverClassName(dataSourceProperties.getDriverClassName());
ds.setJdbcUrl(dataSourceProperties.getUrl());
ds.setUsername(dataSourceProperties.getUsername());
ds.setPassword(dataSourceProperties.getPassword());
return ds;
}
}
- 如果是使用原生的jdbc连接,反射修改serverEncoding即可
private static void fixEncoding(Connection connection){
if(connection instanceof DmdbConnection){
DmdbConnection dmdbConnection = (DmdbConnection)connection;
dmdbConnection.setServerEncoding(StandardCharsets.UTF_8);
}
}