条件查询时,如果有的输入框不输入,后台应该怎么处理?

如图,如果我有的字段不输入,点击查询的话,后台应该怎么处理?

下面我来说两种做法,一个是我之前使用的,一个是新学到的方法,拿来给大家分享。

/*
* 第一种做法:
*/
public
Student searchStudent01(Student student) { // 准备sql语句 String sql = "select * from t_student where 1=1"; if (student.getName() != null || student.getName() != "") sql += " and name = " + student.getName(); if (student.getSex() != null || student.getSex() != "") sql += " and sex = " + student.getSex(); if (student.getAddress() != null || student.getAddress() != "") sql += " and address = " + student.getAddress(); if (student.getEmail() != null || student.getEmail() != "") sql += " and email = " + student.getEmail(); // 声明相关变量 ResultSet rs = null; Connection conn = null; PreparedStatement pstmt = null; try { conn = DbUtil.getConnection(); // 获取连接 pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if (rs.next()) return new Student(); // 这里只是模拟,不再对查询结果进行封装! } catch (Exception e) { e.printStackTrace(); } return null; // 没有查询结果时返回空 }
/*
* 第二种做法:
*/

public
Student searchStudent02(Student student) { // 准备sql语句,注意这里使用的是StringBuffer而不是String,具体原因看下边就知道了 StringBuffer sb = new StringBuffer("select * from t_student"); // 注意这里没有where 1=1 if (student.getName() != null || student.getName() != "") // 这里所有拼接的sql语句都是用的and而不是where,那么where判断语句在什么时候添加?往下看! sb.append(" and name = " + student.getName()); if (student.getSex() != null || student.getSex() != "") sb.append(" and sex = " + student.getSex()); if (student.getAddress() != null || student.getAddress() != "") sb.append(" and address = " + student.getAddress()); if (student.getEmail() != null || student.getEmail() != "") sb.append(" and email = " + student.getEmail()); // 声明相关变量 ResultSet rs = null; Connection conn = null; PreparedStatement pstmt = null; try { conn = DbUtil.getConnection(); // 获取连接 pstmt = conn.prepareStatement(sb.toString().replaceFirst("and", "where")); /* * 这里是关键,replaceFirst是String的方法,它的作用是:替换给定符串匹配给定的字符串的第一个子字符串。 * 这里有点绕,其是就是将sb.toString这个字符串第一个出现的"and"替换为"where",而且只替换第一个。 * 当然,如果你所有的查询条件都没有输入,也就是说后边一个拼接也没有,没有and,那么它就不会替换了。 */ rs = pstmt.executeQuery(); if (rs.next()) return new Student(); // 这里只是模拟,不再对查询结果进行封装! } catch (Exception e) { e.printStackTrace(); } return null; // 没有查询结果时返回空 }

两种方法的比较,我个人比较推崇第二种方法,而且在实践当中也是用的这种方法。表面上看起来这两种方法没有什么区别,但是需要注意了,第一种方法里的where 1=1。下面我们来谈一下where 1=1的弊端:

1) 当表中的数据比较多的时候,它的查询效率会很慢,因为它会对表中所有的内容进行查找。

2) 使用where 1=1作为过滤条件的话,数据库系统就无法使用索引等查询优化策略(不同的数据库系统不太一样),这样的话数据库系统就会被迫的对所有数据进行扫描(也就是全表扫描),所以,如果对性能要求比较高的情况下,尽量不要使用where 1=1。

posted @ 2016-11-27 10:57  我滴个小张张  阅读(517)  评论(0编辑  收藏  举报