Springboot 使用 JPA
Springboot 使用jpa
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
数据库配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql:///jpa
username: root
password: root
jpa:
show-sql: true
database: mysql
hibernate:
ddl-auto: update
book实体
package com.draymonder.book.jpa;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;
// 该类是一个实体类, 项目启动时会根据该类自动生成一张表
@Entity
public class Book {
// Id注解表示该属性是一个主键, @GeneratedValue注解表示主键自动生成
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
// Column可以定制属性字段, nullable
@Column(name="book_name", nullable=false)
private String name;
private String author;
private Float price;
// Transient 在生成数据库的表, 该属性被忽略
@Transient
private String desc;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", desc='" + desc + '\'' +
'}';
}
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;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
bookDao
package com.draymonder.book.jpa;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface BookDao extends JpaRepository<Book, Integer> {
List<Book> getBooksByAuthorStartingWith(String author);
List<Book> getBooksByPriceGreaterThan(Float price);
@Query(value = "select * from book where id=(select max(id) from book)", nativeQuery = true)
Book getMaxIdBook();
@Query("select b from Book b where b.id > :id and b.author = :author")
List<Book> getBookByTry1(@Param("author") String author, @Param("id") Integer id);
@Query("select b from Book b where b.id < ?2 and b.name like %?1%")
List<Book> getBooksByTry(String name, Integer id);
}
bookService
package com.draymonder.book.jpa;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@Service
public class BookService {
@Autowired
private BookDao bookDao;
public void addBook(Book book) {
bookDao.save(book);
}
public Page<Book> getBookByPage(Pageable pageable) {
return bookDao.findAll(pageable);
}
public List<Book> getBooksByAuthorStartingWith(String author) {
return bookDao.getBooksByAuthorStartingWith(author);
}
public List<Book> getBooksByPriceGreaterThan(Float price) {
return bookDao.getBooksByPriceGreaterThan(price);
}
public Book getMaxIdBook() {
return bookDao.getMaxIdBook();
}
public List<Book> getBookByIdAndAuthor(String author, Integer id) {
return bookDao.getBookByTry1(author, id);
}
public List<Book> getBooksByIdAndName(String name, Integer id) {
return bookDao.getBooksByTry(name, id);
}
}
bookController
package com.draymonder.book.jpa;
import java.util.List;
import javax.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BookController {
@Autowired
BookService bookService;
@GetMapping("/findAll")
public void findAll() {
PageRequest pageable = PageRequest.of(0, 3);
Page<Book> page = bookService.getBookByPage(pageable);
System.out.println("总页数: " + page.getTotalPages());
System.out.println("总记录数: " + page.getTotalElements());
System.out.println("查询结果: " + page.getContent());
System.out.println("当前页数: " + (page.getNumber() + 1));
System.out.println("每页记录数: " + page.getSize());
}
@GetMapping("/search")
public void search() {
List<Book> bs1 = bookService.getBookByIdAndAuthor("鲁迅", 7);
List<Book> bs2 = bookService.getBooksByAuthorStartingWith("吴");
List<Book> bs3 = bookService.getBooksByIdAndName("西", 8);
List<Book> bs4 = bookService.getBooksByPriceGreaterThan(30f);
Book b = bookService.getMaxIdBook();
System.out.println("bs1: " + bs1);
System.out.println("bs2: " + bs2);
System.out.println("bs3: " + bs3);
System.out.println("bs4: " + bs4);
System.out.println("b: " + b);
}
@GetMapping("/save")
public void save(@RequestParam(value="author") String author,
@RequestParam(value="name") String name) {
System.out.println("start");
if (author.isEmpty() || name.isEmpty()) {
return;
}
Book book = new Book();
book.setAuthor(author);
book.setName(name);
book.setPrice(30f);
bookService.addBook(book);
System.out.println("end");
}
}
参考文档
JPQL: jianshu.com/p/4a4410075bab
jpa补充: http://www.ityouknow.com/springboot/2016/08/20/spring-boot-jpa.html