Bootstrap和Java分页实例第二篇(2)

package getui.controller; import getui.util.Pager; import java.util.Iterator; import java.util.List; import java.util.Map; import org.springframework.ui.Model; /** * 分页控件初始化父类 * * @author Anny */ public class BaseController { //初始化分页相关信息 protected void initPage(Map<String,Object> map, Integer pageNum, Integer pageSize, Integer totalCount){ if(null==pageSize || pageSize.equals("")){ pageSize = 10; //每页显示条数 } // if(pageSize>50){ // pageSize = 50; // } Integer totalPage = (totalCount+pageSize-1)/pageSize; if(null==pageNum){ pageNum = 1; }else if(pageNum>totalPage){ pageNum = totalPage; } map.put("startIndex", Pager.getStartIndex(pageNum, pageSize)); map.put("pageNum", pageNum); map.put("totalPage", totalPage); map.put("pageSize", pageSize); map.put("totalCount", totalCount); map.put("lastIndex", Pager.getLastIndex(pageSize,totalCount,totalPage,pageNum)); } //将相关数据放入model protected void initResult(Model model, List<Map<String, Object>> list, Map<String,Object> map){ model.addAttribute("list", list); Iterator it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry m = (Map.Entry)it.next(); model.addAttribute(m.getKey().toString(), m.getValue()); } } }

实例-Dao

package getui.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.stereotype.Repository; import getui.controller.GeTuiController; import getui.entity.Branch; import getui.entity.GetuiInfo; @Repository("bookDao") public class BookDao extends BaseDao{ /** 日志记录 .*/ private static Logger logger = Logger.getLogger(BookDao.class); public int getListCount(){ String sql ="select count(1) from Book"; return super.jdbcTemplate.queryForInt(sql); } public List getListBook(int pageIndex,int pageSize){ String sql = "select * from Book where 1=1 limit "+pageIndex+","+pageSize; // String sql = "select a.*" // + " from (" // +" select id as nid,name ,price" // +" from book" // +" order by(id) " // +" ) a" // +" where nid between "+pageIndex+" and "+pageSize+""; return super.jdbcTemplate.queryForList(sql); } }

实例-service

package getui.service; import getui.dao.BookDao; import getui.dao.ClientInfoDao; import getui.entity.Branch; import getui.entity.GetuiInfo; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("bookService") public class BookService { /** 日志记录 .*/ private static Logger logger = Logger.getLogger(BookService.class); @Autowired private BookDao bookDao; public int getListCount(){ return bookDao.getListCount(); } public List getListBook(int pageIndex,int pageSize){ return bookDao.getListBook(pageIndex, pageSize); // return bookDao.getListBook(); } }

实例-action

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwjszf.html