文档章节

spring jpa的随机 排序使用

林伟琨
 林伟琨
发布于 2016/08/17 10:48
字数 1261
阅读 52
收藏 2
点赞 0
评论 0

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

这边的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
博文 91
码字总数 30082
作品 0
厦门
后端工程师
Spring mvc 创建完项目测试的时候前台404,后台没有错误信息这是咋了

前天遇到一个问题,springmvc创建的项目测试的时候前台404,后台没有错误输出,奇了怪了...... 这是WEB-INF下的xml文件

好吧我输了 ⋅ 06/19 ⋅ 0

spring data jpa 详解

本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求。这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring整合的环境中实现...

无敌小学僧 ⋅ 05/30 ⋅ 0

jpa----spring-data-jpa 复杂查询,包括多表关联,分页,排序等

转载:由于此转载文章的出处也是转载的,所以原作者不详 本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求。这里不讲解JPA和Spring-data...

u010775025 ⋅ 05/29 ⋅ 0

(一)SpringMVC之配置DispatcherServlet的一些坑

DispatcherServlet是SpringMVC的核心控制器,就像是SpringMVC的心脏,几乎所有的请求都会经过这个控制器,通过它,大大的降低了模块之间的耦合度。所有学SpringMVC的同学们第一步肯定都是先配...

Java攻城玩家 ⋅ 06/03 ⋅ 0

Spring Boot整合模板引擎jsp

jsp也算是一种模板引擎吧。整合jsp前,先说一下运行SpringBoot项目的几种方式 1. 运行SpringBoot项目的几种方式 1.1 使用内嵌Tomcat运行项目 在IDE中右键运行启动类,也就是直接直接运行App...

yysue ⋅ 06/15 ⋅ 0

Spring Context加载方式

Spring 加载方式 对于可执行文件方式,我们一般的加载Spring 配置的方式是 ClassPathXmlApplicationContext 从spring 3.0开始,开始使用注解的方式来进行spring 配置的注册 demoService是定义...

David_jim ⋅ 05/28 ⋅ 0

缅甸银河国际开户13170533331

解决这个问题涉及到两个方面的问题: 配置问题 、引入静态文件问题 1、配置问题 web.xml配置的DispatchServlet如下: [html] view plain copy springmvc org.springframework.web.servlet.D...

银河国际 ⋅ 05/26 ⋅ 0

深入 Spring Boot :实现对 Fat Jar jsp 的支持

原文出处:Hengyunabc spring boot 对于jsp支持的限制 对于jsp的支持,Spring Boot官方只支持了war的打包方式,不支持fat jar。参考官方文档: https://docs.spring.io/spring-boot/docs/cu...

Hengyunabc ⋅ 06/04 ⋅ 0

后端发送数据到前端出现溢出问题

用户编辑某个页面时,后台会把一个大的实体(包含所有模块数据)返回到前端。经常出现以下报错,但是重新请求下可能就没有了。是不是因为数据量很大的缘故呢?信息如下: 09:55:04.448 [qtp6...

琴麻岛 ⋅ 04/27 ⋅ 0

springboot学习(二)——springmvc配置使用

以下内容,如有问题,烦请指出,谢谢 上一篇讲解了springboot的helloworld部分,这一篇开始讲解如何使用springboot进行实际的应用开发,基本上寻着spring应用的路子来讲,从springmvc以及web...

嘻哈开发者 ⋅ 04/26 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

Gitee 生成并部署SSH key

1.如何生成ssh公钥 你可以按如下命令来生成 sshkey: ssh-keygen -t rsa -C "xxxxx@xxxxx.com" # Generating public/private rsa key pair...# 三次回车即可生成 ssh key 查看你的 ...

晨猫 ⋅ 37分钟前 ⋅ 0

zblog2.3版本的asp系统是否可以超越卢松松博客的流量[图]

最近访问zblog官网,发现zlbog-asp2.3版本已经进入测试阶段了,虽然正式版还没有发布,想必也不久了。那么作为aps纵横江湖十多年的今天,blog2.2版本应该已经成熟了,为什么还要发布这个2.3...

原创小博客 ⋅ 今天 ⋅ 0

聊聊spring cloud的HystrixCircuitBreakerConfiguration

序 本文主要研究一下spring cloud的HystrixCircuitBreakerConfiguration HystrixCircuitBreakerConfiguration spring-cloud-netflix-core-2.0.0.RELEASE-sources.jar!/org/springframework/......

go4it ⋅ 今天 ⋅ 0

二分查找

二分查找,也称折半查找、二分搜索,是一种在有序数组中查找某一特定元素的搜索算法。搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束;如果某一特定元素大于...

人觉非常君 ⋅ 今天 ⋅ 0

VS中使用X64汇编

需要注意的是,在X86项目中,可以使用__asm{}来嵌入汇编代码,但是在X64项目中,再也不能使用__asm{}来编写嵌入式汇编程序了,必须使用专门的.asm汇编文件来编写相应的汇编代码,然后在其它地...

simpower ⋅ 今天 ⋅ 0

ThreadPoolExecutor

ThreadPoolExecutor public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, ......

4rnold ⋅ 昨天 ⋅ 0

Java正无穷大、负无穷大以及NaN

问题来源:用Java代码写了一个计算公式,包含除法和对数和取反,在页面上出现了-infinity,不知道这是什么问题,网上找答案才明白意思是负的无穷大。 思考:为什么会出现这种情况呢?这是哪里...

young_chen ⋅ 昨天 ⋅ 0

前台对中文编码,后台解码

前台:encodeURI(sbzt) 后台:String param = URLDecoder.decode(sbzt,"UTF-8");

west_coast ⋅ 昨天 ⋅ 0

实验楼—MySQL基础课程-挑战3实验报告

按照文档要求创建数据库 sudo sercice mysql startwget http://labfile.oss.aliyuncs.com/courses/9/createdb2.sqlvim /home/shiyanlou/createdb2.sql#查看下数据库代码 代码创建了grade......

zhangjin7 ⋅ 昨天 ⋅ 0

一起读书《深入浅出nodejs》-node模块机制

node 模块机制 前言 说到node,就不免得提到JavaScript。JavaScript自诞生以来,经历了工具类库、组件库、前端框架、前端应用的变迁。通过无数开发人员的努力,JavaScript不断被类聚和抽象,...

小草先森 ⋅ 昨天 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部