Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Spring Boot jpa Service layer

Shulou Source: shulou.com Published: 2022-06-03 19:17:54 09月23日 Update

This article mainly introduces the Spring Boot jpa Service layer example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Example:

Package com.fei.service.impl;import java.util.ArrayList;import java.util.List;import javax.persistence.criteria.CriteriaBuilder;import javax.persistence.criteria.CriteriaQuery;import javax.persistence.criteria.Predicate;import javax.persistence.criteria.Root;import org.springframework.beans.BeanUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.data.jpa.domain.Specification;import org.springframework.stereotype.Service;import com.fei.NotFoundException Import com.fei.po.Blog;import com.fei.po.Type;import com.fei.repository.BlogRepository;import com.fei.service.BlogService;/** * Created by zxf on October 3, 2019 * / @ Servicepublic class BlogServiceImpl implements BlogService {@ Autowired private BlogRepository blogRepository; / * query a blog according to id * * @ param id * @ return * / @ Override public Blog getBlog (Long id) {return blogRepository.findById (id). Get () } / * Multi-conditional dynamic query blog list * * @ param pageable * @ param blog * @ return * / @ Override public Page listBlog (Pageable pageable, Blog blog) {return blogRepository.findAll (new Specification () {@ Override public Predicate toPredicate (Root root, CriteriaQuery cq, CriteriaBuilder cb) {List predicates = new ArrayList (); String title = blog.getTitle () If (! ".equals (title) & & title! = null) {predicates.add (cb.like (root.get (" title "),"% "+ title +"% "));} Long id = blog.getType (). GetId (); if (id! = null) {predicates.add (cb.equal (root.get (" type "). Get (" id "), id)) } boolean isRecommend = blog.isRecommend (); if (isRecommend) {predicates.add (cb.equal (root.get ("recommend"), isRecommend));} cq.where (predicates.toArray (new Predicate [predicates.size ()]); return null;}}, pageable) } / * Save a blog * * @ param blog * @ return * / @ Override public Blog saveBlog (Blog blog) {return blogRepository.save (blog) Update a blog, first echo * * @ param id * @ param blog * @ return * / @ Override public Blog updateBlog (Long id, Blog blog) {Blog b = blogRepository.findById (id). Get (); if (b = = null) {throw new NotFoundException ("the blog you want to update does not exist!") ;} BeanUtils.copyProperties (b, blog); return blogRepository.save (blog);} / * Delete a blog according to id * * @ param id * / @ Override public void deleteBlog (Long id) {blogRepository.deleteById (id) }} Thank you for reading this article carefully. I hope the article "sample Analysis of the Spring Boot jpa Service layer" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Blog article example analysis update query value interest dynamic at the same time more friends conditions knowledge results editing industry information information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS MariaDB OPPO Reno Shulou Technology