首先来一下代码
StringBuffer sql = new StringBuffer();
sql.append("select");
sql.append(" f.id,");
sql.append(" f.foodName,");
sql.append(" f.price,");
sql.append(" f.mprice,");
sql.append(" f.intro,");
sql.append(" f.img,");
sql.append(" t.id,");
sql.append(" t.typeName ");
sql.append("from ");
sql.append(" OS_foodInfo f, ");
sql.append(" OS_foodType t ");
sql.append("where 1=1");
sql.append(" and f.foodType_id=t.id ");
List<Object> list = new ArrayList<Object>();
if (foodType_id > 0) {
sql.append("and f.foodType_id = ? ");
list.add(foodType_id);
}
if (foodName != null && !"".equals(foodName.trim())) {
sql.append("and f.foodName like '%");
sql.append("?");
sql.append("%' ");
list.add(foodName);
}
sql.append("limit ?,?");
然后来运行后的错误
java.sql.SQLException: Wrong number of parameters: expected 3, was given 4 Query: select f.id, f.foodName, f.price, f.mprice, f.intro, f.img, t.id, t.typeName from OS_foodInfo f, OS_foodType t where 1=1 and f.foodType_id=t.id and f.foodType_id = ? and f.foodName like '%?%' limit ?,? Parameters: [1, 虾, 0, 6]
是模糊拼接有问题吗?
Three question marks and you gave four parameters
Can’t you find it if you output the concatenated
sql
string?Obviously, it is your SQL error. f.foodType_id = f.id AND f.foodType_id = 1 ?
Why don’t you use the mybatis framework and still use this painful way to spell sql