關于sql語句的問題
if (command!=null&&!"".equals(command.trim())) {
sql.append(" and command=?");
paramList.add(command);
}
if (description!=null&&!"".equals(description.trim())) {
sql.append(" and description=like '%' ? '%'");
paramList.add(description);
}
這里面and前面為什么要空格,問號和百分號是干什么的,能詳細點么
2018-09-06
1、這個是sql拼接, 如果if語句中條件滿足了,就會和之前的sql拼接,舉個例子:select * from user where id=1,如果條件滿足不加空格的話就會變成“select * from user where id=1and command=?”,很明顯and和1連在一起了,是錯誤的
2、?是指帶你要輸入的數(shù)據(jù),這個是數(shù)據(jù)庫PreparedStatement預處理的操作,%?%舉個例子:比如模糊查詢想查userName, 那這個?代表你前臺傳過來的userName, 數(shù)據(jù)庫里有123,1124,3323,你輸入2,這三個值都能查出來,代表前面有任意字符,后面任意字符。