SpringBoot Mybatis many paging problem solved using PageHelper

  In general use  PageHelper  can solve most of the problems pagination, search the related use of garden blog, you can find a lot of information.

  Before I encountered such a problem when doing SpringBoot project, that is, when the case paged when many union query, use PageHelper can not be carried out on a page, but to do the query results page.

  Later, after to find relevant information, found a use PageHelper according to a joint inquiry to-many paging, a method is nested sub-queries, so the effect paged results is needed.

  The relevant code is hereby record it, memo.

  Common examples here use goods and commodities information, the establishment of two corresponding table in the MySql database,

The CREATE  TABLE `item` ( 
  ` item_id` bigint ( 20 ) the NOT  NULL AUTO_INCREMENT the COMMENT ' Product Code ' , 
  `img_url` VARCHAR ( 500 ) the NOT  NULL  the DEFAULT  '' the COMMENT ' picture address ' , 
  ` title` VARCHAR ( 1000 ) the NOT  NULL the COMMENT ' title ' , 
  `price` VARCHAR ( 500 ) the NOT  NULLThe COMMENT ' price ' , 
  `item_type` VARCHAR ( 30 ) the NOT  NULL the COMMENT ' category ' , 
  ` quantity` BIGINT ( 20 is ) the NOT  NULL the COMMENT ' number ' ,
   a PRIMARY  KEY ( `item_id`) 
) ENGINE = the InnoDB the AUTO_INCREMENT = . 1  the DEFAULT the CHARSET = the ROW_FORMAT UTF8 = the COMPACT the COMMENT = ' trade ' ;
CREATE TABLE `item_sku` (
  `sku_id` bigint(20) NOT NULL AUTO_INCREMENT  COMMENT '规格ID',
  `item_id` varchar(30) NOT NULL COMMENT '商品ID',
  `sku_price` varchar(100) NOT NULL DEFAULT '' COMMENT 'SKU价格',
  `sku_unique_code` varchar(100) NOT NULLThe COMMENT ' specifications unique identifier ' , 
  `quantity` BIGINT ( 20 is ) the NOT  NULL the COMMENT ' number ' ,
   a PRIMARY  KEY (` sku_id`) 
) ENGINE = the InnoDB the AUTO_INCREMENT = 0  the DEFAULT the CHARSET = UTF8 the ROW_FORMAT = the COMPACT the COMMENT = ' Product SKUs ' ;

  New model-related classes and interfaces and mapper xml file in your project, add resultMap in xml file 

Property field correspondence relationship described below  <-! Property field corresponding to the field name List itemSkus must focus as a result of: Private List <ItemSku> itemSkus; -> 

    <resultMap id="item" type="com.demo.dal.entity.pojo.Item">
        <result column="item_id" jdbcType="VARCHAR" property="itemId"/>
        <result column="img_url" jdbcType="VARCHAR" property="imgUrl"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="price" jdbcType="VARCHAR" property="price"/>
        <result column="item_type" jdbcType="VARCHAR" property="itemType"/>
        <result column="quantity" jdbcType="BIGINT" property="quantity"/>

        <collection property="itemSkus" ofType="com.demo.dal.entity.pojo.ItemSku"
                    javaType="java.util.List" select="getSkuByItemId"
                    column="{itemId=item_Id}"><!- columns to be queried in the parent in the querySELECT field {itemId = item_Id, quantity = quantity-->   <!--property字段对应的itemSkus必须在结果集中List的字段名 如:private List<ItemSku> itemSkus;-->
            <result column="sku_id" jdbcType="VARCHAR" property="skuId"/>
            <result column="sku_price" jdbcType="VARCHAR" property="skuPrice"/>
            <result column="sku_unique_code" jdbcType="VARCHAR" property="skuUniqueCode"/>
            <result column="quantity" jdbcType="BIGINT" property="quantity"/>
        </collection>
    </resultMap>

  Main query  selectItemAndSku 

<select id="selectItemAndSku" resultMap="item" parameterType="map">
        SELECT
        s1.item_id,
        s1.title,
        s1.img_url,
        s1.item_type,
        s1.price,
        s1.quantity,
        FROM
        item s1
        <where>
            <if test="title != null and title != ''">
                AND s1.title LIKE '%${title}%'
            </if>
            <if test="itemId != null and itemId != ''">
                AND s1.item_id = '${itemId}'
            </if>
        </where>
        order by item_id desc
    </select>

  Nested sub-queries  getSkuByItemId 

<select id="getSkuByItemId" parameterType="map"
            resultType="map">
select s2.sku_id,
        s2.sku_price,
        s2.sku_unique_code,
        s2.quantity,
from item_sku s2
where
s2.item_id = #{itemId}
ORDER BY s2.sku_id
</select>

  When these are complete, the new file is added at the interface mapper such a method:  List <Item> selectItemAndSku (the Map <String, Object> Map); 

  Then the service implementation class interface class can be injected into the mapper, and then use the tab to plug the paging.

Sample code:

  PageInfo<Item> pageInfo =
          PageHelper.startPage(page, pageSize)
              .doSelectPageInfo(
                  () ->
                      itemDao.selectItemAndSku(map);//JDK 8.0以上的语法
                     
  //List<Item> list = PageHelper.startPage(page, pageSize);
  //itemDao.selectItemAndSku(map);
  //PageInfo<Item> pageInfo = new PageInfo<>(list); //通用写法
pageInfo is paged out the results. Go out and see the results returned by controller ✿

Guess you like

Origin www.cnblogs.com/levywang/p/mybatis_one2many.html