SpringBoot动态查询 Specification使用

2021-07-14 22:06

阅读:736

YPE html>

标签:over   控制   9.png   iss   open   .data   link   oct   delete   

   有时候需要用到动态查询。动态查询 Specification  使用需实现接口JpaSpecificationExecutor

技术分享图片技术分享图片
 1 package com.hik.dao;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.data.jpa.repository.JpaRepository;
 6 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 7 import org.springframework.data.jpa.repository.Query;
 8 
 9 import com.hik.entity.Book;
10 
11 /**
12  * 图书Dao接口
13  * @author jed
14  *
15  */
16 public interface BookDao extends JpaRepository,JpaSpecificationExecutor {
17     
18     //Hql语句查询
19     @Query("select b from Book b where b.bookName like %?1%")
20     public List findByBookName(String bookName);
21     
22     //本地sql语句查询
23     @Query(value="select * from t_book order by RAND() limit ?1",nativeQuery=true)
24     public List randomList(Integer n);
25 }
View Code

JpaSpecificationExecutor接口方法

技术分享图片技术分享图片
 1 /*
 2  * Copyright 2008-2011 the original author or authors.
 3  *
 4  * Licensed under the Apache License, Version 2.0 (the "License");
 5  * you may not use this file except in compliance with the License.
 6  * You may obtain a copy of the License at
 7  *
 8  *      http://www.apache.org/licenses/LICENSE-2.0
 9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.springframework.data.jpa.repository;
17 
18 import java.util.List;
19 
20 import org.springframework.data.domain.Page;
21 import org.springframework.data.domain.Pageable;
22 import org.springframework.data.domain.Sort;
23 import org.springframework.data.jpa.domain.Specification;
24 
25 /**
26  * Interface to allow execution of {@link Specification}s based on the JPA criteria API.
27  * 
28  * @author Oliver Gierke
29  */
30 public interface JpaSpecificationExecutor {
31 
32     /**
33      * Returns a single entity matching the given {@link Specification}.
34      * 
35      * @param spec
36      * @return
37      */
38     T findOne(Specification spec);
39 
40     /**
41      * Returns all entities matching the given {@link Specification}.
42      * 
43      * @param spec
44      * @return
45      */
46     List findAll(Specification spec);
47 
48     /**
49      * Returns a {@link Page} of entities matching the given {@link Specification}.
50      * 
51      * @param spec
52      * @param pageable
53      * @return
54      */
55     Page findAll(Specification spec, Pageable pageable);
56 
57     /**
58      * Returns all entities matching the given {@link Specification} and {@link Sort}.
59      * 
60      * @param spec
61      * @param sort
62      * @return
63      */
64     List findAll(Specification spec, Sort sort);
65 
66     /**
67      * Returns the number of instances that the given {@link Specification} will return.
68      * 
69      * @param spec the {@link Specification} to count instances for
70      * @return the number of instances
71      */
72     long count(Specification spec);
73 }
View Code

Specification接口

技术分享图片技术分享图片
 1 /*
 2  * Copyright 2008-2017 the original author or authors.
 3  *
 4  * Licensed under the Apache License, Version 2.0 (the "License");
 5  * you may not use this file except in compliance with the License.
 6  * You may obtain a copy of the License at
 7  *
 8  *      http://www.apache.org/licenses/LICENSE-2.0
 9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.springframework.data.jpa.domain;
17 
18 import javax.persistence.criteria.CriteriaBuilder;
19 import javax.persistence.criteria.CriteriaQuery;
20 import javax.persistence.criteria.Predicate;
21 import javax.persistence.criteria.Root;
22 
23 /**
24  * Specification in the sense of Domain Driven Design.
25  * 
26  * @author Oliver Gierke
27  * @author Thomas Darimont
28  * @author Krzysztof Rzymkowski
29  */
30 public interface Specification {
31 
32     /**
33      * Creates a WHERE clause for a query of the referenced entity in form of a {@link Predicate} for the given
34      * {@link Root} and {@link CriteriaQuery}.
35      * 
36      * @param root
37      * @param query
38      * @return a {@link Predicate}, may be {@literal null}.
39      */
40     Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb);
41 }
View Code

BookDao接口类:

技术分享图片技术分享图片
 1 package com.hik.dao;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.data.jpa.repository.JpaRepository;
 6 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 7 import org.springframework.data.jpa.repository.Query;
 8 
 9 import com.hik.entity.Book;
10 
11 /**
12  * 图书Dao接口
13  * @author jed
14  *
15  */
16 public interface BookDao extends JpaRepository,JpaSpecificationExecutor {
17     
18     //Hql语句查询
19     @Query("select b from Book b where b.bookName like %?1%")
20     public List findByBookName(String bookName);
21     
22     //本地sql语句查询
23     @Query(value="select * from t_book order by RAND() limit ?1",nativeQuery=true)
24     public List randomList(Integer n);
25 }
View Code

