1、问题遇到的现象和发生背景
数据库插入时出现��乱码
2、问题相关代码,请勿粘贴截图
for (DetectionFlowBinding flowBinding : bindingList) {
task = new PcrTask();
BeanUtils.copyProperties(operatorData, task);
task.setDetectionItemName(new String(operatorData.getDetectionItemName().getBytes(StandardCharsets.UTF_8)));
task.setFlowId(flowBinding.getFlowId());
task.setFlowName(detectionFlow.getFlowName());
task.setFlowNodeId(flowBinding.getNodeId());
task.setFlowNodeName(new String(flowBinding.getNodeName().getBytes(StandardCharsets.UTF_8)));
task.setContainsParam(flowBinding.getContainsParam());
task.setSampleId(operatorData.getId());//取样本表主键id
task.setCreateBy(param.getCreateBy());
task.setCreateTime(new Date());
operatorTaskList.add(task);
}
3、通过mybatis plus自动生成代码批量插入
4、DetectionItemName FlowName 这两个字段偶尔会出现中文+� 的组合乱码
5、插入前的日志看中文是正常的,阿里云上看sql审计也是正常的,保存到数据库就乱码了
注:mysql8.0版本
看下你mysql数据库的字符集是不是utf8
通过指令,在数据库中查看具体的字符集情况:
mysql> show variables like 'collation_%';
mysql> show variables like 'character_set_%';
修改数据库的字符集
mysql>alter database dbname character set utf-8; -- dbname 是你数据库的名字
再看下代码文件本身的文本编码是ansi还是utf8,包括前端和后端