标题SQLException:数据库SQL异常
Cause: java.sql.SQLException: Illegal mix of collations (utf16_general_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation 'UNION'
; uncategorized SQLException; SQL state [HY000]; error code [1267]; Illegal mix of collations (utf16_general_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation 'UNION'; nested exception is java.sql.SQLException: Illegal mix of collations (utf16_general_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation 'UNION'
查资料知道是多表联查过程中,设计表时字符串varchar类型字段选择编码方式时不一致导致的:(utf16_general_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT)
解决方式:统一2个数据库的编码格式
GlobalExceptionHandler : Springboot全局异常处理GlobalExceptionHandler
二:请求方式错误
Request method 'GET' not supported错误:{}
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
解决后:
三:MySQLIntegrityConstraintViolationException
1.意思是违反了数据库的完整性约束,在表中建立了唯一索引
Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'state' in where clause is ambiguous
; ]; Column 'state' in where clause is ambiguous; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'state' in where clause is ambiguous
2.异常原因:
仔细分析一下,原来就是是因为插入的新数据,与表的主键唯一约束产生了冲突,也就是新数据的主键在表中已经存在了,不能重复插入同样的数据
3.解决方法:
1.第一种方案,将表中原有的相关记录删除,再重新插入即可;
2.第二种方案,可以考虑修改表的主键约束(不建议修改原有约束)。
3.第三种方案,检查sql语句
四.前端解析参数错误:
@ResponseBody
@PostMapping(“updateState”)
@ApiOperation(“手动更新执行排产单状态”)
public R updateApsPlanState(@RequestBody List planNumberList){
apsPlanService.updateApsPlanState(planNumberList);
return R.ok();
}
@PostMapping(value = "exportPlanDetail", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ApiOperation("导出排产产品信息")
public R exportPlanDetail(@RequestBody ExportPo exportPo, HttpServletResponse response){
apsPlanService.exportPlanDetail(exportPo.getPlanNumber(), exportPo.getLine(), response);
return R.ok();
}
接口做的是数据库查询数据导出到Excel表操作,开始我想的是导出完成以后给前端一个Code值,一个返回值信息。
public void exportPlanDetail(String planNumber,String line, HttpServletResponse response) {
String sheetName = StrUtil.format("{}{}排产详情表", planNumber, line);
String fileName = StrUtil.format("{}{}.xlsx", planNumber, line);
List<CommonParse> commonParseList = commonParseDao.selectList(new LambdaQueryWrapper<CommonParse>()
.eq(CommonParse::getPlanNumber, planNumber).eq(CommonParse::getLinePlan, line));
CustomException.throwByList(commonParseList, "获取排产信息失败,请稍后重试");
SXSSFWorkbook wb = FileUtil.createEuropeanFile(commonParseList,sheetName);
OutputStream os = null;
try {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
response.setHeader("content-Type","application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
os = response.getOutputStream();
wb.write(os);
os.flush();
} catch (Exception e) {
log.warn("导出",e);
}finally {
if (wb != null){
wb.dispose();
}
try {
if (os != null){
os.close();
}
} catch (IOException e){
log.warn("导出",e);
}
}
}
但业务层这里已经把返回值的参数类型重新定义了,造成上述的BUG,结果虽然正确,但日志服务报错。
修复这个BUG,不要返回值即可
@PostMapping(value = "exportPlanDetail", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ApiOperation("导出排产产品信息")
public void exportPlanDetail(@RequestBody ExportPo exportPo, HttpServletResponse response){
apsPlanService.exportPlanDetail(exportPo.getPlanNumber(), exportPo.getLine(), response);
}