Mybatis异常处理之MySQL Connector Java] will not be managed by Spring

时间:2022-12-25 16:16:22

很长时间没写后台代码有点生疏了,这不今天又出点小插曲,写个文章记录下。

由于要上传点数据到后台,顺手整了个mybatis+springmvc。在保存数据时出现了异常。

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@71670c91] was not registered for synchronization because synchronization is not active
JDBC Connection [jdbc:mysql://localhost:3306/statsdb?useUnicode=true&characterEncoding=UTF-8, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring
==> Preparing: insert into record_info
==> Parameters:
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@71670c91]

看提示还以为事务啥的有问题,一通资料查找处理后,发现问题还在。

于是换postman调试接口,看看具体什么错误

Mybatis异常处理之MySQL Connector Java] will not be managed by Spring

BadSqlGrammarException

有点懵逼,SQL都是generator生成的。抱着怀疑的态度,xml中的SQL改的简单点 insert into record_info (model) values ('xx'),运行居然成功。果然是这块有问题,于是继续改SQL,改成参数形式:insert into record_info (model) VALUES (#{model,jdbcType=VARCHAR}),运行成功发现没值。这时感觉有点不对,值肯定传了啊。于是查看赋值代码,这一看发现问题了,忘了提取data数据进行转换,改正后还原xml代码,运行成功...

if (!StringUtils.isBlank(body)) {
       //错误的写法
       //RecordInfo recordInfo = getGson().fromJson(body, RecordInfo.class);        //改正后写法
JsonParser jsonParser = new JsonParser();
JsonElement ele = jsonParser.parse(body);
JsonElement jo = ele.getAsJsonObject().get("data");
RecordInfo recordInfo = getGson().fromJson(jo, RecordInfo.class);
if (recordInfo != null) {
reportService.addPosRecordInfo(recordInfo);
}
}

  

结论:由于参数赋值有问题,导致SQL运行出错