Spring Boot jpa Service層代碼實(shí)例
這篇文章主要介紹了Spring Boot jpa Service層代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
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 2019年10月3日
*/
@Service
public class BlogServiceImpl implements BlogService {
@Autowired
private BlogRepository blogRepository;
/**
* 根據(jù)id查詢一條博客
*
* @param id
* @return
*/
@Override
public Blog getBlog(Long id) {
return blogRepository.findById(id).get();
}
/**
* 多條件動態(tài)查詢博客列表
*
* @param pageable
* @param blog
* @return
*/
@Override
public Page<Blog> listBlog(Pageable pageable, Blog blog) {
return blogRepository.findAll(new Specification<Blog>() {
@Override
public Predicate toPredicate(Root<Blog> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<>();
String title = blog.getTitle();
if (!"".equals(title) && title != null) {
predicates.add(cb.like(root.<String>get("title"), "%" + title + "%"));
}
Long id = blog.getType().getId();
if (id != null) {
predicates.add(cb.equal(root.<Type>get("type").get("id"), id));
}
boolean isRecommend = blog.isRecommend();
if (isRecommend) {
predicates.add(cb.equal(root.<Boolean>get("recommend"), isRecommend));
}
cq.where(predicates.toArray(new Predicate[predicates.size()]));
return null;
}
}, pageable);
}
/**
* 保存一條博客
*
* @param blog
* @return
*/
@Override
public Blog saveBlog(Blog blog) {
return blogRepository.save(blog);
}
/**
* 更新一條博客,先根據(jù)id查出結(jié)果回顯
*
* @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("你要更新的博客不存在!");
}
BeanUtils.copyProperties(b, blog);
return blogRepository.save(blog);
}
/**
* 根據(jù)id刪除一條博客
*
* @param id
*/
@Override
public void deleteBlog(Long id) {
blogRepository.deleteById(id);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)XML增加元素操作簡單示例
這篇文章主要介紹了java實(shí)現(xiàn)XML增加元素操作,結(jié)合簡單實(shí)例形式分析了java針對xml格式數(shù)據(jù)的讀取、遍歷、創(chuàng)建等操作技巧,需要的朋友可以參考下2017-02-02
SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控的步驟
這篇文章主要介紹了SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控,一步一步的實(shí)現(xiàn)?Springboot?整合?Redis?來存儲數(shù)據(jù),讀取數(shù)據(jù),需要的朋友可以參考下2022-01-01
Java數(shù)據(jù)結(jié)構(gòu)之選擇排序算法的實(shí)現(xiàn)與優(yōu)化
選擇排序:(Selection?sort)是一種簡單直觀的排序算法,也是一種不穩(wěn)定的排序方法。本文主要為大家介紹一下選擇排序的實(shí)現(xiàn)與優(yōu)化,希望對大家有所幫助2023-01-01
Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼
本篇文章主要介紹了Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
利用Spring Session和redis對Session進(jìn)行共享詳解
這篇文章主要給大家介紹了關(guān)于利用Spring、Session和redis對Session進(jìn)行共享的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
Kotlin + Spring Boot 請求參數(shù)驗(yàn)證的代碼實(shí)例
本篇文章主要介紹了Kotlin + Spring Boot 請求參數(shù)驗(yàn)證的代碼實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

