springMVC集成mybatis-paginator实现分页

mybatis-paginator下载地址:https://github.com/miemiedev/mybatis-paginator

1、引入maven依赖

        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
            <version>1.2.17</version>
        </dependency>        

2、spring配置文件添加分页插件:

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-setting.xml"/>
        <property name="mapperLocations" value="classpath*:com/tianwen/nlp/mapping/*.xml"></property>
        <property name="plugins">
            <list>
                <bean class="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
                    <property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.MySQLDialect"/>
                 </bean>
            </list>
        </property>
    </bean>

    
    
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.tianwen.nlp.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

3、下面添加一分页插件调用事例

3.1、controller层方法

//分页查询回访记录列表
    @RequestMapping("/contactList")
    public String contactList(Model model,
            @RequestParam(value="customerId", required=false)String customerId,
            @RequestParam(value="remark", required=false)String remark,
            @RequestParam(value="page", defaultValue="1")Integer curPage,
            @RequestParam(value="pageSize", defaultValue="20")Integer pageSize) {
        Map<String, Object> whereMap = new HashMap<String, Object>();
        whereMap.put("customerId", customerId);
        whereMap.put("remark", remark);
        PageBounds pb = new PageBounds(curPage, pageSize, Order.formString("id.desc"));
        PageList<ContactRecord> pageList= contactService.queryPageContactRecord(whereMap, pb);
        Page page = new Page(curPage, pageList.getPaginator().getTotalCount(), pageSize);    //根据当前页码、总记录数、每页记录数构造page对象
        model.addAttribute("data", pageList);
        model.addAttribute("page", page);
        return "contact/contactList";
    }

3.2、service层方法

    @Override
    public PageList<ContactRecord> queryPageContactRecord(
            Map<String, Object> whereMap, PageBounds pb) {
        return recordMappert.selectPageList(whereMap, pb);
    }

3.3、dao层接口方法及其xmp配置

  <sql id="whereCondition">
      <if test="customerId != null and !&quot;&quot;.equals(customerId.trim())">
          and customer_id like concat('%',trim(#{customerId}),'%')
      </if>
      <if test="remark != null and !&quot;&quot;.equals(remark.trim())">
          and REMARK like concat('%',trim(#{remark}),'%')
      </if>
  </sql>
  
  <!-- 分页查询回访记录 -->
  <select id="selectPageList" parameterType="map" resultMap="BaseResultMap">
      select * from contact_record
      <where>
          <include refid="whereCondition"></include>
      </where>
      <if test="groupBy != null and !&quot;&quot;.equals(groupBy.trim())">
          group by ${groupBy}
      </if>
  </select>
public interface ContactRecordMapper {

    PageList<ContactRecord> selectPageList(Map<String, Object> whereMap,
            PageBounds pb);


}
原文地址:https://www.cnblogs.com/yinz/p/7109713.html