SpringBoot整合持久层技术--(三)Spring Data JPA
简介:
JPA(java Persistence API)和SpringData是两个范畴的概念。spring data jpa是spring公司下的spring data项目的一个模块。
spring data jpa定义了接口来进行持久层的编写规范,同时还大大简化了持久层的CRUD操作。
从此可以看出,spring data jpa与jpa之间并没有直接的关系。
jpa是由sun公司定义的持久层规范,但是jpa 并没有做任何简化,其中只有一堆接口。
而spring data jpa中不仅有接口,还有实现类,正是这些实现类实现了CRUD操作的简化,但是实现类并不做CRUD操作。
spring data jpa和jpa一样,虽然定义了持久层的编写规范,但是持久层的具体操作需要由第三方框架来做,它自己并不能做相应的CRUD操作。
简而言之,spring data jpa和jpa的区别:
1、spring data jpa由spring提供 jpa由sun公司提供
2、两者属于同一等级,都是持久层的规范,spring data jpa对CRUD操作做了简化
两者都可以管理任何第三方持久层框架。是同一级别的。
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
application.properties
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql:///chapter05 spring.datasource.username=root spring.datasource.password=123456 spring.jpa.show-sql=true spring.jpa.database=mysql spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect #spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57InnoDBDialect #spring.jpa.properties.database=mysql #spring.jpa.properties.hibernate.hbm2ddl.auto=update #spring.jpa.properties.show-sql= true
实体类,controller,service,dao
@Entity(name = "t_book") public class Book { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @Column(name = "book_name",nullable = false) private String name; private String author; private Float price; @Transient private String description; //省略getter/setter }
@Entity表示注解的类是实体类,name值为表名,项目启动时自动生成一张表
@Id表示改属性是主键,@GeneratedValue表示主键自动生成,strategy表示主键生成策略@Transientb表示生成数据库表时属性被忽略,就是数据表没有这个字段。
@RestController public class BookController { @Autowired BookService bookService; @GetMapping("/findAll") public void findAll() { PageRequest pageable = PageRequest.of(0, 2); 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.getNumberOfElements()); System.out.println("每页记录数:"+page.getSize()); } @GetMapping("/search") public void search() { List<Book> bs1 = bookService.getBookByIdAndAuthor("鲁迅", 1); List<Book> bs2 = bookService.getBooksByAuthorStartingWith("吴"); List<Book> bs3 = bookService.getBooksByIdAndName("西", 3); 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() { Book book = new Book(); book.setAuthor("鲁迅"); book.setName("呐喊"); book.setPrice(23F); bookService.addBook(book); } }
@Service public class BookService { @Autowired 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.getBookByIdAndAuthor(author, id); } public List<Book> getBooksByIdAndName(String name, Integer id){ return bookDao.getBooksByIdAndName(name, id); } }
public interface BookDao extends JpaRepository<Book,Integer>{ List<Book> getBooksByAuthorStartingWith(String author); List<Book> getBooksByPriceGreaterThan(Float price); @Query(value = "select * from t_book where id=(select max(id) from t_book)",nativeQuery = true) Book getMaxIdBook(); @Query("select b from t_book b where b.id>:id and b.author=:author") List<Book> getBookByIdAndAuthor(@Param("author") String author, @Param("id") Integer id); @Query("select b from t_book b where b.id<?2 and b.name like %?1%") List<Book> getBooksByIdAndName(String name, Integer id); }