RouteDao package cn.itcast.travel.dao; import cn.itcast.travel.domain.Route; import java.util.List; public interface RouteDao { /** * 根据id查询总记录数 * @param cid * @param rname * @return */ public int findTotalCount(int cid, String rname); /** * 根据cid,start,pageSize查询当前页的数据集合 * @param cid * @param start * @param pageSize * @param rname * @return */ public List<Route> findByPage(int cid, int start, int pageSize, String rname); /** * 根据id查询 * @param rid * @return */ public Route findOne(int rid); }
RouteDaoImpl 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; public class RouteDaoImpl implements RouteDao { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); @Override public int findTotalCount(int cid, String rname) { //String sql = "select count(*) from tab_route where cid = ?"; //定义SQL模板 String sql = "select count(*) from tab_route where 1=1 "; //判断参数是否有值 StringBuilder sb = new StringBuilder(sql); List params = new ArrayList();//条件 if (cid!=0){ sb.append( " and cid = ? "); params.add(cid);//添加?对应的值 } if (rname !=null && rname.length()>0 && !"null".equals(rname)){ sb.append(" and rname like ? "); params.add("%"+rname+"%"); } sql = sb.toString(); return template.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 ? , ?"; //定义SQL模板 String sql = "select * from tab_route where 1 = 1 "; //判断参数是否有值 StringBuilder sb = new StringBuilder(sql); List params = new ArrayList();//条件 if (cid!=0){ sb.append( " and cid = ? "); params.add(cid);//添加?对应的值 } if (rname !=null && rname.length()>0 && !"null".equals(rname)){ sb.append(" and rname like ? "); params.add("%"+rname+"%"); } sb.append(" limit ? , ? ");//分页条件 sql = sb.toString(); params.add(start); params.add(pageSize); return template.query(sql,new BeanPropertyRowMapper<Route>(Route.class),params.toArray()); } @Override public Route findOne(int rid) { String sql = "select * from tab_route where rid = ?"; return template.queryForObject(sql,new BeanPropertyRowMapper<Route>(Route.class),rid); } }
RouteService package cn.itcast.travel.service; import cn.itcast.travel.domain.PageBean; import cn.itcast.travel.domain.Route; /** * 线路Service */ public interface RouteService { /** * 根据类别进行分页查询 * @param cid * @param currentPage * @param pageSize * @return */ public PageBean<Route> pageQuery(int cid,int currentPage,int pageSize,String rname); /** * 根据id查询 * @param rid * @return */ public Route findOne(String rid); }
RouteServiceImpl package cn.itcast.travel.service.impl; import cn.itcast.travel.dao.FavoriteDao; import cn.itcast.travel.dao.RouteDao; import cn.itcast.travel.dao.RouteImgDao; import cn.itcast.travel.dao.SellerDao; import cn.itcast.travel.dao.impl.FavoriteDaoImpl; import cn.itcast.travel.dao.impl.RouteDaoImpl; import cn.itcast.travel.dao.impl.RouteImgDaoImpl; import cn.itcast.travel.dao.impl.SellerDaoImpl; import cn.itcast.travel.domain.PageBean; import cn.itcast.travel.domain.Route; import cn.itcast.travel.domain.RouteImg; import cn.itcast.travel.domain.Seller; import cn.itcast.travel.service.RouteService; import java.util.List; public class RouteServiceImpl implements RouteService { private RouteDao routeDao = new RouteDaoImpl(); private RouteImgDao routeImgDao = new RouteImgDaoImpl(); private SellerDao sellerDao = new SellerDaoImpl(); private FavoriteDao favoriteDao = new FavoriteDaoImpl(); @Override public PageBean<Route> pageQuery(int cid, int currentPage, int pageSize,String rname) { //封装PageBean PageBean<Route> pb = new PageBean<Route>(); //设置当前页码 pb.setCurrentPage(currentPage); //设置每页显示条数 pb.setPageSize(pageSize); //设置总记录数 int totalCount = routeDao.findTotalCount(cid,rname); pb.setTotalCount(totalCount); //设置当前页显示的数据集合 int start = (currentPage - 1) * pageSize;//开始的记录数 List<Route> list = routeDao.findByPage(cid,start,pageSize,rname); pb.setList(list); //设置总页数 = 总记录数/每页显示条数 int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize)+1; pb.setTotalPage(totalPage); return pb; } @Override public Route findOne(String rid) { //根据id去route表中查询route对象 Route route = routeDao.findOne(Integer.parseInt(rid)); //根据route的id查询图片集合信息 List<RouteImg> routeImgList = routeImgDao.findByRid(route.getRid()); //将集合设置到route对象 route.setRouteImgList(routeImgList); //根据route的id查询卖家的信息 Seller seller = sellerDao.findById(route.getSid()); route.setSeller(seller); //查询收藏次数 int count = favoriteDao.findCountByRid(route.getRid()); route.setCount(count); return route; } }标签:itcast,cn,展示,int,travel,cid,旅游网,import,黑马 From: https://www.cnblogs.com/wsfj/p/17154327.html