MyBatis動態查詢,分頁查詢,List/Array/Map查詢.
阿新 • • 發佈:2018-12-17
- 動態查詢
xml中程式碼 <select id="Selective" parameterType="com.***.***.entity.Doctor" resultMap="BaseResultMap"> select * from doctor <where> <if test="id != null"> AND id = #{id,jdbcType=VARCHAR} </if> <if test="name != null"> AND name = #{name,jdbcType=CHAR} </if> </where> </select> id為對映類中方法名稱,where內條件根據需求增減.
- 分頁查詢
首先實體類繼承一個基類.
實體類中建立基類BaseEntity public BaseEntity() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); String pageSizeStr = request.getParameter("pageSize"); String pageNoStr = request.getParameter("pageNo"); if (StringUtils.isNotBlank(pageSizeStr)) { int size = Integer.parseInt(pageSizeStr); this.pageSize = size > 0 ? size : PAGE_SIZE; } else { this.pageSize = PAGE_SIZE; } if (StringUtils.isNotBlank(pageNoStr)) { int number = Integer.parseInt(pageNoStr); this.pageNo = number > 0 ? number - 1 : PAGE_NO; } else { this.pageNo = PAGE_NO; } this.beginNo = pageNo * pageSize; } } 需要分頁查詢的使其實體類繼承BaseEntity,實體類就會含有pageNo(頁碼),pageSize(每頁條數),beginNo(查詢起始條數)屬性.
controller層傳入的引數需要含有pageNo,pageSize屬性.
xml中程式碼 <select id="selectLimit" parameterType="com.***.***.entity.Doctor" resultMap="BaseResultMap"> select * from doctor <where> ************** </where> limit #{beginNo},#{pageSize} </select>