String sql = "insert into order_item(order_id,food_id,food_servings) values(:Id,:foodId,:foodServings)";
JDBCTemplateHelper.add(sql, orderItem);
JAVA中SQL语句报错如下:
Invalid property 'Id' of bean class [com.newsong.JavaBean.OrderItem]: Bean property 'Id' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
上述报错,大意思是Id not readable 或者没有写setter,经过一番追踪
OrderItem orderItem = new OrderItem(orderId, foodId, servings);
oim.addOrderItem(orderItem);
OrderItem的构造方法:
public OrderItem(int orderId, int foodId, int foodServings) {
super();
this.orderId = orderId;
this.foodId = foodId;
this.foodServings = foodServings;
}
发现并没有Id,因而将文初的SQL语句中的:Id改为:orderId问题解决。如下:
String sql = "insert into order_item(order_id,food_id,food_servings) values(:orderId,:foodId,:foodServings)"; JDBCTemplateHelper.add(sql, orderItem);
总结:遇到这种问题,仔细看错误提示语句,然后再去排查。