MyBatis数据分页的实现

来源:互联网 发布:政治考试软件 编辑:程序博客网 时间:2024/04/29 10:34

利用pagehelper实现MyBatis数据的分页功能。


在pom.xml中添加依赖:

<dependency>  <groupId>com.github.pagehelper</groupId>  <artifactId>pagehelper</artifactId>  <version>${pagehelper.version}</version></dependency>


mybatis配置文件添加:

<plugins>   <plugin interceptor="com.github.pagehelper.PageInterceptor">      <property name="param1" value="value1"/>   </plugin></plugins>
mapping中的sql

<!-- 日志信息的查询 --><select id="selectSelective" parameterType="java.util.Map" resultMap="BaseResultMap">   select    <include refid="Base_Column_List" />    from sys_log    <where>      <if test="type !=null and type != 0 ">           type = #{type, jdbcType=TINYINT}   </if>    <if test="status != null and status != 0">       AND state =  #{state, jdbcType=TINYINT}   </if>   <if test="host != null">       AND host = #{host, jdbcType=CHAR}   </if>   <if test="username != null ">       AND username = #{username, jdbcType=CHAR}   </if>    </where>    ORDER BY id desc</select>


数据分页的实现

@RequestMapping(value="/log", method = RequestMethod.GET, headers = "X-Requested-With=XMLHttpRequest")@ApiOperation(value = "日志信息查询", response = ModelAndView.class,       notes = "日志查询 操作类型 0 全部、 1 登入、 2 登出",produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)@ApiImplicitParams({   @ApiImplicitParam(name = "username", value = "帐号", required = false, dataType = "String", paramType = "query"),   @ApiImplicitParam(name = "type", value = "操作类型", required = false, dataType = "String",defaultValue="0", paramType = "query"),   @ApiImplicitParam(name = "host", value = "主机地址", required = false, dataType = "String", paramType = "query"),   @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "String",defaultValue="0", paramType = "query"),   @ApiImplicitParam(name = "pageSize", value = "每页显示两", required = true, dataType = "String",defaultValue="5", paramType = "query")})public ModelAndView getLog(HttpServletRequest request){   Map<String,Object> paramMap = new HashMap();   paramMap.put("type", Util.getIntParameter(request, "type", false, "SysLog.type", 0));   paramMap.put("usermane", Util.getTrimParameter(request, "usermane"));   PageData pageData = new PageData();   pageData = Util.getPage(request, pageData);   Page page = PageHelper.startPage(pageData.getPage(),pageData.getLimit(),true);   List<?> list = logService.selectSelective(paramMap);   pageData.setTotal(page.getTotal());   pageData.setItems(list);   return getResultPageView(pageData);}


结果展示

第一页


第二页



0 0
原创粉丝点击