1 StringBuilder sql = new StringBuilder("SELECT * FROM t_customer WHERE 1=1"); 2 /* 3 * 2. 判断条件,完成向sql中追加where子句 4 */ 5 /* 6 * 3. 创建一个ArrayList,用来装载参数值 7 */ 8 List<Object> params = new ArrayList<Object>(); 9 String cname = criteria.getCname(); 10 if(cname != null && !cname.trim().isEmpty()) { 11 sql.append(" and cname like ?"); 12 params.add("%" + cname + "%"); 13 } 14 15 String gender = criteria.getGender(); 16 if(gender != null && !gender.trim().isEmpty()) { 17 sql.append(" and gender=?"); 18 params.add(gender); 19 } 20 21 String cellphone = criteria.getCellphone(); 22 if(cellphone != null && !cellphone.trim().isEmpty()) { 23 sql.append(" and cellphone like ?"); 24 params.add("%" + cellphone + "%"); 25 } 26 27 String email = criteria.getEmail(); 28 if(email != null && !email.trim().isEmpty()) { 29 sql.append(" and email like ?"); 30 params.add("%" + email + "%"); 31 }