日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Spring Boot Data JPA

發(fā)布時(shí)間:2025/3/12 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot Data JPA 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Spring Data JPA簡(jiǎn)介

用來簡(jiǎn)化創(chuàng)建 JPA 數(shù)據(jù)訪問層和跨存儲(chǔ)的持久層功能。

Spring Data JPA提供的接口

Repository:最頂層的接口,是一個(gè)空的接口,目的是為了統(tǒng)一所有Repository的類型,且能讓組件掃描的時(shí)候自動(dòng)識(shí)別。

CrudRepository :是Repository的子接口,提供CRUD的功能。

PagingAndSortingRepository:是CrudRepository的子接口,添加分頁和排序的功能。

JpaRepository:是PagingAndSortingRepository的子接口,增加了一些實(shí)用的功能,比如:批量操作等。

JpaSpecificationExecutor:用來做負(fù)責(zé)查詢的接口。

Specification:是Spring Data JPA提供的一個(gè)查詢規(guī)范,要做復(fù)雜的查詢,只需圍繞這個(gè)規(guī)范來設(shè)置查詢條件即可。

Repository接口查詢規(guī)則

關(guān)鍵字 @ 案例 @效果

And @ findByLastnameAndFirstname @ … where x.lastname = ?1 and x.firstname = ?2Or @ findByLastnameOrFirstname @ … where x.lastname = ?1 or x.firstname = ?2 Is,Equals@findByFirstname,findByFirstnameIs,findByFirstnameEquals@… where x.firstname = ?1Between@findByStartDateBetween@… where x.startDate between ?1 and ?2LessThan@findByAgeLessThan@… where x.age < ?1LessThanEqual @findByAgeLessThanEqual@… where x.age <= ?1GreaterThan @findByAgeGreaterThan@… where x.age > ?1GreaterThanEqual @findByAgeGreaterThanEqual@… where x.age >= ?1After @findByStartDateAfter@… where x.startDate > ?1Before@findByStartDateBefore@… where x.startDate < ?1IsNull@findByAgeIsNull@… where x.age is nullIsNotNull,NotNull@findByAge(Is)NotNull@… where x.age not nullLike @findByFirstnameLike@… where x.firstname like ?1NotLike @findByFirstnameNotLike@… where x.firstname not like ?1StartingWith@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 descNot@findByLastnameNot@… where x.lastname <> ?1In @findByAgeIn(Collection<Age> ages)@… where x.age in ?1NotIn @findByAgeNotIn(Collection<Age> age)@… where x.age not in ?1TRUE@findByActiveTrue()@… where x.active = trueFALSE @findByActiveFalse()@… where x.active = falseIgnoreCase@findByFirstnameIgnoreCase@… where UPPER(x.firstame) = UPPER(?1)

項(xiàng)目圖片

pom.xml

只需要在pom.xml引入需要的數(shù)據(jù)庫配置,就會(huì)自動(dòng)訪問此數(shù)據(jù)庫,如果需要配置其他數(shù)據(jù)庫,可以在application.properties進(jìn)行添加

默認(rèn)使用org.apache.tomcat.jdbc.pool.DataSource創(chuàng)建連接池

<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><groupId>com.jege.spring.boot</groupId><artifactId>spring-boot-data-jpa</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>spring-boot-data-jpa</name><url>http://maven.apache.org</url><!-- 公共spring-boot配置,下面依賴jar文件不用在寫版本號(hào) --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.1.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><!-- 持久層 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- h2內(nèi)存數(shù)據(jù)庫 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><finalName>spring-boot-data-jpa</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin></plugins></build> </project>

模型對(duì)象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;/*** @jpa模型對(duì)象*/ @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;/***持久層接口,由spring自動(dòng)生成其實(shí)現(xiàn)*/ public interface UserRepository extends JpaRepository<User, Long> {List<User> findByNameLike(String name);}

啟動(dòng)類Application

package com.jege.spring.boot;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** spring boot 啟動(dòng)類*/@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

測(cè)試類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;@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest() public class UserRepositoryTest {@AutowiredUserRepository userRepository;// 打印出class com.sun.proxy.$Proxy66表示spring注入通過jdk動(dòng)態(tài)代理獲取接口的子類@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();}}

掃一掃獲取更多相關(guān)資訊喲!!!

總結(jié)

以上是生活随笔為你收集整理的Spring Boot Data JPA的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。