标签:持久化 enc 增删改查 mod order by append void 代码 mes
JPA:Java Persistence API 是java持久化规范。它为java开发人员提供了一种对象/映射工具来管理java应用中的关系数据。目的是为了简化持久化开发工作和整合ORM技术。
Spring Data JPA 是Spring基于ORM框架、JPA规范的基础上封装的一套JPA应用框架。可使得开发者用极简的代码即可实现对数据的访问和操作。
Spring Data JPA默认生成了一些基本的增删改查。
    List<T> findAll();
    List<T> findAll(Sort var1);
    List<T> findAll(Iterable<ID> var1);
    <S extends T> List<S> save(Iterable<S> var1);
    void flush();
    <S extends T> S saveAndFlush(S var1);
    void deleteInBatch(Iterable<T> var1);
    void deleteAllInBatch();
    T getOne(ID var1);
    <S extends T> List<S> findAll(Example<S> var1);
    <S extends T> List<S> findAll(Example<S> var1, Sort var2);
自定义操作:
具体的关键字,使用方法和生产成SQL如下表所示
| Keyword | Sample | JPQL snippet | 
|---|---|---|
| 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查询
其实Spring data 觉大部分的SQL都可以根据方法名定义的方式来实现,但是由于某些原因我们想使用自定义的SQL来查询,spring data也是完美支持的;在SQL的查询方法上面使用@Query注解,如涉及到删除和修改在需要加上@Modifying.也可以根据需要添加 @Transactional 对事物的支持,查询超时的设置等
@Modifying
@Query("update User u set u.userName = ?1 where u.id = ?2")
int modifyByIdAndUserId(String  userName, Long id);
	
@Transactional
@Modifying
@Query("delete from User where id = ?1")
void deleteByUserId(Long id);
  
@Transactional(timeout = 10)
@Query("select u from User u where u.emailAddress = ?1")
    User findByEmailAddress(String emailAddress);
微服务 第六章 springboot 通过Spring-data-jpa 配置Oracle数据源(Spring-data-jpa详细介绍)
标签:持久化 enc 增删改查 mod order by append void 代码 mes
原文地址:https://www.cnblogs.com/yaohuiqin/p/9395247.html