ibatis发展到MyBatis,已经越来越像Hibernate,就像有人调侃,nosql技术系发展到最后就是关系型数据库。
嘿嘿。
关于Spring data jpa的简单分页查询可以参考SpringDataJpa的官方文档。
1:实体类和表的关系
(1)表数据结构
create table T_USER ( ID VARCHAR2(50), USERID VARCHAR2(50), USERNAME VARCHAR2(50), PASSWORD VARCHAR2(50) )
(2)实体类
import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import lombok.Data; @SuppressWarnings("serial") @Entity @Table(name = "T_USER") @Data public class SimpleUserEntity implements Serializable { @Id @Column( name = "ID", length = 50,unique = true,nullable = false) private String id; @Column( name = "USERID", length = 50,unique = true,nullable = false) private String userId; @Column( name = "USERNAME", length = 50,nullable = false) private String userName; @Column( name = "PASSWORD", length = 50,nullable = false) private String passWord; }
1:new Object(feild1,feild2)的用法
(1)该方式需保证对应实体类的方法,有对应的构造方法。
public SimpleUserEntity(String userId,String userName){ this.userId = userId; this.userName = userName; }
(2)新建Repository接口
import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.PagingAndSortingRepository; public interface SimpleQueryRepository extends PagingAndSortingRepository<SimpleUserEntity, String> { @Query( value = " select new SimpleUserEntity(s.userId,s.userName) from SimpleUserEntity s ", countQuery = " select count(s) from SimpleUserEntity s" ) Page<SimpleUserEntity> userPageData(Pageable page); }
(3)测试用例
import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Order; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import com.slfinance.shanlinbao.repository.SimpleQueryRepository; import com.slfinance.shanlinbao.repository.SimpleUserEntity; @ContextConfiguration(locations = { "classpath:/application-jpa.xml"}) @ActiveProfiles("dev") @Slf4j public class SimpleQueryRepositoryTest extends AbstractJUnit4SpringContextTests { @Autowired SimpleQueryRepository simpleQueryRepository; final int pageNum = 0, pageSize = 10; @Test public void testFindPage(){ Order order = new Order(Direction.DESC,"id"); //字段排序O PageRequest pageRequest = new PageRequest(pageNum, pageSize, new Sort(order)); Page<SimpleUserEntity> userPage = simpleQueryRepository.userPageData(pageRequest); } }
(3)返回数据
{ "id": null, "userId": "A004", "userName": "jack4", "passWord": null }
2:new Map(feild1 as otherAlias1,feild2 ad otherAlias2)
有时有些业务需要将查询出的字段更改别名,通过new Map() 方式也是思路之一。
(1)接口SimpleQueryRepository新增如下方法
@Query( value = " select new Map(s.userId as userNo,s.userName as name) from SimpleUserEntity s ", countQuery = " select count(s) from SimpleUserEntity s" ) Page<Map<String,Object>> userPageDataMap(Pageable page);
(2)SimpleQueryRepositoryTest新增方法
@Test public void testFindPageForMap(){ Order order = new Order(Direction.DESC,"id"); //字段排序O PageRequest pageRequest = new PageRequest(pageNum, pageSize, new Sort(order)); Page<Map<String,Object>> userPage = simpleQueryRepository.userPageDataMap(pageRequest); }
(3)返回数据
{ "userNo": "A004", "name": "jack4" }
相关推荐
使用SpringBoot和Spring-data-jpa进行分页查询可以大大简化编码流程并提高开发效率。 在使用Spring-data-jpa进行分页查询时,需要遵循以下步骤: 引入依赖 在pom.xml文件中加入spring-boot-starter-data-jpa依赖,...
spring data jpa 的Specifications动态查询 单条件查询 多条件查询 分页查询
本篇文章主要介绍了spring data jpa分页查询示例代码,分页在很多项目中都能使用,具有一定的参考价值,有兴趣的可以了解一下。
springboot结合jpa实现分页,动态多条件查询,使用thymeleaf前端渲染数据
Spring Data JPA入门项目02(查询操作的基本实现(排序、分页))
Spring+Spring MVC+SpringData JPA整合完成增删改查,翻页实例.zip
spring data jpa curd 分页 hsql
使用Spring Boot 2.1.6轻松实现JPA操作MySQL数据库的CRUD,以及分页和排序功能。在Spring Boot 2.x版本中可以非常轻松、快速搞定持久层的开发动作,配置比SpringBoot+MyBatis还少,偶觉得它除了执行效果不如...
本篇文章主要介绍了Spring Data JPA实现分页Pageable的实例代码,具有一定的参考价值,有兴趣的可以了解一下
Spring Data JPA 是Spring基于ORM框架、JPA规范封装的...学习并使用SpringDataJPA可以极大提高开发效率! 除了CRUD外,还包括如分页、排序等一些常用的功能。下面的示例代码即可完成数据保存的操作,而无需具体实现类.
Spring Data JPA+kkpager实现的一个简单分页DEMO
如何使用jpa进行多条件查询以及查询列表分页呢?下面我将介绍两种多条件查询方式。具体实例代码大家参考下本文吧
主要介绍了Spring Data JPA带条件分页查询实现原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
用了spring data Jpa做的增删改查登录分页查询的简单实例
Spring,Spring Data Jpa,freemarker,bootstrap实现的分页源代码
主要介绍了Spring Data JPA分页复合查询原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本篇文章主要介绍了Spring Data Jpa实现分页和排序代码实例,具有一定的参考价值,有兴趣的可以了解一下。
spingboot后端完整代码,利用spring data jpa 实现数据基本的增删改查,以及分页动态条件查询
本篇文章主要介绍了SpringData JPA实现查询分页demo,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
该系统实现一对多、分页查询以及修改,将病区与病人各为一个微服务进行处理,利用Feign在Consul注册中心进行负载均衡,Swagger进行前后端分离,进行调试。