1、添加根据作者进行查询
jpa提供了一种findBy的查询语法规则,例如我们可以直接使用findByAuthor就可以达到根据作者查询的目的,这里不讲这种方法,我们用通用的方法来查询,我们使用下面的方法来查询(该方法又称为QBE查询,query by example)。
![](https://img.haomeiwen.com/i20791991/1422bda2073324b3.png)
该方法接收一个Example参数,和Pageable参数。
我们修改PostController,新增一个方法
![](https://img.haomeiwen.com/i20791991/eef4057bcd01bdc3.png)
注意这个方法虽然Mapping地址一样,但是请求的Method是post,所以不算重复定义,这里author我们设为非必填,author在这里就属于form data传值的方式。
代码里面我们构造了一个Post例子,设置author为传进来的author,以这个例子作为查询条件进行查询。
我们使用postman来进行测试,发现好像达到了我们预期的目的,大家可以试着传入别的参数。
![](https://img.haomeiwen.com/i20791991/856f7836d3209fd2.png)
为了确认我们结果的正确性,我们看下后台执行的sql语句,默认sql语句是不显示的,我们需要进行下面配置
![](https://img.haomeiwen.com/i20791991/561dc7c0cc66db73.png)
再次测试,发现控制台打印如下:
Hibernate: select post0_.id as id1_0_, post0_.author as author2_0_, post0_.content as content3_0_, post0_.publish_date as publish_4_0_, post0_.summary as summary5_0_, post0_.title as title6_0_ from cms_post post0_ where post0_.author=? order by post0_.publish_date desc limit ?
Hibernate: select count(post0_.id) as col_0_0_ from cms_post post0_ where post0_.author=?
这样可以确定我们的接口没有问题了。
2、模糊查询
上面的接口实际上是进行的精确匹配,假如我们需要模糊匹配,即like查询,怎么办呢。
![](https://img.haomeiwen.com/i20791991/668947761bdecdaa.png)
我们加入ExampleMatcher,上面的代码表示,对于author字段,我们指定匹配规则,即字符串包含,忽略大小写
![](https://img.haomeiwen.com/i20791991/966085050aa6700b.png)
我们看下StringMatcher里面定义了几种方式
![](https://img.haomeiwen.com/i20791991/7c013d6adf5f7a39.png)
可以看到,默认使用精确匹配,常用的还有包含,以xx开头,以xx结尾,最后还有大招,直接上正则表达式。
我们看下输出的sql:
Hibernate: select post0_.id as id1_0_, post0_.author as author2_0_, post0_.content as content3_0_, post0_.publish_date as publish_4_0_, post0_.summary as summary5_0_, post0_.title as title6_0_ from cms_post post0_ where post0_.author like ? escape ? order by post0_.publish_date desc limit ?
Hibernate: select count(post0_.id) as col_0_0_ from cms_post post0_ where post0_.author like ? escape ?
确实使用的like查询。
3、再添加一个查询条件
实际项目中,经常会碰到多条件查询,我们再增加一个根据标题模糊查询的条件,我们再修改一下代码
![](https://img.haomeiwen.com/i20791991/39346fae60b09b16.png)
![](https://img.haomeiwen.com/i20791991/48b9d0f82d775015.png)
Hibernate: select post0_.id as id1_0_, post0_.author as author2_0_, post0_.content as content3_0_, post0_.publish_date as publish_4_0_, post0_.summary as summary5_0_, post0_.title as title6_0_ from cms_post post0_ where (post0_.author like ? escape ?) and (post0_.title like ? escape ?) order by post0_.publish_date desc limit ?
Hibernate: select count(post0_.id) as col_0_0_ from cms_post post0_ where (post0_.author like ? escape ?) and (post0_.title like ? escape ?)
当post所有属性都为null时,其实就是查询所有数据。我们的Post所有属性初始化值都是null,如果有些属性有默认值,而你又不想把这个属性作为查询条件的时候,请手动设为null。
4、页面表单
我们给列表页面添加一个过滤查询表单。
![](https://img.haomeiwen.com/i20791991/68f014607294f819.png)
![](https://img.haomeiwen.com/i20791991/a84543ac9480483f.png)
![](https://img.haomeiwen.com/i20791991/4e8969d1564f3dd8.png)
这里我们加入了一个qs插件,用来给post方法参数进行编码,在之前post的方法中我们了解到,直接post数据是使用request payload的方式,后台是用RequestBody接收,而这里我们需要使用form data的方式提交数据,qs插件就可以很方便让我们做到。qs插件可以从这下载https://github.com/ljharb/qs,复制dist目录下的qs.js,当然最后我提供的源码也包含这个文件。
![](https://img.haomeiwen.com/i20791991/d7977cad05da9875.png)
![](https://img.haomeiwen.com/i20791991/d95a47a872c79b64.png)
![](https://img.haomeiwen.com/i20791991/f5467b8d82f71669.png)
5、总结
这章讲了如何使用QBE的方式进行条件过滤查询,这种方式应用场景有限,首先,他只支持and条件组合,你像,如果需求是要查title=A或者author=B,这个就做不到了。其次,值的匹配也有限,对于String,绝大多数能满足要求,只有空字符串比较特殊,这里空字符串直接和null是等价的。对于其他类型的数据,基本上只能使用精确匹配了。后面我们将介绍更加复杂的查询方式。
另外,这里我们已经学习到了三种传参方式,后台@PathVariable,对应前端url地址组合传参,@RequestBody对应request payload,@RequestParam对应form data或者query string,query string就是url里面拼查询参数,类似localhost:port/query?title=&author=
代码:
https://github.com/www15119258/springboot-study/tree/branch14
网友评论