配置xml文件
<resultMap id="BaseResultMap" type="com.huyuqiang.vo.ptNotice.PtNoticeVo"> <id column="NOTICE_ID" property="noticeId" jdbcType="VARCHAR"/> <result column="NOTICE_TITLE" property="noticeTitle" jdbcType="VARCHAR"/> <result column="DATETIME" property="datetime" jdbcType="TIMESTAMP"/> <result column="TYPE" property="type" jdbcType="VARCHAR"/> </resultMap> <!-- 全查 分頁 模糊--> <select id="selectAll" resultMap="BaseResultMap" parameterType="com.huyuqiang.vo.ptNotice.PtNoticeVo"> SELECT NOTICE_ID,NOTICE_TITLE,TYPE,DATETIME FROM pt_notice <where> <if test=" ptNoticeVo.type != null and ptNoticeVo.type != '' "> <bind name="fType" value=" '%' + ptNoticeVo.type + '%' "></bind> type like #{fType} </if> <if test=" ptNoticeVo.noticeTitle != null and ptNoticeVo.noticeTitle != '' "> <bind name="noticeTitlef" value=" '%' + ptNoticeVo.noticeTitle + '%' "></bind> AND NOTICE_TITLE like #{noticeTitlef} </if> </where> limit #{startIndex},#{pageSize} </select> <!--統計總數--> <select id="count" resultType="int" parameterType="com.huyuqiang.vo.ptNotice.PtNoticeVo"> select count(*) from pt_notice <where> <if test=" ptNoticeVo.type != null and ptNoticeVo.type != '' "> <bind name="fType" value=" '%' + ptNoticeVo.type + '%' "></bind> type like #{fType} </if> <if test=" ptNoticeVo.noticeTitle != null and ptNoticeVo.noticeTitle != '' "> <bind name="noticeTitlef" value=" '%' + ptNoticeVo.noticeTitle + '%' "></bind> AND NOTICE_TITLE like #{noticeTitlef} </if> </where> </select>
dao
List<PtNoticeVo> selectAll(@Param("ptNoticeVo") PtNoticeVo ptNoticeVo, @Param("startIndex") int startIndex, @Param("pageSize") int pageSize); int count(@Param("ptNoticeVo") PtNoticeVo ptNoticeVo);
@param是必須的 不然會報錯
在企業開發中 每個程序員在編寫自己模塊的時候 基本都會自己創建相應的vo 也可以叫 entity 盡可能的不去改動mybatis逆向出來的原生實體類 vo可以根據自己的需要創建屬性 當然它就是一個實體類 封裝 無參構造 有參構造 甚至於toString 都應該具有
因為一個實體類被兩個人改動那就會引起不必要的沖突 而且原生實體類包含了數據庫表中所有字段對映的屬性 可是我們在前端顯示的時候往往並不需要展示出所有的屬性
尤其是在springMVC中 vo可以用來接收前端傳送過來的參數 前提是vo里所包含的某個屬性和前端參數中的某個屬性 類型 名稱 一致
同時也可以把一個vo返回給前端
PtNoticeVo代碼如下:
import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; public class PtNoticeVo implements Iterator<Object> { private String noticeId; private String noticeTitle; private String type; private Date datetime; public PtNoticeVo(String noticeId, String noticeTitle, String type, Date datetime) { this.noticeId = noticeId; this.noticeTitle = noticeTitle; this.type = type; this.datetime = datetime; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Date getDatetime() { return datetime; } public void setDatetime(Date datetime) { this.datetime = datetime; } public PtNoticeVo() { } public String getNoticeId() { return noticeId; } public void setNoticeId(String noticeId) { this.noticeId = noticeId; } public String getNoticeTitle() { return noticeTitle; } public void setNoticeTitle(String noticeTitle) { this.noticeTitle = noticeTitle; } @Override public boolean hasNext() { return false; } @Override public Object next() { return null; } }
pageVo代碼如下
import java.util.Date; import java.util.List; public class PageVo<T> { private Integer code = 200; //接口狀態碼 // 當前頁 private Integer currentPage = 1; // 每頁顯示的總條數 private Integer pageSize = 10; // 總條數 private Integer totalNum; // 是否有下一頁 private Integer isMore; // 總頁數 private Integer totalPage; // 開始索引 private Integer startIndex; // 分頁結果 private List<T> items; public PageVo() { super(); } public PageVo(Integer currentPage, Integer pageSize, Integer totalNum) { super(); if (currentPage != null && currentPage > 0) { this.currentPage = currentPage; } if (pageSize != null && pageSize > 0) { this.pageSize = pageSize; } this.totalNum = totalNum; this.totalPage = (this.totalNum + this.pageSize - 1) / this.pageSize; this.startIndex = (this.currentPage - 1) * this.pageSize; this.isMore = this.currentPage >= this.totalPage ? 0 : 1; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotalNum() { return totalNum; } public void setTotalNum(Integer totalNum) { this.totalNum = totalNum; } public Integer getIsMore() { return isMore; } public void setIsMore(Integer isMore) { this.isMore = isMore; } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public Integer getStartIndex() { return startIndex; } public void setStartIndex(Integer startIndex) { this.startIndex = startIndex; } public List<T> getItems() { return items; } public void setItems(List<T> items) { this.items = items; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } }
這里說一下PageVo
前端傳送給你用來分頁的出去模糊查詢之外 一般只有兩個參數 一個是當前頁也就是要查找第幾頁 上面的"currentPag" 和每頁顯示幾條的“pageSize”
我們通過sql是可以查出來總條數的 有了總條數 “totalNum ” 就可以查出總頁數 所有上面用
this.totalPage = (this.totalNum + this.pageSize - 1) / this.pageSize;
按照常理 用總條數除以每頁顯示幾條就可以得到總頁數 但是仔細想想 這是難以完成整除的 但是頁數要取整 例如 45條/10條每頁顯示 在java中獲得的整數是“4”
所有才出現了上面的求總頁數的方法
mysql分頁 是根據索引和每頁顯示條數 兩個參數進行分頁的
所以我們要求索引 所謂索引 就是從你查詢出來的數據的第幾條開始顯示
那怎么求出從第幾條開始顯示呢
如果我們要查詢第五頁 每頁顯示10行
那就是從第41個開始顯示 顯示到第五十個
this.startIndex = (this.currentPage - 1) * this.pageSize;
這行代碼就解決了這個問題 當前頁減去1乘以每頁顯示條數 就是你分頁的索引值
如下是service層代碼
@Service public class PtNoticeServiceImpl implements PtNoticeService { @Autowired PtNoticeMapper ptNoticeMapper; @Override public PageVo<PtNoticeVo> selectAll(PtNoticeVo ptNoticeVo,int currentpage,int pageSize) { int totalNum; if(pageSize<=0){ totalNum = pageSize; }else { totalNum = ptNoticeMapper.count(ptNoticeVo); } PageVo<PtNoticeVo> pageVo = new PageVo<>(currentpage,pageSize,totalNum); pageVo.setItems(ptNoticeMapper.selectAll(ptNoticeVo,pageVo.getStartIndex(),pageSize)); pageVo.setTotalNum(totalNum); return pageVo; } }
controller層代碼
@RestController @RequestMapping("api/ptNotice") public class PtNoticeController { @Autowired PtNoticeService ptNoticeService; @RequestMapping("/selectAll") PageVo<PtNoticeVo> select(PtNoticeVo ptNoticeVo, int currentPag, int pageSize) { System.out.println(ptNoticeVo.getType()); return ptNoticeService.selectAll(ptNoticeVo, currentPag, pageSize); } }