java 分页工具类

该代码示例展示了在SpringBoot应用中,如何通过Controller、Service、Dao和Mapper层实现产品评论爬取任务的分页查询。Controller接收请求并调用Service,Service再调用Dao,Dao利用Mapper查询数据库。Mapper中的SQL动态构建查询条件,包括对多个字段的过滤以及模糊搜索。PaginationVO用于封装分页信息和查询结果。
摘要由CSDN通过智能技术生成
Controller层
    @ApiOperation(value = "任务列表")
    @RequestMapping(value = "/taskList", method = RequestMethod.GET)
    public PaginationVO taskList(PaginationVO<ProductCommentCrawlTask> pageInfo, String searchValue, ProductCommentCrawlTask product) throws Exception {
        return productCommentService.productCommentCrawlTaskList(pageInfo, searchValue, product);
    }
Service层
    @Override
    public PaginationVO productCommentCrawlTaskList(PaginationVO<ProductCommentCrawlTask> pageInfo, String searchValue, ProductCommentCrawlTask comment) throws Exception {
        Map<String, Object> paramMap = MapUtil.beansToMap(comment);
        paramMap.put("searchValue", searchValue);
        Long totalCount = productCommentDao.countByMap(paramMap);
        List<ProductCommentCrawlTask> list = new ArrayList<>();
        if (!totalCount.equals(0L)) {
            PageHelper.startPage(pageInfo.getPage(), pageInfo.getPageSize());
            list = productCommentDao.listCommentCrawlByMap(paramMap);
        }
        return new PaginationVO<>(pageInfo.getPage(), pageInfo.getPageSize(), totalCount, list);
    }

Dao层

Long countByMap(Map<String, Object> params);

List<ProductCommentCrawlTask> listCommentCrawlByMap(Map<String, Object> paramMap);

Mapper

<sql id="baseColumns">
        id, product_id, sku, product_title, product_url, product_image, target_product_id, target_product_url, target_platform, creator_id,
        creator, create_time, updater_id, updater, update_time, status, message, push_status
</sql>

    <sql id="baseInsertColumns">
        product_id, sku, product_title, product_url, product_image, target_product_id, target_product_url, target_platform, creator_id,
        creator, create_time, updater_id, updater, update_time, status, message, push_status
    </sql>