BookController类

技术分享图片技术分享图片
  1 package com.hik.Controller;
  2 
  3 import java.util.List;
  4 
  5 import javax.annotation.Resource;
  6 import javax.persistence.criteria.CriteriaBuilder;
  7 import javax.persistence.criteria.CriteriaQuery;
  8 import javax.persistence.criteria.Predicate;
  9 import javax.persistence.criteria.Root;
 10 
 11 import org.springframework.data.jpa.domain.Specification;
 12 import org.springframework.stereotype.Controller;
 13 import org.springframework.web.bind.annotation.GetMapping;
 14 import org.springframework.web.bind.annotation.PathVariable;
 15 import org.springframework.web.bind.annotation.PostMapping;
 16 import org.springframework.web.bind.annotation.RequestMapping;
 17 import org.springframework.web.bind.annotation.RequestMethod;
 18 import org.springframework.web.bind.annotation.ResponseBody;
 19 import org.springframework.web.servlet.ModelAndView;
 20 
 21 import com.hik.dao.BookDao;
 22 import com.hik.entity.Book;
 23 
 24 /**
 25  * Book控制类
 26  * @author jed
 27  *
 28  */
 29 @Controller
 30 @RequestMapping("/book")
 31 public class BookController {
 32     
 33     @Resource
 34     private BookDao bookDao;
 35     
 36     /**
 37      * 查询所有图书
 38      * @return
 39      */
 40     @RequestMapping(value="/list")
 41     public ModelAndView list() {
 42         ModelAndView mav = new ModelAndView ();
 43         mav.addObject("bookList", bookDao.findAll());
 44         mav.setViewName("bookList");
 45         return mav;
 46     }
 47     
 48     /**
 49      * 根据条件动态查询
 50      * @param book
 51      * @return
 52      */
 53     @RequestMapping(value="/list2")
 54     public ModelAndView list2(Book book) {
 55         ModelAndView mav = new ModelAndView();
 56         List bookList = bookDao.findAll(new Specification() {
 57 
 58             @Override
 59             public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
 60                 Predicate predicate= cb.conjunction();
 61                 if(book!=null) {
 62                     if(book.getBookName()!=null && !"".equals(book.getBookName())) {
 63                         predicate.getExpressions().add(cb.like(root.get("bookName"), "%"+book.getBookName()+"%"));
 64                     }
 65                 }
 66                 return predicate;
 67             }
 68             
 69         });
 70         mav.addObject("bookList",bookList);
 71         mav.setViewName("bookList");
 72         return mav;
 73     }
 74 
 75     /**
 76      * 添加图书
 77      * @param book
 78      * @return
 79      */
 80     @RequestMapping(value="/add", method=RequestMethod.POST)
 81     public String add(Book book) {
 82         bookDao.save(book);
 83         return "forward:/book/list";
 84     }
 85     
 86     @GetMapping(value="/preUpdate/{id}")
 87     public ModelAndView preUpdate(@PathVariable("id") Integer id) {
 88         ModelAndView mav = new ModelAndView();
 89         mav.addObject("book", bookDao.getOne(id));
 90         mav.setViewName("bookUpdate");
 91         return mav;
 92     }
 93     
 94     /**
 95      * 修改图书
 96      * @param book
 97      * @return
 98      */
 99     @PostMapping(value="/update")
100     public String update(Book book) {
101         bookDao.save(book);
102         return "forward:/book/list";
103     }
104     
105     /**
106      * 删除图书
107      * @param id
108      * @return
109      */
110     @RequestMapping(value="/delete",method = RequestMethod.GET)
111     public String delete(Integer id) {
112         bookDao.delete(id);
113         return "forward:/book/list";
114     }
115     
116     @ResponseBody
117     @GetMapping(value="/queryByName")
118     public List queryByName() {
119         return bookDao.findByBookName("思想");
120     }
121     
122     @ResponseBody
123     @GetMapping(value="/randomList")
124     public List randomList(){
125         return bookDao.randomList(2);
126     }
127 }
View Code

bookList.ftl

技术分享图片技术分享图片
 1 
 2 
 3 4  5 图书管理页面 6 
 7 
 8 添加图书
 9 
10 图书名称:  11 12
13 141516171819202122232728 #list> 29
编号 图书名称 操作
${book.id} ${book.bookName} 24 修改 25 删除 26
30 31
View Code

访问:http://localhost:8888/book/list2

技术分享图片

输入查询条件,点击搜索

技术分享图片

查询结果:

技术分享图片

 

SpringBoot动态查询 Specification使用

标签:over   控制   9.png   iss   open   .data   link   oct   delete   

原文地址:https://www.cnblogs.com/jedjia/p/Specification.html


评论


亲,登录后才可以留言!