From a05eef73188277e8d3db6f182312389d6c6bffcc Mon Sep 17 00:00:00 2001 From: rslee Date: Thu, 26 May 2016 15:21:41 +0800 Subject: [PATCH] =?UTF-8?q?[+]=20=E6=B7=BB=E5=8A=A0=E5=9F=BA=E4=BA=8Esort?= =?UTF-8?q?=E5=92=8Cpage=E7=9A=84=E5=88=86=E9=A1=B5=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jpa/repository/AccessLogRepository.java | 31 ++++++++++++- .../jpa/service/AccessLogSortService.java | 46 +++++++++++++++++++ .../jpa/service/AccessLogSortServiceImpl.java | 35 ++++++++++++++ .../src/main/resources/spring-cxf.xml | 1 + .../jpa/service/AccessLogSortServiceTest.java | 44 ++++++++++++++++++ 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/service/AccessLogSortService.java create mode 100644 spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/service/AccessLogSortServiceImpl.java create mode 100644 spring-jpa-demo/src/test/java/cn/rslee/demo/spring/jpa/service/AccessLogSortServiceTest.java diff --git a/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/repository/AccessLogRepository.java b/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/repository/AccessLogRepository.java index 9485602..1a3b8d3 100644 --- a/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/repository/AccessLogRepository.java +++ b/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/repository/AccessLogRepository.java @@ -1,6 +1,8 @@ package cn.rslee.demo.spring.jpa.repository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import cn.rslee.demo.spring.jpa.entity.AccessLog; @@ -14,9 +16,36 @@ import cn.rslee.demo.spring.jpa.entity.AccessLog; * 1 2016年2月19日 rslee Create * **************************************************************************** * - * @author 325175 + * + * JPA可以按照方法的名称自动生成sql语句,具体的命名方法参考下面链接: + * http://docs.spring.io/spring-data/jpa/docs/current/reference/html/ + * http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-jpa/ + * 摘要如下: + * + * 框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如 find、findBy、read、readBy、get、getBy,然后对剩下部分进行解析。 + * 并且如果方法的最后一个参数是 Sort 或者 Pageable 类型,也会提取相关的信息,以便按规则进行排序或者分页查询。 + * + * And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(String user, Striang pwd); + * Or --- 等价于 SQL 中的 or 关键字,比如 findByUsernameOrAddress(String user, String addr); + * Between --- 等价于 SQL 中的 between 关键字,比如 findBySalaryBetween(int max, int min); + * LessThan --- 等价于 SQL 中的 "<",比如 findBySalaryLessThan(int max); + * GreaterThan --- 等价于 SQL 中的">",比如 findBySalaryGreaterThan(int min); + * IsNull --- 等价于 SQL 中的 "is null",比如 findByUsernameIsNull(); + * IsNotNull --- 等价于 SQL 中的 "is not null",比如 findByUsernameIsNotNull(); + * NotNull --- 与 IsNotNull 等价; + * Like --- 等价于 SQL 中的 "like",比如 findByUsernameLike(String user); + * NotLike --- 等价于 SQL 中的 "not like",比如 findByUsernameNotLike(String user); + * OrderBy --- 等价于 SQL 中的 "order by",比如 findByUsernameOrderBySalaryAsc(String user); + * Not --- 等价于 SQL 中的 "! =",比如 findByUsernameNot(String user); + * In --- 等价于 SQL 中的 "in",比如 findByUsernameIn(Collection userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数; + * NotIn --- 等价于 SQL 中的 "not in",比如 findByUsernameNotIn(Collection userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数; + * + * @author rslee * @since 1.0 */ public interface AccessLogRepository extends JpaRepository { + + // 按照用户名过滤数据,按照时间倒序排列,分页返回数据 + Page findByUsernameOrderByVisitTmDesc(String username,Pageable pageable); } diff --git a/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/service/AccessLogSortService.java b/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/service/AccessLogSortService.java new file mode 100644 index 0000000..efbf022 --- /dev/null +++ b/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/service/AccessLogSortService.java @@ -0,0 +1,46 @@ +package cn.rslee.demo.spring.jpa.service; + +import javax.ws.rs.Consumes; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.springframework.data.domain.Page; + +import cn.rslee.demo.spring.jpa.entity.AccessLog; + +/** + * 描述: + * + *
+ * HISTORY
+ * ****************************************************************************
+ *  ID   DATE           PERSON          REASON
+ *  1    2016年2月26日      rslee         Create
+ * ****************************************************************************
+ * 
+ * + * 这个是基于jpa的实现基础的按照条件查询,过滤和排序功能 + * + * + * @author rslee + * @since 1.0 + */ +public interface AccessLogSortService { + + /** + * + * 分页读取指定用户名的数据,访问格式为:/accesslogsorts/{username}/page?page=2&size=100 + * @return + * List + */ + @GET + @Path("/accesslogsorts/{username}/page") + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + @Produces(MediaType.APPLICATION_JSON) + Page findUsernameByPage(@PathParam("username") String username,@FormParam("page") int page,@FormParam("size") int size); + +} diff --git a/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/service/AccessLogSortServiceImpl.java b/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/service/AccessLogSortServiceImpl.java new file mode 100644 index 0000000..293cf5c --- /dev/null +++ b/spring-jpa-demo/src/main/java/cn/rslee/demo/spring/jpa/service/AccessLogSortServiceImpl.java @@ -0,0 +1,35 @@ +package cn.rslee.demo.spring.jpa.service; + +import javax.annotation.Resource; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Component; + +import cn.rslee.demo.spring.jpa.entity.AccessLog; +import cn.rslee.demo.spring.jpa.repository.AccessLogRepository; + +/** + * 描述: + * + *
HISTORY
+ * ****************************************************************************
+ *  ID   DATE                    PERSON          REASON
+ *  1    2016年5月26日         rslee               Create
+ * ****************************************************************************
+ * 
+ * @author rslee + * @since 1.0 + */ +@Component("accessLogSortService") +public class AccessLogSortServiceImpl implements AccessLogSortService { + + @Resource + private AccessLogRepository accessLogRepository; + + @Override + public Page findUsernameByPage(String username, int page, int size) { + return accessLogRepository.findByUsernameOrderByVisitTmDesc(username, new PageRequest(page,size)); + } + +} diff --git a/spring-jpa-demo/src/main/resources/spring-cxf.xml b/spring-jpa-demo/src/main/resources/spring-cxf.xml index 57d76da..7f240f2 100644 --- a/spring-jpa-demo/src/main/resources/spring-cxf.xml +++ b/spring-jpa-demo/src/main/resources/spring-cxf.xml @@ -10,6 +10,7 @@ + diff --git a/spring-jpa-demo/src/test/java/cn/rslee/demo/spring/jpa/service/AccessLogSortServiceTest.java b/spring-jpa-demo/src/test/java/cn/rslee/demo/spring/jpa/service/AccessLogSortServiceTest.java new file mode 100644 index 0000000..293bc89 --- /dev/null +++ b/spring-jpa-demo/src/test/java/cn/rslee/demo/spring/jpa/service/AccessLogSortServiceTest.java @@ -0,0 +1,44 @@ +package cn.rslee.demo.spring.jpa.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import javax.annotation.Resource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.data.domain.Page; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import cn.rslee.demo.spring.jpa.entity.AccessLog; + +/** + * 描述: + * + *
HISTORY
+ * ****************************************************************************
+ *  ID   DATE                    PERSON          REASON
+ *  1    2016年5月26日         rslee               Create
+ * ****************************************************************************
+ * 
+ * @author rslee + * @since 1.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("/spring.xml") +public class AccessLogSortServiceTest { + + @Resource + AccessLogSortService accessSortLogService; + + @Test + public void testFindUsernameByPage() { + Page page = accessSortLogService.findUsernameByPage("guest",0, 10); + assertNotNull(page.getContent()); + assertEquals(page.getContent().size(), 10); + } + + + +} -- Gitee