Spring Boot jpa Service層代碼實(shí)例
更新時間:2019年10月07日 09:24:33 作者:行之間
這篇文章主要介紹了Spring Boot jpa Service層代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
這篇文章主要介紹了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)文章
關(guān)于JAVA_HOME路徑修改之后JDK的版本依然不更改的解決辦法
今天小編就為大家分享一篇關(guān)于JAVA_HOME路徑修改之后JDK的版本依然不更改的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04
SpringBoot之使用Feign實(shí)現(xiàn)微服務(wù)間的交互
這篇文章主要介紹了SpringBoot中使用Feign實(shí)現(xiàn)微服務(wù)間的交互,對微服務(wù)這方面感興趣的小伙伴可以參考閱讀本文2023-03-03
Spring Boot中整合PageHelper實(shí)現(xiàn)分頁功能詳細(xì)步驟
在Spring Boot項(xiàng)目中整合PageHelper并實(shí)現(xiàn)分頁查詢功能的全部步驟,通過以上配置和代碼,我們可以輕松地實(shí)現(xiàn)數(shù)據(jù)庫分頁查詢,提高了開發(fā)效率并改善了用戶體驗(yàn),感興趣的朋友跟隨小編一起看看吧2024-05-05

