基于JPA的分页查询PageRequest的工具类PageUtil

0 条评论

学习Spring Data JPA有一段时间了,今天开始整理基于JPA的一些东西,比如这篇文章要分享的PageUtil。
解决的问题是,通过静态实现快速构造Pageable的实例,在分页查询的时候,写代码也更简单。


1、代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

public class PageUtil {
public static PageRequest getPageRequest(Integer pageIndex, Integer pageSize, Sort sort){
//默认页面为0,
if(pageIndex==null || pageIndex<1){
pageIndex = 0;
}else{
pageIndex = pageIndex-1;
}
//默认页面大小20
if(pageSize==null || pageSize<1){
pageSize = 20;
}
//默认采用ID倒叙排列
if(sort==null){
sort = new Sort(Sort.Direction.DESC,"id");
}
return new PageRequest(pageIndex,pageSize,sort);
}

public static PageRequest getPageRequest(){
return getPageRequest(null,null,null);
}

public static PageRequest getPageRequest(Integer pageIndex,Integer pageSize){
return getPageRequest(pageIndex,pageSize,null);
}
}

阅读全文

Spring Data JPA系列:数据查询(Specification)(二)

0 条评论

写了一系列入门文章之后,博客也有了一些访问量,按照计划,对数据查询进行深入一些的探究,包括

  • inner join查询
  • 连接对象的属性值查询
  • in条件查询

阅读全文

Redis: OOM command not allowed when used memory > ‘maxmemory’

0 条评论

1、事故:

检查服务器,发现redis无法正常写入,大多数时候写入失败,打开redis的日志信息发现:
Redis: OOM command not allowed when used memory > ‘maxmemory’
意思是Redis内存不足,可以在命令行中,通过 info memory查看当前redis 的内存设置信息,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Memory
used_memory:2365344
used_memory_human:2.26M
used_memory_rss:9953280
used_memory_rss_human:9.49M
used_memory_peak:2483552
used_memory_peak_human:2.37M
total_system_memory:16725504000
total_system_memory_human:15.58G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:4194304
maxmemory_human:4.00M
maxmemory_policy:noeviction
mem_fragmentation_ratio:4.21
mem_allocator:jemalloc-4.0.3

阅读全文

Spring Data JPA系列:数据查询(Specification)(一)

0 条评论

在JPA中,如果用@Query查询有诸多不便,因此JPA提供了基于Criteria API的查询,比@Query查询更灵活方便,这篇文章就简单说说Specification的简单使用。
这篇文章是参考:Useing JPA Criteria API ,相关代码如下所示:

阅读全文

Java看源码如何方便看注释文档?

0 条评论

现在的JAVA项目大多用maven管理依赖,在maven的导入选项旁边有下载源码和文档,执行这个操作可以在看相关项目依赖的时候直接看对应的源码及文档注释很方便。
很多人对于问题都是求之于搜索引擎给出的答案,这样不是说不行,只是很浪费时间。
开始的时候我们没有入门可以借助文档和搜索引擎来达到快速入门的目的,在入门之后,很多问题应该求之于源码上,这个阶段,看源码是必经之路,如果你没有经过,那总有一天死都不知道怎么死的,就是这样。
继续说看源码,在IDEA内,按住Ctrl然后点击引用的类名即可进入对应的源码,然后在源码内有方法的定义和文档的说明,比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* The <code>CriteriaQuery</code> interface defines functionality that is specific
* to top-level queries.
*
* @param <T> the type of the defined result
* @since Java Persistence 2.0
*/
public interface CriteriaQuery<T> extends AbstractQuery<T> {

/**
* Specify the item that is to be returned in the query result.
* Replaces the previously specified selection(s), if any.
*
* <p> Note: Applications using the string-based API may need to
* specify the type of the select item when it results from
* a get or join operation and the query result type is
* specified.
*
* <pre>
* For example:
*
* CriteriaQuery<String> q = cb.createQuery(String.class);
* Root<Order> order = q.from(Order.class);
* q.select(order.get("shippingAddress").<String>get("state"));
*
* CriteriaQuery<Product> q2 = cb.createQuery(Product.class);
* q2.select(q2.from(Order.class)
* .join("items")
* .<Item,Product>join("product"));
*
* </pre>
*
* @param selection selection specifying the item that
* is to be returned in the query result
*
* @return the modified query
*
* @throws IllegalArgumentException if the selection is
* a compound selection and more than one selection
* item has the same assigned alias
*/
CriteriaQuery<T> select(Selection<? extends T> selection);

阅读全文

Spring Data JPA系列:数据更新(Update)

0 条评论

上次通过《Spring Data JPA系列:使用@Modifying修改(Modifying queries)》介绍了数据更新的方式,这种更新方式会很不方便,写的时候也比较麻烦,可以为更新密码、更新用户名等一些特殊的更新单独定义,但是对大多数数据操作是不方便的,比如我要更新一条有一百个字段的数据,这时候如果要通过Modifying方式就非常的不方便,因此,我们需要一种新的方式来解救。
通过阅读Spring-Data-JPA相关的文档和博客,找到了对应的解决方案,就是使用save()方法,经过测试,可用。
我们平时对save()方法的理解,大多是等同于insert(),主要是指新增一条数据,而JPA的save()方法包含了merge()的概念,就是说,如果save的对象不存在primary key或者primary key值在database内不存在的时候会新添加一条数据,如果primary key 存在并且primary key已经在database中存在,那就会依据primary key对该条数据进行更新,这是我们乐意见到的。

阅读全文

Spring Boot:@RestController中.(点号)导致截断的问题

0 条评论

在测试/api/v1/user/info/email/{email}这个方法的时候@PathVariable无法正常接收参数,邮件地址中(.)后的值会被截断,在社区http://www.spring4all.com 中通过小马哥和@杨小强的帮助下解决了这个问题,记录一下。

阅读全文

OKHTTP3简单工具类分享

0 条评论

1、官网:

https://square.github.io/okhttp/

阅读全文

Spring Data JPA系列:投影(Projection)的用法

0 条评论

在JPA的查询中,有一个不方便的地方,@Query注解,如果查询直接是Select C from Customer c,这时候,查询的返回对象就是Customer这个完整的对象,包含所有字段,对于我们的示例并没有什么问题,但是对于比较庞大的domain类,这个查询时就比较要命,并不是所有的字段都能用到,比较头疼。另外,如果定义select c.firstName as firstName,c.lastName as lastName from Customer c这个查询结果,返回的对象是Object类型,而且无法直接转换成Customer对象,这样用起来就不是很方便。
对于这种情况,JPA提供了一种声明方式来解决,即声明一个接口类,然后直接使用这个接口类接受返回的数据即可。下面奉上代码:

阅读全文

Spring Data JPA系列:分页(Pageable)

0 条评论

在JPA中提供了很方便的分页功能,那就是Pageable(org.springframework.data.domain.Pageable)以及它的实现类PageRequest(org.springframework.data.domain.PageRequest),详细的可以见示例代码:

阅读全文