SpringBoot+SpringData快速完成微服务架构CRUD操作
SpringBoot+SpringData快速完成微服务架构CRUD操作
废话不多说,直接上项目演示
按条件查询
按名称查询
添加图书
可进行修改
可进行删除
后台运行的SQL语句
详细项目演示地址
若要代码请评论邮箱,我发给你~
先看下项目结构
该项目主要是以图书管理来做的
主运行程序
Controller层
package com.java1234.controller;
import java.util.List;
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 org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.java1234.entity.Book;
import com.java1234.repository.BookDao;
@Controller
@RequestMapping("/book")
public class BookController {
@Resource
private BookDao bookDao;
/**
* 查询所有图书
*
* @return
*/
@RequestMapping("/list")
public ModelAndView list() {
ModelAndView andView = new ModelAndView();
andView.addObject("bookList", bookDao.findAll());
andView.setViewName("bookList");
return andView;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(Book book) {
System.err.println(book.getName());
System.err.println(book.getAuthor());
bookDao.save(book);
return "forward:/book/list";
}
/**
* 根据Id查找
*
* @param id
* @return
*/
@RequestMapping(value = "/preUpdate/{id}")
public ModelAndView preUpdate(@PathVariable("id") Integer id) {
ModelAndView mv = new ModelAndView();
mv.addObject("book", bookDao.getOne(id));
mv.setViewName("bookUpdate");
return mv;
}
@RequestMapping("/addbook.html")
public String addbookOne() {
return "bookadd";
}
/**
* 根据条件动态查询
* @param book
* @return
*/
@RequestMapping("/list2")
public ModelAndView list2(Book book) {
ModelAndView mv = new ModelAndView();
List<Book> bookList = bookDao.findAll(new Specification<Book>() {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 动态拼接条件
*/
@Override
public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate pj = cb.conjunction();
if (book != null) {
if (book.getName()!=null && !"".equals(book.getName())) {
pj.getExpressions().add(cb.like(root.get("name"), "%"+book.getName()+"%"));
}
if (book.getAuthor()!=null && !"".equals(book.getAuthor())) {
pj.getExpressions().add(cb.like(root.get("author"), "%"+book.getAuthor()+"%"));
}
}
return pj;
}
});
mv.setViewName("bookList");
mv.addObject("bookList", bookList);
return mv;
}
/**
* 修改图书
*
* @param book
* @return
*/
/* @RequestMapping(value="/update"
* ,method=RequestMethod.POST) */
@PostMapping(value = "/update")
public String update(Book book) {
bookDao.save(book);
return "forward:/book/list";
}
@GetMapping("/delete")
public String delete(Integer id) {
bookDao.deleteById(id);
return "forward:/book/list";
}
@ResponseBody
@GetMapping("/queryByName")
public List<Book> queryByName(){
return bookDao.findByName("java");
}
/**
* 使用本地查询 随机查询两条
* @return
*/
@ResponseBody
@GetMapping("/randomList")
public List<Book> randomList(){
return bookDao.randomList(2);
}
}
实体类
package com.java1234.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_book")
public class Book {
@Id
@GeneratedValue
private Integer id;
@Column(length=100)
private String name;
@Column(length=50)
private String author;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
Dao层
package com.java1234.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import com.java1234.entity.Book;
public interface BookDao extends JpaRepository<Book, Integer>,JpaSpecificationExecutor<Book> {
// HQL
@Query("select b from Book b where b.name like %?1%")
public List<Book> findByName(String name);
@Query(value="select * from t_book order by RAND() LIMIT ?1",nativeQuery=true)
public List<Book> randomList(Integer n);
}
yml配置文件
POM.XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.java1234</groupId>
<artifactId>springboot-springData-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-springData-5</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
<!-- Springboot-springdata-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>