SpringBoot之自定义查询Query

2021-07-15 01:08

阅读:516

标签:order   nbsp   .com   object   limit   servlet   random   splay   Servle   

   下面讲解下SpringBoot之自定义查询Query的实例

SpringBoot之自定义查询Query有HQL语句查询(Hibernate),还可以采用sql语句本地查询

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.Query;
 7 
 8 import com.hik.entity.Book;
 9 
10 /**
11  * 图书Dao接口
12  * @author jed
13  *
14  */
15 public interface BookDao extends JpaRepository{
16     
17     //Hql语句查询
18     @Query("select b from Book b where b.bookName like %?1%")
19     public List findByBookName(String bookName);
20     
21     //本地sql语句查询
22     @Query(value="select * from t_book order by RAND() limit ?1",nativeQuery=true)
23     public List randomList(Integer n);
24 }
View Code

BookController类

技术分享图片技术分享图片
 1 package com.hik.Controller;
 2 
 3 import java.util.List;
 4 
 5 import javax.annotation.Resource;
 6 
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.GetMapping;
 9 import org.springframework.web.bind.annotation.PathVariable;
10 import org.springframework.web.bind.annotation.PostMapping;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RequestMethod;
13 import org.springframework.web.bind.annotation.ResponseBody;
14 import org.springframework.web.servlet.ModelAndView;
15 
16 import com.hik.dao.BookDao;
17 import com.hik.entity.Book;
18 
19 /**
20  * Book控制类
21  * @author jed
22  *
23  */
24 @Controller
25 @RequestMapping("/book")
26 public class BookController {
27     
28     @Resource
29     private BookDao bookDao;
30     
31     /**
32      * 查询所有图书
33      * @return
34      */
35     @RequestMapping(value="/list")
36     public ModelAndView list() {
37         ModelAndView mav = new ModelAndView ();
38         mav.addObject("bookList", bookDao.findAll());
39         mav.setViewName("bookList");
40         return mav;
41     }
42 
43     /**
44      * 添加图书
45      * @param book
46      * @return
47      */
48     @RequestMapping(value="/add", method=RequestMethod.POST)
49     public String add(Book book) {
50         bookDao.save(book);
51         return "forward:/book/list";
52     }
53     
54     @GetMapping(value="/preUpdate/{id}")
55     public ModelAndView preUpdate(@PathVariable("id") Integer id) {
56         ModelAndView mav = new ModelAndView();
57         mav.addObject("book", bookDao.getOne(id));
58         mav.setViewName("bookUpdate");
59         return mav;
60     }
61     
62     /**
63      * 修改图书
64      * @param book
65      * @return
66      */
67     @PostMapping(value="/update")
68     public String update(Book book) {
69         bookDao.save(book);
70         return "forward:/book/list";
71     }
72     
73     /**
74      * 删除图书
75      * @param id
76      * @return
77      */
78     @RequestMapping(value="/delete",method = RequestMethod.GET)
79     public String delete(Integer id) {
80         bookDao.delete(id);
81         return "forward:/book/list";
82     }
83     
84     @ResponseBody
85     @GetMapping(value="/queryByName")
86     public List queryByName() {
87         return bookDao.findByBookName("思想");
88     }
89     
90     @ResponseBody
91     @GetMapping(value="/randomList")
92     public List randomList(){
93         return bookDao.randomList(2);
94     }
95 }
View Code

hql查询结果:

技术分享图片

 

 本地sql查询结果:

技术分享图片

 

SpringBoot之自定义查询Query

标签:order   nbsp   .com   object   limit   servlet   random   splay   Servle   

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


评论


亲,登录后才可以留言!