how2j.cn

下载区
文件名 文件大小
springboot.rar 160k
步骤 1 : 先运行,看到效果,再学习   
步骤 2 : 模仿和排错   
步骤 3 : 基于前面的知识点   
步骤 4 : JPA 条件查询方式   
步骤 5 : 实现原理   
步骤 6 : 条件查询规范   

步骤 1 :

先运行,看到效果,再学习

edit
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
运行 TestJPA可以看到如图所示:进行了3种查询。
1. 查询所有的
2. 根据名称查询
3. 根据名称模糊查询,同时 id >5 ,同时 名称正排序

注: 本运行会重置 category 表所有数据
先运行,看到效果,再学习
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
步骤 3 :

基于前面的知识点

edit
本知识点是建立在上一个 spring 单元测试 知识点可运行项目的基础上进行的改进,所以最好把上个知识点理解和消化了.
步骤 4 :

JPA 条件查询方式

edit
JPA 条件查询方式很有意思,是不需要写 SQL 语句的,只需要在 dao 接口里按照规范的命名定义对应的方法名,及可达到查询相应字段的效果了。
在如下代码里做了如下事情:
1. 首先通过 @Before 把 Category表里所有数据都删除了,并新增了10条。
2. 然后 test1() 查询所有数据,看看新增的10条数据。
3. 接着,test2() 通过自定义的接口方法 findByName,根据name 查询分类表
4. 接着,test3() 通过自定义的接口方法 findByNameLikeAndIdGreaterThanOrderByNameAsc,根据名称模糊查询,id 大于某值, 并且名称正排序查询。
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; @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(); } }
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; } @Override public String toString() { return "Category [id=" + id + ", name=" + name + "]"; } }
虽然 JPA 没有自己手动写sql 语句,但是通过反射获取自定义的接口方法里提供的信息,就知道用户希望根据什么条件来查询了。 然后 JPA 底层再偷偷摸摸地拼装对应的 sql 语句,丢给数据库,就达到了条件查询的效果啦。

对反射不熟悉的同学,可了解反射基础教程: 反射基础教程
步骤 6 :

条件查询规范

edit
JPA 条件查询方式 只是个别举例,下表把 jpa 做的各种查询规范都列出来了。 如果要做其他相关查询,按照表格中的规范设计接口方法即可。
<style> table{ border-collapse:collapse } table.jpatable thead tr{ background-color:lightgray; } td,th{ border:1px solid black; } </style> <table class="jpatable"> <thead> <tr> <th>关键词</th> <th>举例</th> <th>生成的JPQL 语句片段</th> </tr> </thead> <tbody> <tr> <td>And</td> <td>findByLastnameAndFirstname</td> <td>… where x.lastname = ?1 and x.firstname = ?2</td> </tr> <tr> <td>Or</td> <td>findByLastnameOrFirstname</td> <td>… where x.lastname = ?1 or x.firstname = ?2</td> </tr> <tr> <td>Is,Equals</td> <td>findByFirstname,<br>findByFirstnameIs,<br>findByFirstnameEquals</td> <td>… where x.firstname = ?1</td> </tr> <tr> <td>Between</td> <td>findByStartDateBetween</td> <td>… where x.startDate between ?1 and ?2</td> </tr> <tr> <td>LessThan</td> <td>findByAgeLessThan</td> <td>… where x.age < ?1</td> </tr> <tr> <td>LessThanEqual</td> <td>findByAgeLessThanEqual</td> <td>… where x.age ⇐ ?1</td> </tr> <tr> <td>GreaterThan</td> <td>findByAgeGreaterThan</td> <td>… where x.age > ?1</td> </tr> <tr> <td>GreaterThanEqual</td> <td>findByAgeGreaterThanEqual</td> <td>… where x.age >= ?1</td> </tr> <tr> <td>After</td> <td>findByStartDateAfter</td> <td>… where x.startDate > ?1</td> </tr> <tr> <td>Before</td> <td>findByStartDateBefore</td> <td>… where x.startDate < ?1</td> </tr> <tr> <td>IsNull</td> <td>findByAgeIsNull</td> <td>… where x.age is null</td> </tr> <tr> <td>IsNotNull,NotNull</td> <td>findByAge(Is)NotNull</td> <td>… where x.age not null</td> </tr> <tr> <td>Like</td> <td>findByFirstnameLike</td> <td>… where x.firstname like ?1</td> </tr> <tr> <td>NotLike</td> <td>findByFirstnameNotLike</td> <td>… where x.firstname not like ?1</td> </tr> <tr> <td>StartingWith</td> <td>findByFirstnameStartingWith</td> <td>… where x.firstname like ?1 (parameter bound with appended %)</td> </tr> <tr> <td>EndingWith</td> <td>findByFirstnameEndingWith</td> <td>… where x.firstname like ?1 (parameter bound with prepended %)</td> </tr> <tr> <td>Containing</td> <td>findByFirstnameContaining</td> <td>… where x.firstname like ?1 (parameter bound wrapped in %)</td> </tr> <tr> <td>OrderBy</td> <td>findByAgeOrderByLastnameDesc</td> <td>… where x.age = ?1 order by x.lastname desc</td> </tr> <tr> <td>Not</td> <td>findByLastnameNot</td> <td>… where x.lastname <> ?1</td> </tr> <tr> <td>In</td> <td>findByAgeIn(Collection<Age> ages)</td> <td>… where x.age in ?1</td> </tr> <tr> <td>NotIn</td> <td>findByAgeNotIn(Collection<Age> age)</td> <td>… where x.age not in ?1</td> </tr> <tr> <td>True</td> <td>findByActiveTrue()</td> <td>… where x.active = true</td> </tr> <tr> <td>False</td> <td>findByActiveFalse()</td> <td>… where x.active = false</td> </tr> <tr> <td>IgnoreCase</td> <td>findByFirstnameIgnoreCase</td> <td>… where UPPER(x.firstame) = UPPER(?1)</td> </tr> </tbody> </table>


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2023-05-06 junit5要用@BeforeEach才能起作用
颠儿颠儿




在Junit4下使用@Before和@After,而在Junit5下使用@BeforeEach和@AfterEach
@BeforeEach
    public void before(){

        List<Category> cs = categoryDAO.findAll();
        for (Category c:cs)
            categoryDAO.delete(c);
        
        for (int i =0;i<10;i++){
            Category c = new Category();
            c.setName("category "+i);
            categoryDAO.save(c);
        }
    }

							





回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2020-06-12 请问这里的@Before和@Test有什么不同吗?
居居的拳击手套

请问这里的@Before和@Test有什么不同吗?




1 个答案

学生new
答案时间:2020-07-01
@BeforeClass – 表示在类中的任意public static void方法执行之前执行 @AfterClass – 表示在类中的任意public static void方法执行之后执行 @Before – 表示在任意使用@Test注解标注的public void方法执行之前执行 @After – 表示在任意使用@Test注解标注的public void方法执行之后执行 @Test – 使用该注解标注的public void方法会表示为一个测试方法



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2019-12-28 好像没有多表查询啊的归还啊,确定全部列出来了吗
2019-07-27 不是模糊查询啊,只是根据名称和id查出那个数据了
2019-07-12 基于mybatis.xml配置方法的根据id查询的控制器和页面怎么写?


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 3 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 框架-SpringBoot-JPA 条件查询 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图