簡介: JPA(java Persistence API)和SpringData是兩個範疇的概念。spring data jpa是spring公司下的spring data項目的一個模塊。 spring data jpa定義了介面來進行持久層的編寫規範,同時還大大簡化了持久層的CRUD操作。 從此可 ...
簡介:
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); }