使用dbutils时出现cannot create xxx query情况解决

阅读: 评论:0

使用dbutils时出现cannot create xxx query情况解决

使用dbutils时出现cannot create xxx query情况解决

项目场景:

关于使用 DBUtils数据库 连接池使用查询语句出现的错误


问题描述:

当写好测试类时准备测试查询时出现了这个错误,

java.sql.SQLException: Cannot create com.ishop.pojo.Goods: com.ishop.pojo.Goods Query: select `id`,`sname`,`information`, `brand`, `img_path`, `sales`, `stock` from t_goods where `id` = ? Parameters: [4]

然后在语句在mysql中能正常运行,说明sql语句没有错误


在dao中写好测试类准备测试,发现出现了如上错误

实体类如下

public class Goods {private Integer id;          //商品编号private String sname;        //商品名public Goods(Integer id, String sname) {this.id = id;this.sname = sname;}//省略Getter 和 Setter 方法//省略toString方法
}

原因分析:

使用QueryRunner类的query方法时,传入的JavaBean类没有无参构造


解决方案:

原因:queryRunner.query(connection, sql, new BeanListHandler(type), args);
使用 new BeanHandler 还有 new BeanListHandler 时 ,传入的实体类加入无参构造即可

(1)里面的属性要有set方法

(2)类里面如果有带参数的改造方法,必须添加一个没有参数的构造方法

(3)查询语句sql = select id, sname, information, brand, img_path, sales, stock from t_goods where id = ?; 里面的参数保证类里面有同名的构造方法;

正确代码

public class Goods {private Integer id;          //商品编号private String sname;        //商品名public Goods(){}	//空构造public Goods(Integer id, String sname) {this.id = id;this.sname = sname;}//省略Getter 和 Setter 方法//省略toString方法
}

本文发布于:2024-01-29 17:26:24,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170652038617051.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:情况   create   dbutils   query   xxx
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23