java,JdbcTemplate,MySQL 查询,多个参数,怎么判断
展开全部
举例说明如下:
public List<Pin> searchPins(String q, String category, String c, Integer page, Integer perPage) {
q = "%" + q + "%";
if(perPage == null) {
perPage = 20;
}
if(page == null || page == 0) {
page = 0;
} else {
page = (page - 1) * perPage;
}
String arg0 = "";
String arg1 = "";
if(!StringUtils.isBlank(category)) {
arg0 = arg0 + ", tz_board b";
arg1 = arg1 + " AND b.deleting = 0 AND p.board_id = b.board_id AND b.category_id = ?";
q = q + ", " + category;
}
if(!StringUtils.isBlank(c)) {
arg0 = arg0 + ", tz_file f";
arg1 = arg1 + " AND p.file_id = f.file_id AND f.theme = ?";
q = q + ", " + c;
}
String sql = "SELECT p.* FROM tz_pin p" + arg0 + " WHERE p.deleting = 0 AND p.is_private = 0 AND p.raw_text LIKE ?" + arg1 + " ORDER BY p.pin_id DESC LIMIT ?,?";
RowMapper<Pin> rowMapper = new BeanPropertyRowMapper<Pin>(Pin.class);
System.out.println(sql);
System.out.println(q);
System.out.println(page);
System.out.println(perPage);
return jdbcTemplate.query(sql, rowMapper, q, page, perPage);
}
public List<Pin> searchPins(String q, String category, String c, Integer page, Integer perPage) {
q = "%" + q + "%";
if(perPage == null) {
perPage = 20;
}
if(page == null || page == 0) {
page = 0;
} else {
page = (page - 1) * perPage;
}
String arg0 = "";
String arg1 = "";
if(!StringUtils.isBlank(category)) {
arg0 = arg0 + ", tz_board b";
arg1 = arg1 + " AND b.deleting = 0 AND p.board_id = b.board_id AND b.category_id = ?";
q = q + ", " + category;
}
if(!StringUtils.isBlank(c)) {
arg0 = arg0 + ", tz_file f";
arg1 = arg1 + " AND p.file_id = f.file_id AND f.theme = ?";
q = q + ", " + c;
}
String sql = "SELECT p.* FROM tz_pin p" + arg0 + " WHERE p.deleting = 0 AND p.is_private = 0 AND p.raw_text LIKE ?" + arg1 + " ORDER BY p.pin_id DESC LIMIT ?,?";
RowMapper<Pin> rowMapper = new BeanPropertyRowMapper<Pin>(Pin.class);
System.out.println(sql);
System.out.println(q);
System.out.println(page);
System.out.println(perPage);
return jdbcTemplate.query(sql, rowMapper, q, page, perPage);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询