1. 程式人生 > >springMVC 整合 mybatis-paginator 實現分頁

springMVC 整合 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);

}