idea 建立JPA项目(二)
作者:互联网
建立Entity
package com.example.jpademo.Entity; import lombok.Data; import javax.persistence.*; @Entity @Table(name="t_book") @Data public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(length = 100) private String name; @Column(length = 50) private String author; }
建立控制器
package com.example.jpademo.Controller; import com.example.jpademo.Dao.BookRepository; import com.example.jpademo.Entity.Book; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.util.List; @Controller @RequestMapping("/book") public class BookController { @Resource private BookRepository bookRepository; private Book book; //查询所有图书 @RequestMapping("/list") public ModelAndView list() { ModelAndView mav=new ModelAndView(); mav.addObject("booklist",bookRepository.findAll()); mav.setViewName("bookList"); return mav; } //添加图书 @PostMapping("/add") public String add(Book book) { bookRepository.save(book); //return "redirect:/book/list"; return "forward:/book/list"; // url不变 } //根据id查询book @RequestMapping(value = "/preUpdate/{id}") public ModelAndView preUpdate(@PathVariable("id")Integer id) { ModelAndView mav=new ModelAndView(); mav.addObject("book",bookRepository.getById(id)); mav.setViewName("bookUpdate"); return mav; } //修改图书 @PostMapping(value = "/update") public String update(Book book) { bookRepository.save(book); return "forward:/book/list"; } //删除 @GetMapping("/delete") public String delete(Integer id) { bookRepository.deleteById(id); return "forward:/book/list"; } //根据条件动态查询 @RequestMapping("/list2") public ModelAndView list2(Book book) { ModelAndView mav=new ModelAndView(); List<Book> bookList=bookRepository.findAll(new Specification<Book>() { @Override public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Predicate predicate=cb.conjunction(); if(book!=null) { if(book.getName()!=null && !"".equals(book.getName())) { predicate.getExpressions().add(cb.like(root.get("name"),"%"+book.getName()+"%")); } if(book.getAuthor()!=null && !"".equals(book.getAuthor())) { predicate.getExpressions().add(cb.like(root.get("author"),"%"+book.getAuthor()+"%")); } } return predicate; } }); mav.addObject("book",book); mav.addObject("booklist",bookList); mav.setViewName("booklist"); return mav; } //查询 @ResponseBody @RequestMapping("/query") public List<Book> findByName(String name) //url中可以没有请求参数,用了@RequestParam 就必须有请求参数 { return bookRepository.findByName("建筑设计"); } //查询1 rul必须带参数 , 返回的是json格式 @ResponseBody @RequestMapping("/query1") public List<Book> findByName1(@RequestParam String name) { return bookRepository.findByName(name); } //随机显示 @ResponseBody @RequestMapping("/randomlist") public List<Book> randomList(String name) { return bookRepository.randomList(2); } }
建立接口
package com.example.jpademo.Dao; import com.example.jpademo.Entity.Book; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import java.util.List; public interface BookRepository extends JpaRepository<Book,Integer> , JpaSpecificationExecutor<Book> //???? { @Query(value ="select * from t_book where t_book.name like %?1% ",nativeQuery = true) public List<Book> findByName(String name); /* SQL SERVER中对查询结果随机排序 对结果记录随机排序,或随机返回X条记录,可以通过在SELECT语句中使用RAND函数来实现。但是RAND函数在查询中只生成一次,因此每一行都将得到相同的值。可以通过在ORDER BY子句中使用NEWID函数来对结果进行排序的方法来实现,代码如下: SELECT * FROM Northwind.Orders ORDER BY NEWID() SELECT TOP 10 * FROM Northwind.Orders ORDER BY NEWID() */ @Query(value = "select top 5 * from t_book order by NEWID() ",nativeQuery = true) //支持 //@Query(value = "select top ?1 * from t_book order by NEWID() ",nativeQuery = true) //不支持 ?????? public List<Book> randomList( Integer count); }
在static目录建立bookAdd.html
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html"> <head> <meta charset="UTF-8"> <title>增加图书</title> </head> <body> <form action="/book/add" method="post"> 图书名称:<input type="text" name="name"/><br/> 图书作者:<input type="test" name="author"/><br/> <input type="submit" value="提交"/> </form> </body> </html>
在templates目录建立bookList.html
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>图书管理</title> </head> <body> <a href="/bookAdd.html">添加</a></br> <table> <tr> <th>编号</th> <th>图书名称</th> <th>图书作者</th> <th>操作</th> </tr> </table> <p th:each="book:${booklist}"> <tr> <td th:text="${book.id}"></td> <td th:text="${book.name}"></td> <td th:text="${book.author}"></td> <td> <a th:href="'/book/preUpdate/'+${book.id}">修改</a> <a th:href="'/book/delete?id='+${book.id}">删除</a> </td> </tr> </p> </body> </html>
在templates建立bookUpdate.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>图书修改</title> </head> <body> <!--th:action="@{/book/update}" 或 th:action="'/book/update'"都正确, th:action="/book/update"错误,但是taction="'/book/update"正确--> <form th:action="@{/book/update}" th:method="post"> <input th:type="hidden" th:name="id" th:value="${book.id}"/> 图书名称:<input th:type="text" th:name="name" th:value="${book.name}"/><br/> 图书作者:<input th:type="text" th:name="author" th:value="${book.author}"/><br/> <input th:type="submit" th:value="提交"/> </form> </body> </html>
标签:return,项目,JPA,mav,idea,book,ModelAndView,import,public 来源: https://www.cnblogs.com/wfy680/p/14998654.html