SpringBoot JPA + 分页 + 单元测试SpringBoot JPA条件查询
application.properties
新增数据库链接必须的参数
spring.jpa.properties.hibernate.hbm2ddl.auto=update
表示会自动更新表结构,所以创建表 这一步其实是可以不需要的~
增加对mysql和jpa的支持
<!-- mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Pojo 下的Category类
增加一个包:com.coaing4fun.springboot.pojo,然后创建实体类Category。
@Entity 注解表示这是个实体类
@Table(name = "category_") 表示这个类对应的表名是 category_ ,注意有下划线哦
@Id 表明主键
@GeneratedValue(strategy =
GenerationType.IDENTITY) 表明自增长方式
@Column(name = "id") 表明对应的数据库字段名
CategoryDAO
增加一个包:com.coding4fun.springboot.dao,然后创建dao接口CategoryDAO,继承了JpaRepository,并且提供泛型<Category,Integer> 表示这个是针对Category类的DAO,Integer表示主键是Integer类型。
JpaRepository 这个父接口,就提供了CRUD, 分页等等一系列的查询了,直接拿来用,都不需要二次开发的了。
public interface CategoryDAO extends JpaRepository<Category,Integer>{
}
CategoryController
增加一个包:com.coding4fun.springboot.web,然后创建CategoryController 类。
1. 接受listCategory映射
2. 然后获取所有的分类数据
3. 接着放入Model中
4. 跳转到listCategory.jsp中
用jstl遍历从CategoryController 传递过来的集合:cs.
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${cs}" var="c" varStatus="st">
<tr>
<td>${c.id}</td>
<td>${c.name}</td>
</tr>
</c:forEach>
</table>
demo
项目源码:
链接:https://pan.baidu.com/s/1KgQCNyelwxcqDWfVTjyrTA
提取码:ha1u
数据库:
链接:https://pan.baidu.com/s/1jPu7yY5d4A6CUdRT2GBP_g
提取码:dw89
SpringBoot CRUD+分页 使用JPA
实现完整的增删改查 CRUD和分页
CRUD和分页
在JPA 基本用法教程中 学习了JPA的基本运用,可是最后呢,总归还是要搞 CRUD和分页的。 并且借助CRUD和分页对JPA 的常用手法做一个学习。
为CategoryController添加: 增加、删除、获取、修改映射
值得注意:JPA 新增和修改用的都是save. 它根据实体类的id是否为0来判断是进行增加还是修改
修改查询映射
@RequestMapping("/listCategory")
public String listCategory(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
start = start<0?0:start;
Sort sort = new Sort(Sort.Direction.DESC, "id");
Pageable pageable = new PageRequest(start, size, sort);
Page<Category> page =categoryDAO.findAll(pageable);
m.addAttribute("page", page);
return "listCategory";
}
1. 在参数里接受当前是第几页 start ,以及每页显示多少条数据 size。 默认值分别是0和5。 (Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) 2. 如果 start 为负,那么修改为0. 这个事情会发生在当前是首页,并点击了上一页的时候 start = start<0?0:start; 3. 设置倒排序 Sort sort = new Sort(Sort.Direction.DESC, "id"); 4. 根据start,size和sort创建分页对象 Pageable pageable = new PageRequest(start, size, sort); 5. CategoryDAO根据这个分页对象获取结果page. Page<Category> page =categoryDAO.findAll(pageable); 在这个page对象里,不仅包含了分页信息,还包含了数据信息,即有哪些分类数据。 这个可以通过getContent()获取出来。
6. 把page放在"page"属性里,跳转到listCategory.jsp m.addAttribute("page", page);
return "listCategory";
通过page.getContent遍历当前页面的Category对象。
在分页的时候通过page.number获取当前页面,page.totalPages获取总页面数。
注:page.getContent会返回一个泛型是Category的集合。
listCategory.jsp
<a href="?start=0">[首 页]</a>
<a href="?start=${page.number-1}">[上一页]</a>
<a href="?start=${page.number+1}">[下一页]</a>
<a href="?start=${page.totalPages-1}">[末 页]</a> <form action="addCategory" method="post"> name: <input name="name"> <br>
<button type="submit">提交</button> </form>
修改界面
<form action="updateCategory" method="post"> name: <input name="name" value="${c.name}"> <br> <input name="id" type="hidden" value="${c.id}">
<button type="submit">提交</button> </form>
demo
项目代码:
链接:https://pan.baidu.com/s/1NWZIdvdPBdgC2PW9alM3Jg
提取码:ple3
数据库:
链接:https://pan.baidu.com/s/1jPu7yY5d4A6CUdRT2GBP_g
提取码:dw89
单元测试SpringBoot JP条件查询
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TestJPA { @Autowired
CategoryDAO dao; @Before
public void before() {
List<Category> cs= dao.findAll();
for (Category c : cs) {
dao.delete(c);
} for (int i = 0; i < 10; i++) {
Category c = new Category();
c.setName("category " + i);
dao.save(c);
} } @Test
public void test1() {
List<Category> cs= dao.findAll();
System.out.println("所有的分类信息:");
for (Category c : cs) {
System.out.println(c.getName());
}
System.out.println();
} @Test
public void test2() {
System.out.println("查询名称是 \"category 1 \"的分类:");
List<Category> cs= dao.findByName("category 1");
for (Category c : cs) {
System.out.println("c.getName():"+ c.getName());
}
System.out.println();
}
@Test
public void test3() {
System.out.println("根据名称模糊查询,id 大于5, 并且名称正排序查询");
List<Category> cs= dao.findByNameLikeAndIdGreaterThanOrderByNameAsc("%3%",5);
for (Category c : cs) {
System.out.println(c);
}
System.out.println(); }
demo
项目代码:
链接:https://pan.baidu.com/s/1Q6YqsL4xUV3TqeiVtWgEOQ
提取码:47sa
数据库:
链接:https://pan.baidu.com/s/1jPu7yY5d4A6CUdRT2GBP_g
提取码:dw89
SpringBoot JPA + 分页 + 单元测试SpringBoot JPA条件查询的更多相关文章
- Spring Data JPA,一种动态条件查询的写法
我们在使用SpringData JPA框架时,进行条件查询,如果是固定条件的查询,我们可以使用符合框架规则的自定义方法以及@Query注解实现. 如果是查询条件是动态的,框架也提供了查询接口. Jpa ...
- jpa中使用Query判断条件查询
jpa中使用Query判断条件查询 @Query(value = " select m.* from mining_area as m " + " where 1 = 1 ...
- 【Spring Data 系列学习】Spring Data JPA 自定义查询,分页,排序,条件查询
Spring Boot Jpa 默认提供 CURD 的方法等方法,在日常中往往时无法满足我们业务的要求,本章节通过自定义简单查询案例进行讲解. 快速上手 项目中的pom.xml.application ...
- Hibernate结合JPA编写通用泛型多条件查询
项目中使用Hibernate和JPA对数据库对象进行实例化,但是生成的方法不支持多条件查询.而如果针对每一个数据库对象进行多条件查询编码,则会变得很麻烦,而且一旦以后发生表结构发生变化,这些方法可能还 ...
- 利用PHP访问数据库——实现分页功能与多条件查询功能
1.实现分页功能 <body><table width="100%" border="1"> <thead> < ...
- 【spring data jpa】带有条件的查询后分页和不带条件查询后分页实现
一.不带有动态条件的查询 分页的实现 实例代码: controller:返回的是Page<>对象 @Controller @RequestMapping(value = "/eg ...
- SpringBoot Jpa 分页查询最新配置方式
这是已经被废弃的接口 Sort sort = new Sort(Sort.Direction.DESC,"bean类中字段"); //创建时间降序排序 Pageable pagea ...
- SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法
软件152 尹以操 首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml ...
- spring data jpa实现多条件查询(分页和不分页)
目前的spring data jpa已经帮我们干了CRUD的大部分活了,但如果有些活它干不了(CrudRepository接口中没定义),那么只能由我们自己干了.这里要说的就是在它的框架里,如何实现自 ...
随机推荐
- MySQL入门(6)——流程控制
MySQL入门(6)--流程控制 IF语句 条件判断语句,逻辑与大多数编程语言相同,表示形式如下: IF condition THEN ... [ELSE condition THEN] ... [E ...
- Nodejs学习笔记(3) 创建服务器:Web 模块(http)与 express 框架
目录 参考资料 1. 使用 http 模块创建服务器 1.1 实现思路及代码 1.2 HTTP 结构 1.2.1 Request中的重要字段 1.2.2 Response 头信息:文件类型.状态码.连 ...
- C#控制鼠标自动连续点(DEMO)
---------------------------界面---------------------------------------------------- ------------------ ...
- ResNet的个人总结
ResNet可以说是我认真读过的第一篇paper,据师兄说读起来比较简单,没有复杂的数学公式,不过作为经典的网络结构还是有很多细节值得深究的.因为平时不太读英文文献,所以其实读的时候也有很多地方不是很 ...
- 3、MyBatis教程之CURD操作
4.CURD操作 1.查询 根据用户 Id查询用户 在UserMapper中添加对应方法 public interface UserMapper { List<User> getUserL ...
- Object类中的常用方法
1.getClass方法 源码: 功能: 返回此Object的运行时类. 什么是运行时类? 如上图所示,类从被加载到虚拟机内存开始,到卸载出内存为止,他的生命周期一共包含7个阶段.其中加载阶段虚拟机需 ...
- 【安全研究】Domain fronting域名前置网络攻击技术
出品|MS08067实验室(www.ms08067.com) 千里百科 Domain Fronting基于HTTPS通用规避技术,也被称为域前端网络攻击技术.这是一种用来隐藏Metasploit,Co ...
- [递推]C. 【例题3】数的划分
C . [ 例 题 3 ] 数 的 划 分 C. [例题3]数的划分 C.[例题3]数的划分 题目描述 将整数 n n n 分成 k k k 份,且每份不能为空,任意两个方案不相同(不考虑顺序). 例 ...
- Dapper, Ef core, Freesql 插入大量数据性能比较(一)
需求:导入9999行数据时Dapper, Ef core, Freesql 谁的性能更优,是如何执行的,级联增加谁性能更佳. 确认方法:sql server 的 sys.dm_exec_query_s ...
- windows上phpstudy配置memcache
原文 http://blog.csdn.net/ltx06/article/details/78588448 总的来说,分两步:同时安装memcached软件服务和安装php_memcache ...