亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Boot jpa Service層代碼實(shí)例

 更新時(shí)間:2019年10月07日 09:24:33   作者:行之間  
這篇文章主要介紹了Spring Boot jpa Service層代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Spring Boot jpa Service層代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

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();
  }

  /**
   * 多條件動(dòng)態(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);
  }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java實(shí)現(xiàn)XML增加元素操作簡(jiǎn)單示例

    java實(shí)現(xiàn)XML增加元素操作簡(jiǎn)單示例

    這篇文章主要介紹了java實(shí)現(xiàn)XML增加元素操作,結(jié)合簡(jiǎn)單實(shí)例形式分析了java針對(duì)xml格式數(shù)據(jù)的讀取、遍歷、創(chuàng)建等操作技巧,需要的朋友可以參考下
    2017-02-02
  • Java程序執(zhí)行的全流程

    Java程序執(zhí)行的全流程

    這篇文章主要介紹了Java程序執(zhí)行的全流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控的步驟

    SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控的步驟

    這篇文章主要介紹了SpringBoot整合RedisTemplate實(shí)現(xiàn)緩存信息監(jiān)控,一步一步的實(shí)現(xiàn)?Springboot?整合?Redis?來(lái)存儲(chǔ)數(shù)據(jù),讀取數(shù)據(jù),需要的朋友可以參考下
    2022-01-01
  • Java數(shù)據(jù)結(jié)構(gòu)之選擇排序算法的實(shí)現(xiàn)與優(yōu)化

    Java數(shù)據(jù)結(jié)構(gòu)之選擇排序算法的實(shí)現(xiàn)與優(yōu)化

    選擇排序:(Selection?sort)是一種簡(jiǎn)單直觀的排序算法,也是一種不穩(wěn)定的排序方法。本文主要為大家介紹一下選擇排序的實(shí)現(xiàn)與優(yōu)化,希望對(duì)大家有所幫助
    2023-01-01
  • Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼

    Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼

    本篇文章主要介紹了Java使用Jedis操作Redis服務(wù)器的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • 利用Spring Session和redis對(duì)Session進(jìn)行共享詳解

    利用Spring Session和redis對(duì)Session進(jìn)行共享詳解

    這篇文章主要給大家介紹了關(guān)于利用Spring、Session和redis對(duì)Session進(jìn)行共享的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • SpringMVC基于注解的Controller詳解

    SpringMVC基于注解的Controller詳解

    這篇文章主要介紹了SpringMVC基于注解的Controller詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Kotlin + Spring Boot 請(qǐng)求參數(shù)驗(yàn)證的代碼實(shí)例

    Kotlin + Spring Boot 請(qǐng)求參數(shù)驗(yàn)證的代碼實(shí)例

    本篇文章主要介紹了Kotlin + Spring Boot 請(qǐng)求參數(shù)驗(yàn)證的代碼實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Spring Boot運(yùn)行部署過(guò)程圖解

    Spring Boot運(yùn)行部署過(guò)程圖解

    這篇文章主要介紹了Spring Boot運(yùn)行部署過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 深入理解Java IO的flush

    深入理解Java IO的flush

    本篇文章是小編總結(jié)的關(guān)于Java IO的flush的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友可以跟著學(xué)習(xí)下。
    2018-06-06

最新評(píng)論