2020.05.17 模糊查询下的分页查询
Posted aojie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020.05.17 模糊查询下的分页查询相关的知识,希望对你有一定的参考价值。
package cn.itcast.travel.dao.impl;
import cn.itcast.travel.dao.RouteDao;
import cn.itcast.travel.domain.Route;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.List;
/**
* @author aojie
* @fuction
* @create 2020-05-15 22:15
*/
public class RouteDaoImpl implements RouteDao {
private JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public int findTotalCount(int cid, String rname) {
// String sql="select count(*) from tab_route where cid=?";
String sql="select count(*) from tab_route where 1=1 ";
StringBuilder builder=new StringBuilder(sql);
//用来判断参数是否有值
List params=new ArrayList();
if (cid!=0){
builder.append(" and cid=? ");
//添加?对应的值
params.add(cid);
}
if (rname.equals("")){
builder.append(" and rname like ?");
params.add("%"+rname+"%");
}
sql=builder.toString();
return jdbcTemplate.queryForObject(sql,Integer.class,params.toArray());
}
@Override
public List<Route> findByPage(int cid, int start, int pageSize, String rname) {
// String sql="select * from tab_route where cid=? limit ? , ? ";
String sql="select * from tab_route where 1=1 ";
StringBuilder builder=new StringBuilder(sql);
List params=new ArrayList();
if (cid!=0){
builder.append(" and cid=? ");
//添加?对应的值
params.add(cid);
}
if (rname.equals("")){
builder.append(" and rname like ?");
params.add("%"+rname+"%");
}
builder.append(" limit ? , ?");
sql=builder.toString();
params.add(start);
params.add(pageSize);
return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Route>(Route.class),params.toArray());
}
@Override
public Route findOne(int rid) {
String sql="select * from tab_route where rid=?";
return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Route>(Route.class),rid);
}
}
以上是关于2020.05.17 模糊查询下的分页查询的主要内容,如果未能解决你的问题,请参考以下文章