mybatis分页插件:
1,maven包
<!-- 引入mybatis的 pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
springconfig配置文件引入
<!-- 引入 pageHelper插件 -->
<!--注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--reasonable:分页合理化参数,默认值为false,直接根据参数进行查询。
当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。-->
<!--<property name="reasonable" value="true"/>-->
</plugin>
<!--<plugin interceptor="com.realm.interceptor.PagePlugin"></plugin>-->
</plugins>
2,controller层
/** * 单表mybatis插件分页方法测试 * @param request * @return * @throws Exception */ @RequestMapping("/queryList") @ResponseBody public PageInfo<Notice> queryList(HttpServletRequest request) { PageInfo<Notice> pageInfo= new PageInfo<>(); Notice notice = new Notice(); String pageNums = request.getParameter("pageNum");//页码 String pageSizes = request.getParameter("pageSize");//当页显示条数 Integer pageNum=0; Integer pageSize=10; if(pageNums != null && !"".equals(pageNums)){ pageNum = Integer.parseInt(pageNums); } if(pageSizes != null && !"".equals(pageSizes)){ pageSize = Integer.parseInt(pageSizes); } pageInfo = iNoticeService.queryList(notice,pageNum,pageSize); return pageInfo; }
3,、serviceImpl层
@Override @Transactional public PageInfo<Notice> queryList(Notice notice, Integer pageNum, Integer pageSize){ //利用PageHelper分页查询 注意:这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效 PageHelper.startPage(pageNum,pageSize); List<Notice> list = noticeMapper.queryList(notice); PageInfo<Notice> pageInfo = new PageInfo<Notice>(list); return pageInfo; }
4,SQL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ns.dao.NoticeMapper">
<resultMap id="BaseResultMap" type="com.ns.entity.Notice">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="isdelete" jdbcType="INTEGER" property="isdelete" />
<result column="createtime" jdbcType="TIMESTAMP" property="createtime" />
<result column="createUser" jdbcType="VARCHAR" property="createUser" />
<result column="projectName" jdbcType="VARCHAR" property="projectName" />
<result column="opening_area" jdbcType="DECIMAL" property="opening_area" />
<result column="construction" jdbcType="VARCHAR" property="construction" />
<result column="plot_ratio" jdbcType="VARCHAR" property="plot_ratio" />
<result column="total_area" jdbcType="DECIMAL" property="total_area" />
<result column="area_of_plot_ratio" jdbcType="DECIMAL" property="area_of_plot_ratio" />
<result column="assessment" jdbcType="VARCHAR" property="assessment" />
<result column="research" jdbcType="VARCHAR" property="research" />
<result column="source" jdbcType="VARCHAR" property="source" />
<result column="project_summary" jdbcType="VARCHAR" property="project_summary" />
<result column="organization" jdbcType="VARCHAR" property="organization" />
<result column="intention" jdbcType="VARCHAR" property="intention" />
<result column="conditions" jdbcType="VARCHAR" property="conditions" />
<result column="cooperation" jdbcType="VARCHAR" property="cooperation" />
<result column="benefit" jdbcType="VARCHAR" property="benefit" />
<result column="proportion" jdbcType="VARCHAR" property="proportion" />
<result column="deadline" jdbcType="VARCHAR" property="deadline" />
<result column="instruction" jdbcType="VARCHAR" property="instruction" />
<result column="decision" jdbcType="VARCHAR" property="decision" />
<result column="plot" jdbcType="VARCHAR" property="plot" />
<result column="enterprise" jdbcType="VARCHAR" property="enterprise" />
<result column="filename" jdbcType="VARCHAR" property="filename" />
<result column="payment" jdbcType="CHAR" property="payment" />
<result column="trading_terms" jdbcType="VARCHAR" property="trading_terms" />
<result column="information" jdbcType="VARCHAR" property="information" />
<result column="complaint" jdbcType="VARCHAR" property="complaint" />
<result column="fax" jdbcType="VARCHAR" property="fax" />
<result column="url" jdbcType="VARCHAR" property="url" />
<result column="weChat_official" jdbcType="VARCHAR" property="weChat_official" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="is_partner" jdbcType="CHAR" property="is_partner" />
<result column="is_guarantee" jdbcType="CHAR" property="is_guarantee" />
<result column="is_client" jdbcType="CHAR" property="is_client" />
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
id, isdelete, DATE_FORMAT(createtime,'%Y-%m-%d') as createtime, createUser, projectName, opening_area, construction, plot_ratio,
total_area, area_of_plot_ratio, assessment, research, source, project_summary, organization,
intention, conditions, cooperation, benefit, proportion, deadline, instruction, decision,
plot, enterprise, filename, payment, trading_terms, information, complaint, fax,
url, weChat_official, address, is_partner, is_guarantee, is_client
</sql>
<select id="queryList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from notice
where isdelete=0
<if test="projectName !=null and projectName!=''">
and projectName like CONCAT('%',CONCAT(#{projectName},'%'))
</if>
</select>
</mapper>
记录一下平常遇到的问题及新的知识,方便以后查看