http://localhost:8080/Sell/vi?begin=2023-06-04&end=2023-06-06
//接口get查询地址
查询成功,但是postman测试返回data为空
查询成功,但是postman测试返回data为空
{
"code": 1,
"msg": "success",
"data": [
null
]
}
这时到后端查看mybatis查询情况却发现查询成功返回数据正常
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6a2e1306] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@2076509092 wrapping com.mysql.cj.jdbc.ConnectionImpl@64dc86c6] will not be managed by Spring
==> Preparing: SELECT SUM(pricesum) FROM tb_sellinfo WHERE date BETWEEN ? AND ?
==> Parameters: 2023-06-04(LocalDate), 2023-06-06(LocalDate)
<== Columns: SUM(pricesum)
<== Row: 1400
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6a2e1306]
排查问题发现实际上是实体类中没有查询返回结果中的 “SUM(pricesum)” getset方法
public class SellInfo {
private Integer id;
//规整时间返回格式@JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
@JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd",timezone="GMT+8")
private Date date;
private String ISBN;
private Integer num;
private String price;
private Integer pricesum;
private String SUM;//加上对应名称的实体类,并修改返回结果的列名
}
在sql查询中修改:
--这是原sql查询
SELECT SUM(pricesum)
FROM tb_sellinfo
<where>
<if test="isbn != null">
AND ISBN = #{isbn}
</if>
<if test="begin != null and end != null">
AND date BETWEEN #{begin} AND #{end}
</if>
</where>
--修改返回列名为实体类中对应的SUM
SELECT SUM(pricesum) AS SUM
FROM tb_sellinfo
<where>
<if test="isbn != null">
AND ISBN = #{isbn}
</if>
<if test="begin != null and end != null">
AND date BETWEEN #{begin} AND #{end}
</if>
</where>
重启后端之后,此时同样的get请求查询sum返回已经正确封装在data中
{
"code": 1,
"msg": "success",
"data": [
{
"id": null,
"date": null,
"num": null,
"price": null,
"pricesum": null,
"isbn": null,
"sum": "1400"
}
]
}
到后端查看发现列名已经变成与实体类对应的SUM文章来源:https://www.toymoban.com/news/detail-697573.html
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@55c2be89] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@805059805 wrapping com.mysql.cj.jdbc.ConnectionImpl@5f3b84bd] will not be managed by Spring
==> Preparing: SELECT SUM(pricesum) AS SUM FROM tb_sellinfo WHERE date BETWEEN ? AND ?
==> Parameters: 2023-06-04(LocalDate), 2023-06-06(LocalDate)
<== Columns: SUM
<== Row: 1400
<== Total: 1
总结:实际上就是查询时数据库返回的列名在实体类找不到对应的字段,无法封装要查询的这个列和数据,所以get请求无法得到相应的data数据。文章来源地址https://www.toymoban.com/news/detail-697573.html
到了这里,关于解决mybatis,sql后端查询成功但postman测试返回的data却为null,空问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!