文档章节

spring jpa的随机 排序使用

林伟琨
 林伟琨
发布于 2016/08/17 10:48
字数 1261
阅读 169
收藏 2

输入图片说明 输入图片说明

这边的page 开始应该是0  不是1



重写的pageRequest


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import com.google.common.collect.Lists;


public class PageRequest implements Serializable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 9100768518781654128L;

	/** 搜索关键词 */
	private String searchText = "";

	/** The page no. */
	protected int pageNo = 1;

	/** The page size. */
	protected int pageSize = 10;

	/** The order by. */
	protected String orderBy = null;

	/** The order dir. */
	protected String orderDir = null;

	/** The order type. */
	protected String orderType = null;

	/** The count total. */
	protected boolean countTotal = true;

	/** The sort. */
	protected Sort sort;

	/** The search debug. */
	protected boolean searchDebug = false;

	/**
	 * Instantiates a new page request.
	 */
	public PageRequest() {
	}

	/**
	 * Instantiates a new page request.
	 * 
	 * @param pageNo
	 *            the page no
	 * @param pageSize
	 *            the page size
	 */
	public PageRequest(int pageNo, int pageSize) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
	}

	/**
	 * Gets the page no.
	 * 
	 * @return the page no
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * Sets the page no.
	 * 
	 * @param pageNo
	 *            the new page no
	 */
	public void setPageNo(final int pageNo) {
		this.pageNo = pageNo;

		if (pageNo < 1) {
			this.pageNo = 1;
		}
	}

	/**
	 * Gets the page size.
	 * 
	 * @return the page size
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * Sets the page size.
	 * 
	 * @param pageSize
	 *            the new page size
	 */
	public void setPageSize(final int pageSize) {
		this.pageSize = pageSize;

		if (pageSize < 1) {
			this.pageSize = 1;
		}
	}

	/**
	 * Gets the order by.
	 * 
	 * @return the order by
	 */
	public String getOrderBy() {
		return orderBy;
	}

	/**
	 * Sets the order by.
	 * 
	 * @param orderBy
	 *            the new order by
	 */
	public void setOrderBy(final String orderBy) {
		this.orderBy = orderBy;
	}

	/**
	 * Gets the order dir.
	 * 
	 * @return the order dir
	 */
	public String getOrderDir() {
		return orderDir;
	}

	/**
	 * Sets the order dir.
	 * 
	 * @param orderDir
	 *            the new order dir
	 */
	public void setOrderDir(final String orderDir) {
		if (StringUtils.isNotBlank(orderDir)) {
			String lowcaseOrderDir = StringUtils.lowerCase(orderDir);

			// 检查order字符串的合法值
			String[] orderDirs = StringUtils.split(lowcaseOrderDir, ',');
			for (String orderDirStr : orderDirs) {
				if (!StringUtils.equalsIgnoreCase(Direction.DESC.name(), orderDirStr) && !StringUtils.equalsIgnoreCase(Direction.ASC.name(), orderDirStr)) {
					throw new IllegalArgumentException("排序方向" + orderDirStr + "不是合法值");
				}
			}

			this.orderDir = lowcaseOrderDir;
		}
	}

	/**
	 * Gets the order type.
	 * 
	 * @return the order type
	 */
	public String getOrderType() {
		return orderType;
	}

	/**
	 * Sets the order type.
	 * 
	 * @param orderType
	 *            the new order type
	 */
	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

	/**
	 * Gets the sort.
	 * 
	 * @return the sort
	 */
	public Sort getSort() {
		List<Order> orders = Lists.newArrayList();
		if (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(orderDir)) {
			String[] orderBys = StringUtils.split(orderBy, ',');
			String[] orderDirs = StringUtils.split(orderDir, ',');
			Validate.isTrue(orderBys.length == orderDirs.length, "分页多重排序参数中,排序字段与排序方向的个数不相等");
			for (int i = 0; i < orderBys.length; i++) {
				orders.add(new Order(Direction.fromString(orderDirs[i]), orderBys[i]));
			}
		}
		if (this.sort != null) {
			orders.addAll(this.sort.orders);
		}
		if (!orders.isEmpty())
			return new Sort(orders);
		return new Sort();
	}

	/**
	 * Sets the sort.
	 * 
	 * @param sort
	 *            the new sort
	 */
	public void setSort(Sort sort) {
		this.sort = sort;
	}

	/**
	 * Checks if is order by setted.
	 * 
	 * @return true, if is order by setted
	 */
	public boolean isOrderBySetted() {
		return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(orderDir));
	}

	/**
	 * Checks if is count total.
	 * 
	 * @return true, if is count total
	 */
	public boolean isCountTotal() {
		return countTotal;
	}

	/**
	 * Sets the count total.
	 * 
	 * @param countTotal
	 *            the new count total
	 */
	public void setCountTotal(boolean countTotal) {
		this.countTotal = countTotal;
	}

	/**
	 * Gets the offset.
	 * 
	 * @return the offset
	 */
	public int getOffset() {
		return ((pageNo - 1) * pageSize);
	}

	/**
	 * The Enum Direction.
	 * 
	 * @author
	 */
	public static enum Direction {

		/** The ASC. */
		ASC,

		/** The DESC. */
		DESC;

		/**
		 * From string.
		 * 
		 * @param value
		 *            the value
		 * @return the direction
		 */
		public static Direction fromString(String value) {

			try {
				return Direction.valueOf(value.toUpperCase(Locale.US));
			} catch (Exception e) {
				throw new IllegalArgumentException(String.format("Invalid value '%s' for orders given! Has to be either 'desc' or 'asc' (case insensitive).", value), e);
			}
		}
	}

	/**
	 * The Class Order.
	 * 
	 * @author
	 */
	public static class Order implements Serializable {

		/** The Constant serialVersionUID. */
		private static final long serialVersionUID = 1522511010900108987L;

		/** The direction. */
		private Direction direction;

		/** The property. */
		private String property;

		/**
		 * Instantiates a new order.
		 */
		public Order() {
			super();
		}

		/**
		 * Instantiates a new order.
		 * 
		 * @param direction
		 *            the direction
		 * @param property
		 *            the property
		 */
		public Order(Direction direction, String property) {

			// if (property == null || "".equals(property.trim())) {
			// throw new
			// IllegalArgumentException("PropertyPath must not null or empty!");
			// }

			this.direction = direction == null ? Sort.DEFAULT_DIRECTION : direction;
			this.property = property;
		}

		/**
		 * Instantiates a new order.
		 * 
		 * @param property
		 *            the property
		 */
		public Order(String property) {
			this(Sort.DEFAULT_DIRECTION, property);
		}

		/**
		 * Creates the.
		 * 
		 * @param direction
		 *            the direction
		 * @param properties
		 *            the properties
		 * @return the list
		 */
		public static List<Order> create(Direction direction, Iterable<String> properties) {

			List<Order> orders = new ArrayList<Order>();
			for (String property : properties) {
				orders.add(new Order(direction, property));
			}
			return orders;
		}

		/**
		 * Gets the direction.
		 * 
		 * @return the direction
		 */
		public Direction getDirection() {
			return direction;
		}

		/**
		 * Gets the property.
		 * 
		 * @return the property
		 */
		public String getProperty() {
			return property;
		}

		/**
		 * Checks if is ascending.
		 * 
		 * @return true, if is ascending
		 */
		public boolean isAscending() {
			return this.direction.equals(Direction.ASC);
		}

		/**
		 * With.
		 * 
		 * @param order
		 *            the order
		 * @return the order
		 */
		public Order with(Direction order) {
			return new Order(order, this.property);
		}

		/**
		 * With properties.
		 * 
		 * @param properties
		 *            the properties
		 * @return the sort
		 */
		public Sort withProperties(String... properties) {
			return new Sort(this.direction, properties);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {

			int result = 17;

			result = 31 * result + direction.hashCode();
			result = 31 * result + property.hashCode();

			return result;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {

			if (this == obj) {
				return true;
			}

			if (!(obj instanceof Order)) {
				return false;
			}

			Order that = (Order) obj;

			return this.direction.equals(that.direction) && this.property.equals(that.property);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			return String.format("%s: %s", property, direction);
		}
	}

	/**
	 * The Class Sort.
	 * 
	 * @author
	 */
	public static class Sort implements Serializable, Iterable<Order> {

		/** The Constant serialVersionUID. */
		private static final long serialVersionUID = -6732052557382112251L;

		/** The Constant DEFAULT_DIRECTION. */
		public static final Direction DEFAULT_DIRECTION = Direction.ASC;

		/** The orders. */
		private List<Order> orders = Lists.newArrayList();

		/**
		 * Instantiates a new sort.
		 */
		public Sort() {
			super();
		}

		/**
		 * Instantiates a new sort.
		 * 
		 * @param orders
		 *            the orders
		 */
		public Sort(Order... orders) {
			this(Arrays.asList(orders));
		}

		/**
		 * Instantiates a new sort.
		 * 
		 * @param orders
		 *            the orders
		 */
		public Sort(List<Order> orders) {

			if (null == orders || orders.isEmpty()) {
				throw new IllegalArgumentException("You have to provide at least one sort property to sort by!");
			}

			this.orders = orders;
		}

		/**
		 * Instantiates a new sort.
		 * 
		 * @param properties
		 *            the properties
		 */
		public Sort(String... properties) {

			this(DEFAULT_DIRECTION, properties);
		}

		/**
		 * Instantiates a new sort.
		 * 
		 * @param direction
		 *            the direction
		 * @param properties
		 *            the properties
		 */
		public Sort(Direction direction, String... properties) {

			this(direction, properties == null ? new ArrayList<String>() : Arrays.asList(properties));
		}

		/**
		 * Instantiates a new sort.
		 * 
		 * @param direction
		 *            the direction
		 * @param properties
		 *            the properties
		 */
		public Sort(Direction direction, List<String> properties) {

			if (properties == null || properties.isEmpty()) {
				throw new IllegalArgumentException("You have to provide at least one property to sort by!");
			}

			this.orders = new ArrayList<Order>(properties.size());

			for (String property : properties) {
				this.orders.add(new Order(direction, property));
			}
		}

		/**
		 * And.
		 * 
		 * @param sort
		 *            the sort
		 * @return the sort
		 */
		public Sort and(Sort sort) {

			if (sort == null) {
				return this;
			}

			ArrayList<Order> these = new ArrayList<Order>(this.orders);

			for (Order order : sort) {
				these.add(order);
			}

			return new Sort(these);
		}

		/**
		 * Gets the order for.
		 * 
		 * @param property
		 *            the property
		 * @return the order for
		 */
		public Order getOrderFor(String property) {

			for (Order order : this) {
				if (order.getProperty().equals(property)) {
					return order;
				}
			}

			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Iterable#iterator()
		 */
		public Iterator<Order> iterator() {

			return this.orders.iterator();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {

			if (this == obj) {
				return true;
			}

			if (!(obj instanceof Sort)) {
				return false;
			}

			Sort that = (Sort) obj;

			return this.orders.equals(that.orders);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {

			int result = 17;
			result = 31 * result + orders.hashCode();
			return result;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {

			return org.springframework.util.StringUtils.collectionToCommaDelimitedString(orders);
		}

		/**
		 * Gets the orders.
		 * 
		 * @return the orders
		 */
		public List<Order> getOrders() {
			return orders;
		}

		/**
		 * Sets the orders.
		 * 
		 * @param orders
		 *            the new orders
		 */
		public void setOrders(List<Order> orders) {
			this.orders = orders;
		}

	}

	/**
	 * Checks if is search debug.
	 * 
	 * @return true, if is search debug
	 */
	public boolean isSearchDebug() {
		return searchDebug;
	}

	/**
	 * Sets the search debug.
	 * 
	 * @see cn.com.summall.commons.Page#debugMsg
	 * @param searchDebug
	 *            the new search debug
	 */
	public void setSearchDebug(boolean searchDebug) {
		this.searchDebug = searchDebug;
	}

	/**
	 * get searchText
	 * 
	 * @return
	 * @author nibili 2016年1月8日
	 */
	public String getSearchText() {
		return searchText;
	}

	/**
	 * set searchText
	 * 
	 * @param searchText
	 * @author nibili 2016年1月8日
	 */
	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

}

© 著作权归作者所有

共有 人打赏支持
林伟琨
粉丝 9
博文 106
码字总数 38738
作品 0
厦门
后端工程师
Spring Boot [组件学习-Spring Data JPA]

导读: 在上篇文章中对Spring MVC常用的一些注解做了简要的说明,在这篇文章中主要对Spring Data JPA 做一个简要的说明,并附有一个简单的例子,可以体会到Spring Data JPA 的强大之处。 Sp...

yangrd
08/27
0
0
Spring管理Filter和Servlet

Spring管理filter和servlet 在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建。如果要在filter或者servlet中使用s...

zhwj407906450
2014/11/27
0
0
深入理解Spring MVC 思想

一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 五、spring mvc 父子上下文的说明 六、springMVC-mvc.xml 配置文件片段讲解 七、s...

qq58edf1d989a2d
06/26
0
0
SpringFramework4系列之SpringJMS:(一)搭建JMS-注解加XML版

Maven dependency maven的dependency用着 隐形的依赖传递性,如果只是用到 JMS这部分功能 引用一下Maven坐标即可 <dependency> <groupId>org.springframework</groupId> <artifactId>spring......

Garrry
2015/07/06
0
0
Springboot 中使用单元测试

参考自官网:Spring1.59的文档 一、测试 Testing Springboot 测试支持由两个模块提供; spring-boot-test 包含核心项目,而spring-boot-test-autoconfigure支持测试的自动配置。 大多数开发人...

不开心时不要学习
01/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

初级开发-编程题

` public static void main(String[] args) { System.out.println(changeStrToUpperCase("user_name_abc")); System.out.println(changeStrToLowerCase(changeStrToUpperCase("user_name_abc......

小池仔
今天
8
0
现场看路演了!

HiBlock
昨天
19
0
Rabbit MQ基本概念介绍

RabbitMQ介绍 • RabbitMQ是一个消息中间件,是一个很好用的消息队列框架。 • ConnectionFactory、Connection、Channel都是RabbitMQ对外提供的API中最基本的对象。Connection是RabbitMQ的s...

寰宇01
昨天
12
0
官方精简版Windows10:微软自己都看不过去了

微软宣布,该公司正在寻求解决方案,以减轻企业客户的Windows 10规模。该公司声称,企业客户下载整个Windows 10文件以更新设备既费钱又费时。 微软宣布,该公司正在寻求解决方案,以减轻企业...

linux-tao
昨天
20
0
TypeScript基础入门之JSX(二)

转发 TypeScript基础入门之JSX(二) 属性类型检查 键入检查属性的第一步是确定元素属性类型。 内在元素和基于价值的元素之间略有不同。 对于内部元素,它是JSX.IntrinsicElements上的属性类型...

durban
昨天
13
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部