Spring Boot Jpa

2020-07-03  本文已影响0人  yellow_han

1、什么是Jpa

Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化规范。它为 Java 开发人员提供了一种对象/关联映射工具来管理 Java 应用中的关系数据。它的出现主要是为了简化现有的持久化开发工作和整合 ORM 技术,结束现在 Hibernate,TopLink,JDO 等 ORM 框架各自为营的局面。Jpa是一套规范。

2、Spring Boot Jpa

Spring Boot Jpa 是 Spring 基于 ORM 框架、Jpa 规范的基础上封装的一套 Jpa 应用框架,可使开发者用极简的代码即可实现对数据的访问和操作。它提供了包括增删改查等在内的常用功能,且易于扩展。学习并使用 Spring Data Jpa 可以极大提高开发效率。

3、Jpa使用

基本查询,继承JpaRepository后有一些基本方法可以使用。重点理解ExampleMatcher
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()
                //修改默认匹配器为模糊查询
//                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                //模糊查询匹配开头,即{account}%
                .withMatcher("account", ExampleMatcher.GenericPropertyMatchers.startsWith())
                //全部模糊查询,即%{password}%
                .withMatcher("password" ,ExampleMatcher.GenericPropertyMatchers.contains())
                //忽略属性:是否关注。因为是基本类型,需要忽略掉
                .withIgnorePaths("id");  
Page<User> page = userRepository.findAll(example,pageable);
自定义简单查询

自定义的简单查询就是根据方法名来自动生成 SQL,主要的语法是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);
上一篇 下一篇

猜你喜欢

热点阅读