今天在测试模糊查询时,sql语句自己测试明明没有错,debug了半天,终于发现问题所在。
原因:模糊查询进行了转义 %'字段'% 的值加上了 '' 导致查询报错
解决办法:String newUsername ="%"+username+"%"; 将要传入的参数直接加上%%,作为一个整体参数传入
@Override
public List<User> getUserByCondition(String username, String gendar) throws SQLException {
String sql = "select u_id as uid , u_name as username , u_password as upassword, u_sex as usex , " +
"u_status as ustatus , u_code as code , u_email as email, u_role as urole from user ";
if(StringUtils.isEmpty(gendar) && !StringUtils.isEmpty(username)){
sql += " where u_name like ?";
String newUsername = "%" + username + "%";
return queryRunner.query(sql,new BeanListHandler<>(User.class),newUsername);
}else if(!StringUtils.isEmpty(gendar) && StringUtils.isEmpty(username)){
sql += " where u_sex = ?";
return queryRunner.query(sql,new BeanListHandler<>(User.class),gendar);
}else if(!StringUtils.isEmpty(gendar) && !StringUtils.isEmpty(username)){
sql += " where u_name like ? and u_sex = ?";
String newUsername = "%" + username + "%";
System.out.println(sql);
System.out.println(sql);
return queryRunner.query(sql,new BeanListHandler<>(User.class),newUsername,gendar);
}else{
return queryRunner.query(sql,new BeanListHandler<>(User.class));
}