In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to achieve jpa external join query join in Java. I hope you will get something after reading this article. Let's discuss it together.
1 、 IndexTagController.java
@ GetMapping ("/ tags/ {id}") public String types (@ PageableDefault (size = 3 PageableDefault sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, @ PathVariable long id, Model model, HttpSession session) {/ / find all tags and sort List tags = tagService.listTagTop (50) by tag news volume If (id = =-1) {/ / get the classification of maximum data id = tags.get (0). GetId ();} model.addAttribute ("tags", tags); model.addAttribute ("page", newsService.listNews (id,pageable)); model.addAttribute ("activeId", id); session.setAttribute ("query", "); return" tags " }
Id is the id of the tag in newService.listNews (id,pgeable). All you need to do in this method is to query all the news that contains id as the parameter id in the tag.
2. Business layer code
NewService.java is an interface in which the following methods exist
/ / find eligible news Page listNews (long id,Pageable pageable) according to the tag Id
NewServiceImpl.java is a class that implements the NewService interface and implements the listNews method
@ Override public Page listNews (long id, Pageable pageable) {return newsRepository.findAll (new Specification () {@ Override public Predicate toPredicate (Root root, CriteriaQuery cq, CriteriaBuilder cb) {/ / external join query Join Join join = root.join ("tags"); return cb.equal (join.get ("id"), id) }, pageable);}
NewsRepository.java inherits JpaSpecificationExecutor
Public interface NewsRepository extends JpaRepository, JpaSpecificationExecutor {@ Query ("select n from News n where n.recommend = true") List findTop (Pageable pageable); @ Query ("select n from News n where n.title like? 1 or n.content like? 1") Page findByQuery (String query,Pageable pageable); @ Query ("select function ('date_format',n.updateTime,'%Y') as year1 from News n group by year1 order by year1 desc") List findGroupYear () @ Query ("select n from News n where function ('date_format',n.updateTime,'%Y') =? 1") List findByYear (String year);} after reading this article, I believe you have a certain understanding of "how to implement jpa external join query join in Java". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.