springMVC集成mybatis-paginator实现分页

springMVC集成mybatis-paginator实现分页

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

 

1、引入maven依赖

1
2
3
4
5
<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);


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值