文档章节

spring jpa的随机 排序使用

林伟琨
 林伟琨
发布于 2016/08/17 10:48
字数 1261
阅读 138
收藏 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;
	}

}

© 著作权归作者所有

共有 人打赏支持
林伟琨
粉丝 8
博文 102
码字总数 34558
作品 0
厦门
后端工程师
Spring管理Filter和Servlet

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

zhwj407906450
2014/11/27
0
0
Java面似宝典--Java Web(JSP、Servlet、各常用框架SSH、MyBatis等)

1、什么MVC模式?说说你对MVC的理解。 2、工作中经常用到的框架有哪些?它们各自的优缺点是什么? 3、说说Struts、Spring中的设计模式。 4、拦截器和过滤器两者的区别是什么? 5、简述一下,...

瓜子葫芦侠
2014/03/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
Spring 和 JavaEE的关系

Java EE vs Spring. Or: What is a standard? 什么是JavaEE J2EE是一系列技术标准所组成的平台。JavaEE包含了对一系列标准(接口)的实现。如果你要用这些接口,恐怕要使用JavaEE服务器而不仅...

Finley.Hamilton
2014/11/12
0
0
Springboot 中使用单元测试

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

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

没有更多内容

加载失败,请刷新页面

加载更多

下一页

【七】组合Action

本章描述了常用定义Action的方法。 自定义action builders 我们在action一章已经看过如何声明一个action——有request parameter、无request parameter、有body parser等等。你可以在 asynch...

Landas
38分钟前
0
0
Spring Boot实战之基础回顾

本文作者: 吴伟祥 本文链接: https://wuweixiang.cn/2018/08/21/Spring-Boot实战之基础回顾/ 版权声明: 本博客所有文章除特别声明外均为原创,采用CC BY-NC-SA 4.0 许可协议。转载请在文章开...

吴伟祥
39分钟前
0
0
OAuth认证开发

提示: 以下测试是基于项目安装成功,初始化数据库(initial_db.ddl, oauth.ddl, initial_data.ddl)后的测试, 也可在页面上点击"client_details"菜单里进行测试 方式1:基于浏览器 (grant_type=...

舒文joven
47分钟前
1
0
第二章-对象及变量的并发访问-第二篇

锁对象的改变 请阅读如下代码 public class MainClass { private String lock = "123"; public void printStringB() { try { synchronized (lock) { ......

简心
51分钟前
0
0
日志中记录代理IP以及真实客户端、apache只记录指定URI的日志

apache 日志中记录代理IP以及真实客户端 默认情况下log日志格式为: LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined 其中%h 是记录访问者的IP,如果在web的前...

李超小牛子
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部