<sql id="queryByMapSql">
        <where>
            <if test="sku != null and sku !=''">sku=#{sku}</if>
            <if test="productId != null and productId !=''">and product_id=#{productId}</if>
            <if test="productTitle != null and productTitle !=''">and product_title=#{productTitle}</if>
            <if test="productUrl != null and productUrl != ''">and product_url=#{productUrl}</if>
            <if test="productImage != null and productImage != ''">and product_image=#{productImage}</if>
            <if test="targetProductId != null and targetProductId !=''">and target_product_id=#{targetProductId}</if>
            <if test="targetPlatform != null and targetPlatform !='' ">and target_platform=#{targetPlatform}</if>
            <if test="targetProductUrl != null and targetProductUrl != ''">and target_product_url=#{targetProductUrl}
            </if>
            <if test="creatorId != null and creatorId != ''">and creator_id=#{creatorId}</if>
            <if test="creator != null and creator !=''">and creator=#{creator}</if>
            <if test="createTime != null">and create_time=#{createTime}</if>
            <if test="updateTime !=null ">and update_time=#{updateTime}</if>
            <if test="updaterId != null and updaterId !=''">and updater_id=#{updaterId}</if>
            <if test="updater != null and updater != ''">and updater=#{updater}</if>
            <if test="status != null ">and status=#{status}</if>
            <if test="pushStatus != null ">and push_status=#{pushStatus}</if>
            <if test="searchValue != null and searchValue != ''">
                AND (
                product_title LIKE CONCAT('%', CONCAT(#{searchValue},'%'))
                OR target_product_id LIKE CONCAT('%', CONCAT(#{searchValue},'%'))
                OR product_url LIKE CONCAT('%', CONCAT(#{searchValue},'%'))
                OR target_platform LIKE CONCAT('%', CONCAT(#{searchValue},'%'))
                OR sku LIKE CONCAT('%', CONCAT(#{searchValue},'%'))
                OR target_product_url LIKE CONCAT('%', CONCAT(#{searchValue},'%'))
                OR product_id LIKE CONCAT('%', CONCAT(#{searchValue},'%'))
                )
            </if>
        </where>
    </sql>


<select id="countByMap" resultType="java.lang.Long">
        select count(1) from zjz_comment_crawl_task
        <include refid="queryByMapSql"/>
</select>


<select id="listCommentCrawlByMap" resultMap="baseResultMap">
        select
        <include refid="baseColumns"/>
        from zjz_comment_crawl_task
        <include refid="queryByMapSql"/>
        <include refid="defaultOrderSql"/>
</select>

util类



import java.io.Serializable;
import java.util.Collection;

/**
 * 
 * <p>
 * 分页实体VO类:
 * </p>
 */
@SuppressWarnings({ "serial", "unused" })
public class PaginationVO<T> implements Serializable, Cloneable {

	private int pageSize = 20; // 每页显示的行数,默认20条
	private int page = 1; // 当前页号,默认第一页
	private long totalPages; // 总页数
	private long total; // 记录数
	private String sort;
	private String direction;
	private Collection<T> data; // 数据

	public PaginationVO() {
	}

	public PaginationVO(Collection<T> data) {
		this.data = data;
	}

	public PaginationVO(Integer page, Integer pageSize) {
		this.page = page == null ? this.page : page;
		this.pageSize = pageSize == null ? this.pageSize : pageSize;
	}

	public PaginationVO(Integer page, Integer pageSize, Long total,
			Collection<T> data) {
		this(page, pageSize);
		this.total = total == null ? 0 : total;
		this.data = data;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public void setData(Collection<T> data) {
		this.data = data;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public long getTotalPages() {
		return (this.total % this.pageSize == 0) ? this.total
				/ this.pageSize : this.total / this.pageSize + 1;
	}

	public void setTotalPages(long totalPages) {
		this.totalPages = totalPages;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDirection() {
		return direction;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public Collection<T> getData() {
		return data;
	}

}
package com.efe.ms.common.util;

import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Map工具类
 * 
 */
public class MapUtil {
	/**
	 * javaBean 转 Map
	 * 
	 * @param object
	 *            需要转换的javabean
	 * @return 转换结果map
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	public static Map<String, Object> beanToMap(Object object) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		Class cls = object.getClass();
		Field[] fields = cls.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			map.put(field.getName(), field.get(object));
		}
		return map;
	}
	
	@SuppressWarnings("rawtypes")
	public static Map<String, String> beanToStringMap(Object object) throws Exception {
		Map<String, String> map = new HashMap<>();
		
		Class cls = object.getClass();
		Field[] fields = cls.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			map.put(field.getName(), field.get(object) == null ? null : field.get(object).toString());
		}
		return map;
	}

	public static Map<String, Object> beansToMap(Object... objects)
			throws Exception {
		final Map<String, Object> map = new HashMap<String, Object>();
		if (objects == null) {
			return map;
		}
		for (Object obj : objects) {
			map.putAll(beanToMap(obj));
		}
		return filterNullValueKeys(map);
	}

	private static Map<String, Object> filterNullValueKeys(Map<String, Object> map) {
		if (map != null) {
			Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
			Entry<String, Object> obj = null;
			while(iterator.hasNext()){
				obj = iterator.next();
				if(obj.getKey() == null || obj.getValue() == null){
					iterator.remove();
				}
			}
		}
		return map;
	}

	/**
	 *
	 * @param map
	 *            需要转换的map
	 * @param cls
	 *            目标javaBean的类对象
	 * @return 目标类object
	 * @throws Exception
	 */
	public static <T> T mapToBean(Map<String, Object> map, Class<T> cls)
			throws Exception {
		T ins = cls.newInstance();
		for (String key : map.keySet()) {
			Field temField = cls.getDeclaredField(key);
			temField.setAccessible(true);
			temField.set(ins, map.get(key));
		}
		return ins;
	}

	public static Map<String, String> convertJSON2Map(JSONObject jo) {
		Map<String, String> map = new HashMap<String, String>();
		Iterator<java.util.Map.Entry<String, Object>> ite = jo.entrySet().iterator();
		Map.Entry<String, Object> entry = null;
		while (ite.hasNext()) {
			entry = ite.next();
			map.put(entry.getKey(), entry.getValue() == null ? "" : entry.getValue().toString());
		}
		return map;
	}
}

实体类

@Data
public class ProductCommentCrawlTask extends BaseModel {

    private String productId;

    private String sku;

    private String productTitle;

    private String productUrl;

    private String productImage;

    private String targetProductId;

    private String targetProductUrl;

    private String targetPlatform;

    private String creatorId;

    private String creator;

    private String updaterId;

    private String updater;

    private Short status;

    private String message;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值