Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Spring Boot jpa Service layer

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report