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; /**
* 根据id查询一条博客
*
* @param id
* @return
*/
@Override
public Blog getBlog(Long id) {
return blogRepository.findById(id).get();
} /**
* 多条件动态查询博客列表
*
* @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);
} /**
* 更新一条博客,先根据id查出结果回显
*
* @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);
} /**
* 根据id删除一条博客
*
* @param id
*/
@Override
public void deleteBlog(Long id) {
blogRepository.deleteById(id);
} }

Spring Boot jpa Service层实现代码的更多相关文章

  1. 从零一起学Spring Boot之LayIM项目长成记(四) Spring Boot JPA 深入了解

    前言 本篇内容主要是一些关于JPA的常用的一些用法等.内容也是很多是看其他博客学来的,顺道在本系列博客里抽出一篇作为总结.下面让我们来看看吧. 不过我更推荐大家读本篇:https://lufficc. ...

  2. Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例

    Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...

  3. Spring Boot JPA 连接数据库

    本文将介绍怎样在Spring Boot project中加入JPA作为持久化方式. 改动 pom.xml 依赖 与上一篇介绍的 jdbc 不同的是 spring-boot-starter-jdbc 改 ...

  4. Spring boot Jpa添加对象字段使用数据库默认值

    Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...

  5. Spring Boot(五):Spring Boot Jpa 的使用

    在上篇文章Spring Boot(二):Web 综合开发中简单介绍了一下 Spring Boot Jpa 的基础性使用,这篇文章将更加全面的介绍 Spring Boot Jpa 常见用法以及注意事项. ...

  6. Spring Boot Jpa 的使用

    Spring Boot Jpa 介绍 首先了解 Jpa 是什么? Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化规范.它为 Java 开发人员提供了一种 ...

  7. (转)Spring Boot(五):Spring Boot Jpa 的使用

    http://www.ityouknow.com/springboot/2016/08/20/spring-boot-jpa.html 在上篇文章Spring Boot(二):Web 综合开发中简单介 ...

  8. Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题

      (转载)Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题   这几天在用spring boot开发项目, 在开发的过程中遇到一个问题hibernate在执 ...

  9. Spring boot JPA 用自定义主键策略 生成自定义主键ID

    最近学习Spring boot JPA 学习过程解决的一些问题写成随笔,大家一起成长.这次遇到自定义主键的问题 package javax.persistence; public enum Gener ...

随机推荐

  1. 三十九、python面向对象一

    A.python面向对象 1.面向对象基础:面向对象三个特性:封装,继承,多态C# java 只能用面向对象编程Ruby,python 函数+面向对象 函数式编程:def 函数def f1(a): r ...

  2. GitHub - 解决 GitHub Page 404

    带有下划线的文件报 404 解决:在仓库文件夹根目录添加.nojekyll文件 参见: Bypassing Jekyll on GitHub Pages - The GitHub Blog How t ...

  3. WAMP搭建与配置

    使用WampServer整合软件包进行WAMP环境搭建 WampServer是一款由法国人开发的Apache Web服务器.PHP解释器以及MySQL数据库的整合软件包.免去了开发人员将时间花费在繁琐 ...

  4. Spring bean的自动装配属性

    bean的自动装配属性能简化xml文件配置. bean 的自动装配属性分为四种: 1.byName 2.byTyoe 3.constructor 4. autodetect byName: 它查找配置 ...

  5. 四种方法 恢复损坏的Excel文档

    四种方法 恢复损坏的Excel文档 打开一个以前编辑好的Excel工作簿,却发现内容混乱,无法继续进行编辑,而且还不能够进行打印.这是很多朋友在处理Excel文件时都可能会遇到的一个问题,面对这种情况 ...

  6. oracle rman catalog--ORA-01580: error creating control backup file

    在测试rman catalog时,错误的设置了snapshot路径,报错 RMAN> show snapshot controlfile name; RMAN configuration par ...

  7. react-native start error Invalid regular expression:

    详细错误: error Invalid regular expression: /(.*\\__fixtures__\\.*|node_modules[\\\]react[\\\]dist[\\\]. ...

  8. CSS3——对齐 组合选择符 伪类 伪元素 导航栏 下拉菜单

     水平&垂直对齐 元素居中对齐 .center { margin: auto; width: 50%; border: 3px solid green; padding: 10px; } 文本 ...

  9. 【FICO系列】SAP FICO FS00修改科目为未清项目管理

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO FS00修改科 ...

  10. r子集代码实现(递归)

    #!/usr/bin/env python #coding:utf-8 SET_START = 1 SET_END = 9 SUB_LEN = 10 def r_subset(i, r, pre, a ...