當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot 菜鸟教程 2 Data JPA
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot 菜鸟教程 2 Data JPA
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
GitHub
src="//ghbtns.com/github-btn.html?user=je-ge&repo=spring-boot&type=watch&count=true" scrolling="0" width="110" height="20">Spring Data JPA
用來簡化創建 JPA 數據訪問層和跨存儲的持久層功能。
官網文檔連接
http://docs.spring.io/spring-data/jpa/docs/current/reference/html/
Spring Data JPA提供的接口
- Repository:最頂層的接口,是一個空的接口,目的是為了統一所有Repository的類型,且能讓組件掃描的時候自動識別
- CrudRepository :是Repository的子接口,提供CRUD的功能
- PagingAndSortingRepository:是CrudRepository的子接口,添加分頁和排序的功能
- JpaRepository:是PagingAndSortingRepository的子接口,增加了一些實用的功能,比如:批量操作等
- JpaSpecificationExecutor:用來做負責查詢的接口
- Specification:是Spring Data JPA提供的一個查詢規范,要做復雜的查詢,只需圍繞這個規范來設置查詢條件即可
Repository接口查詢規則
| And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
| Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
| Is,Equals | findByFirstname,findByFirstnameIs,findByFirstnameEquals | … where x.firstname = ?1 |
| Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
| LessThan | findByAgeLessThan | … where x.age < ?1 |
| LessThanEqual | findByAgeLessThanEqual | … where x.age <= ?1 |
| GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
| GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
| After | findByStartDateAfter | … where x.startDate > ?1 |
| Before | findByStartDateBefore | … where x.startDate < ?1 |
| IsNull | findByAgeIsNull | … where x.age is null |
| IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
| Like | findByFirstnameLike | … where x.firstname like ?1 |
| NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
| StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 (parameter bound with appended %) |
| EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (parameter bound with prepended %) |
| Containing | findByFirstnameContaining | … where x.firstname like ?1 (parameter bound wrapped in %) |
| OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
| Not | findByLastnameNot | … where x.lastname <> ?1 |
| In | findByAgeIn(Collection ages) | … where x.age in ?1 |
| NotIn | findByAgeNotIn(Collection age) | … where x.age not in ?1 |
| TRUE | findByActiveTrue() | … where x.active = true |
| FALSE | findByActiveFalse() | … where x.active = false |
| IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
項目圖片
pom.xml
-只需要在pom.xml引入需要的數據庫配置,就會自動訪問此數據庫,如果需要配置其他數據庫,可以在application.properties進行添加
-默認使用org.apache.tomcat.jdbc.pool.DataSource創建連接池
模型對象User
package com.jege.spring.boot.data.jpa.entity;import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table;/*** @author JE哥* @email 1272434821@qq.com* @description:jpa模型對象*/ @Entity @Table(name = "t_user") public class User {@Id@GeneratedValueprivate Long id;private String name;private Integer age;public User() {}public User(String name, Integer age) {this.name = name;this.age = age;}}持久層UserRepository
package com.jege.spring.boot.data.jpa.repository;import java.util.List;import org.springframework.data.jpa.repository.JpaRepository;import com.jege.spring.boot.data.jpa.entity.User;/*** @author JE哥* @email 1272434821@qq.com* @description:持久層接口,由spring自動生成其實現*/ public interface UserRepository extends JpaRepository<User, Long> {List<User> findByNameLike(String name);}啟動類Application
package com.jege.spring.boot;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author JE哥* @email 1272434821@qq.com* @description:spring boot 啟動類*/@SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}配置文件application.properties
## JPA Settings spring.jpa.generate-ddl: true spring.jpa.show-sql: true spring.jpa.hibernate.ddl-auto: create spring.jpa.properties.hibernate.format_sql: false測試類UserRepositoryTest
package com.jege.spring.boot.data.jpa;import static org.assertj.core.api.Assertions.assertThat;import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.jege.spring.boot.data.jpa.entity.User; import com.jege.spring.boot.data.jpa.repository.UserRepository;/*** @author JE哥* @email 1272434821@qq.com* @description:*/ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest() public class UserRepositoryTest {@AutowiredUserRepository userRepository;// 打印出class com.sun.proxy.$Proxy66表示spring注入通過jdk動態代理獲取接口的子類@Testpublic void proxy() throws Exception {System.out.println(userRepository.getClass());}@Testpublic void save() throws Exception {for (int i = 0; i < 10; i++) {User user = new User("jege" + i, 25 + i);userRepository.save(user);}}@Testpublic void all() throws Exception {save();assertThat(userRepository.findAll()).hasSize(10);}@Testpublic void findByName() throws Exception {save();assertThat(userRepository.findByNameLike("jege%")).hasSize(10);}@Afterpublic void destroy() throws Exception {userRepository.deleteAll();}}源碼地址
https://github.com/je-ge/spring-boot
如果覺得我的文章或者代碼對您有幫助,可以請我喝杯咖啡。
您的支持將鼓勵我繼續創作!謝謝!
總結
以上是生活随笔為你收集整理的Spring Boot 菜鸟教程 2 Data JPA的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud 菜鸟教程 1
- 下一篇: thymeleaf菜鸟教程_Spring