步骤1:先运行,看到效果,再学习步骤2:模仿和排错步骤3:基于前面的知识点步骤4:JPA 条件查询方式步骤5:实现原理步骤6:条件查询规范
步骤 1 : 先运行,看到效果,再学习
老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
运行 TestJPA可以看到如图所示:进行了3种查询。
1. 查询所有的
2. 根据名称查询
3. 根据名称模糊查询,同时 id >5 ,同时 名称正排序注: 本运行会重置 category 表所有数据

步骤 2 : 模仿和排错
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。
推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程步骤 3 : 基于前面的知识点
本知识点是建立在上一个 spring 单元测试 知识点可运行项目的基础上进行的改进,所以最好把上个知识点理解和消化了.步骤 4 : JPA 条件查询方式
JPA 条件查询方式很有意思,是不需要写 SQL 语句的,只需要在 dao 接口里按照规范的命名定义对应的方法名,及可达到查询相应字段的效果了。
在如下代码里做了如下事情:
1. 首先通过 @Before 把 Category表里所有数据都删除了,并新增了10条。
2. 然后 test1() 查询所有数据,看看新增的10条数据。
3. 接着,test2() 通过自定义的接口方法 findByName,根据name 查询分类表
4. 接着,test3() 通过自定义的接口方法 findByNameLikeAndIdGreaterThanOrderByNameAsc,根据名称模糊查询,id 大于某值, 并且名称正排序查询。
- TestJPA.java
- CategoryDAO.java
- Category.java
package com.how2java.springboot.test;import java.util.List;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import com.how2java.springboot.Application;import com.how2java.springboot.dao.CategoryDAO;import com.how2java.springboot.pojo.Category;@RunWith(SpringRunner.class)@SpringBootTest(classes = Application.class)public class TestJPA {@Autowired CategoryDAO dao;@Beforepublic 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);}}@Testpublic void test1() {List<Category> cs= dao.findAll();System.out.println("所有的分类信息:");for (Category c : cs) {System.out.println(c.getName());}System.out.println();}@Testpublic 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();}@Testpublic 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();}}package com.how2java.springboot.dao;import java.util.List;import org.springframework.data.jpa.repository.JpaRepository;import com.how2java.springboot.pojo.Category;public interface CategoryDAO extends JpaRepository<Category,Integer>{public List<Category> findByName(String name);public List<Category> findByNameLikeAndIdGreaterThanOrderByNameAsc(String name, int id);}package com.how2java.springboot.pojo;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = "category_")public class Category {@Id@GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private int id;@Column(name = "name")private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Category [id=" + id + ", name=" + name + "]";}}步骤 5 : 实现原理
虽然 JPA 没有自己手动写sql 语句,但是通过反射获取自定义的接口方法里提供的信息,就知道用户希望根据什么条件来查询了。 然后 JPA 底层再偷偷摸摸地拼装对应的 sql 语句,丢给数据库,就达到了条件查询的效果啦。
对反射不熟悉的同学,可了解反射基础教程: 反射基础教程步骤 6 : 条件查询规范
更多内容,点击了解: https://how2j.cn/k/springboot/springboot-jpa-query/1990.html
本文介绍JPA条件查询的实践应用,包括下载可运行项目、模仿排错、基于知识点改进等内容,通过具体示例讲解如何实现多种条件查询。
- SPRING JPA 如何做条件查询&spm=1001.2101.3001.5002&articleId=110509250&d=1&t=3&u=cf330022e80c439e91037e640618980f)
1万+

被折叠的 条评论
为什么被折叠?



