1、什么是Jpa
Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化規(guī)范。它為 Java 開(kāi)發(fā)人員提供了一種對(duì)象/關(guān)聯(lián)映射工具來(lái)管理 Java 應(yīng)用中的關(guān)系數(shù)據(jù)。它的出現(xiàn)主要是為了簡(jiǎn)化現(xiàn)有的持久化開(kāi)發(fā)工作和整合 ORM 技術(shù),結(jié)束現(xiàn)在 Hibernate,TopLink,JDO 等 ORM 框架各自為營(yíng)的局面。Jpa是一套規(guī)范。
2、Spring Boot Jpa
Spring Boot Jpa 是 Spring 基于 ORM 框架、Jpa 規(guī)范的基礎(chǔ)上封裝的一套 Jpa 應(yīng)用框架,可使開(kāi)發(fā)者用極簡(jiǎn)的代碼即可實(shí)現(xiàn)對(duì)數(shù)據(jù)的訪問(wèn)和操作。它提供了包括增刪改查等在內(nèi)的常用功能,且易于擴(kuò)展。學(xué)習(xí)并使用 Spring Data Jpa 可以極大提高開(kāi)發(fā)效率。
3、Jpa使用
基本查詢,繼承JpaRepository后有一些基本方法可以使用。重點(diǎn)理解ExampleMatcher
null Handler:Null值處理方式,枚舉類型,有2個(gè)可選值,INCLUDE(包括),IGNORE(忽略)。標(biāo)識(shí)作為條件的實(shí)體對(duì)象中,一個(gè)屬性值(條件值)為Null是,是否參與過(guò)濾。當(dāng)該選項(xiàng)值是INCLUDE時(shí),表示仍參與過(guò)濾,會(huì)匹配數(shù)據(jù)庫(kù)表中該字段值是Null的記錄;若為IGNORE值,表示不參與過(guò)濾。
defaultStringMatcher:默認(rèn)字符串匹配方式,枚舉類型,有6個(gè)可選值,DEFAULT(默認(rèn),效果同EXACT),EXACT(相等),STARTING(開(kāi)始匹配),ENDING(結(jié)束匹配),CONTAINING(包含,模糊匹配),REGEX(正則表達(dá)式)。該配置對(duì)所有字符串屬性過(guò)濾有效,除非該屬性在 propertySpecifiers 中單獨(dú)定義自己的匹配方式。
defaultIgnoreCase:默認(rèn)大小寫忽略方式,布爾型,當(dāng)值為false時(shí),即不忽略,大小不相等。該配置對(duì)所有字符串屬性過(guò)濾有效,除非該屬性在 propertySpecifiers 中單獨(dú)定義自己的忽略大小寫方式。
propertySpecifiers:各屬性特定查詢方式,描述了各個(gè)屬性單獨(dú)定義的查詢方式,每個(gè)查詢方式中包含4個(gè)元素:屬性名、字符串匹配方式、大小寫忽略方式、屬性轉(zhuǎn)換器。如果屬性未單獨(dú)定義查詢方式,或單獨(dú)查詢方式中,某個(gè)元素未定義(如:字符串匹配方式),則采用 ExampleMatcher 中定義的默認(rèn)值,即上面介紹的 defaultStringMatcher 和 defaultIgnoreCase 的值。
ignoredPaths:忽略屬性列表,忽略的屬性不參與查詢過(guò)濾。
Pageable pageable = PageRequest.of(user.getCurrentPage(),user.getPageSize(), Sort.Direction.ASC, "id");
Page<User> page = userRepository.findAll(pageable);
User user = userRepository.getOne(1L);
ExampleMatcher matcher = ExampleMatcher.matching()
//修改默認(rèn)匹配器為模糊查詢
// .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
//模糊查詢匹配開(kāi)頭,即{account}%
.withMatcher("account", ExampleMatcher.GenericPropertyMatchers.startsWith())
//全部模糊查詢,即%{password}%
.withMatcher("password" ,ExampleMatcher.GenericPropertyMatchers.contains())
//忽略屬性:是否關(guān)注。因?yàn)槭腔绢愋?,需要忽略? .withIgnorePaths("id");
Page<User> page = userRepository.findAll(example,pageable);
自定義簡(jiǎn)單查詢
自定義的簡(jiǎn)單查詢就是根據(jù)方法名來(lái)自動(dòng)生成 SQL,主要的語(yǔ)法是findXXBy,readAXXBy,queryXXBy,countXXBy, getXXBy后面跟屬性名稱:
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals 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)
自定義SQL查詢
@Transactional
@Modifying
@Query("update User u set u.userName = ?1 where u.id = ?2")
int modifyByIdAndUserId(String userName, Long id);