Spring Boot的数据访问 之Spring Boot + jpa的demo
1. 快速地创建一个项目,pom中选择如下
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. 新建一个实体,这边以Book对象为例,主要囊括了书店中书的一些信息
package com.example.demo.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import java.io.Serializable; @Entity public class Book implements Serializable { public static final long seriaVersionUID = 1L; @Id @GeneratedValue private long id; @Column(nullable=false,unique=true) private String name; @Column(nullable=false) private String author; @Column(nullable=false) private String ontime; @Column(nullable=false) private int number; @Column(nullable=false) private float price; @Column(nullable=false) private String createdt; @Column(nullable=false) private String updatedt; @Column(nullable=false) private String creatuid; @Column(nullable=false) private String updateuid; public long getId() { return id; } public void setId(int 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; } public String getOntime() { return ontime; } public void setOntime(String ontime) { this.ontime = ontime; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getCreatedt() { return createdt; } public void setCreatedt(String createdt) { this.createdt = createdt; } public String getUpdatedt() { return updatedt; } public void setUpdatedt(String updatedt) { this.updatedt = updatedt; } public String getCreatuid() { return creatuid; } public void setCreatuid(String creatuid) { this.creatuid = creatuid; } public String getUpdateuid() { return updateuid; } public void setUpdateuid(String updateuid) { this.updateuid = updateuid; } protected Book(){super();}; public Book(String name, String author, String ontime, int number, float price, String createdt, String updatedt, String creatuid, String updateuid) { this.name = name; this.author = author; this.ontime = ontime; this.number = number; this.price = price; this.createdt = createdt; this.updatedt = updatedt; this.creatuid = creatuid; this.updateuid = updateuid; } @Override public String toString() { return "Book{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + ", ontime='" + ontime + '\'' + ", number=" + number + ", price=" + price + ", createdt='" + createdt + '\'' + ", updatedt='" + updatedt + '\'' + ", creatuid='" + creatuid + '\'' + ", updateuid='" + updateuid + '\'' + '}'; } }
3. 新建一个BookRepository接口,且继承JpaRepository
public interface BookRepository extends JpaRepository<Book, Long> { }
4. 了解hibernate的ddl-auto四种模式和用途
# 服务端口 server.port=8083 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.ddl-auto = 可以是4中Value
create
create-drop
validate
update
Hibernate我们公司的正式项目中基本不用,原因是性能上不是很好,不知道其他公司使用起来如何?
hibernate在多线程下,拼接sql,知道数据库有问题,却很难定位到是哪一段sql。后来渐渐弃用。用mybaits了~
5. 添加controller进行新增数据
package com.example.demo.controller; import com.example.demo.entity.Book; import com.example.demo.entity.BookRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class BookController { @Autowired private BookRepository bookRepository; @RequestMapping("/save") @ResponseBody public void save() { bookRepository.save(new Book("书名11", "作者", "上架时间", 100, 23, "创建时间", "更新时间", "创建uid", "更新uid")); bookRepository.save(new Book("书名12", "作者", "20001111", 100, 23, "19990111", "19990111", "3", "4")); bookRepository.save(new Book("西游记10", "吴承恩", "19991211", 100, 20, "19991111", "19991111", "2", "3")); } }
项目启动起来,在浏览器中输入 http://localhost:8083/save,查询数据库中,已经新增了三条记录;
可以顺带测试下ddl-auto的四种值,是否有如同描述一般的作用
6. 再添加查询方法,并在controller中进行查询
package com.example.demo.entity; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface BookRepository extends JpaRepository<Book, Long> { List<Book> findByAuthor(String author); }
再在BookRepository 中添加查找方法,根据作者查询出符合的Book 列表
package com.example.demo.controller; import com.example.demo.entity.Book; import com.example.demo.entity.BookRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class BookController { @Autowired private BookRepository bookRepository; @RequestMapping("/save") public void save() { bookRepository.save(new Book("书名11", "作者", "上架时间", 100, 23, "创建时间", "更新时间", "创建uid", "更新uid")); bookRepository.save(new Book("书名12", "作者", "20001111", 100, 23, "19990111", "19990111", "3", "4")); bookRepository.save(new Book("西游记10", "吴承恩", "19991211", 100, 20, "19991111", "19991111", "2", "3")); } @RequestMapping("/findByAuthor") public void findByAuthor() { List<Book> result = bookRepository.findByAuthor("作者"); System.out.println("一共有多少条呢----" + result.size()); for (Book book : result) { System.out.println(book); } System.out.println("================================"); } }
保存,启动新项目,在浏览器中输入http://localhost:8083/findByAuthor,查看控制台是否打印出Book信息
------------------------- A little Progress a day makes you a big success... ----------------------------