Spring Boot 学习第三步(Spring Data JPA学习汇总)
来源:互联网 发布:腾讯微信数据库多大 编辑:程序博客网 时间:2024/05/19 18:46
在前一篇博客中已经对于Spring Data JPA做了简单的描述,了解了一下Repository,CrudRepository等接口的作用和区别,并对于如何选择进行了简单的描述,那么本篇博客就深入JPA,了解一下如何使用。
Repository等接口方法解析
CrudRepository 接口
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { <S extends T> S save(S entity); T findOne(ID primaryKey); Iterable<T> findAll(); Long count(); void delete(T entity); boolean exists(ID primaryKey); // … more functionality omitted.}
PagingAndSortingRepository接口
public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> { Iterable<T> findAll(Sort sort); Page<T> findAll(Pageable pageable);}
从查询入手了解如何在持久层定义自己的方法
我们有两种方法可以指定查询语句:
(1)根据JPA所提供的命名规范,来命名自己的方法,那么Spring Data JPA就会根据它的解析规范来进行处理。
(2)在声明的方法上面使用@query 注解,并自己提供一个查询语句,那么Spring Data JPA在处理的时候就会按照我们提供的语句来进行处理。
Spring Data JPA的方法名解析规范
框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如 find、findBy、read、readBy、get、getBy,然后对剩下部分进行解析。并且如果方法的最后一个参数是 Sort 或者 Pageable 类型,也会提取相关的信息,以便按规则进行排序或者分页查询。
And — 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(String user, Striang pwd);
Or — 等价于 SQL 中的 or 关键字,比如 findByUsernameOrAddress(String user, String addr);
Between — 等价于 SQL 中的 between 关键字,比如 findBySalaryBetween(int max, int min);
LessThan — 等价于 SQL 中的 “<”,比如 findBySalaryLessThan(int max);
GreaterThan — 等价于 SQL 中的”>”,比如 findBySalaryGreaterThan(int min);
IsNull — 等价于 SQL 中的 “is null”,比如 findByUsernameIsNull();
IsNotNull — 等价于 SQL 中的 “is not null”,比如 findByUsernameIsNotNull();
NotNull — 与 IsNotNull 等价;
Like — 等价于 SQL 中的 “like”,比如 findByUsernameLike(String user);
NotLike — 等价于 SQL 中的 “not like”,比如 findByUsernameNotLike(String user);
OrderBy — 等价于 SQL 中的 “order by”,比如 findByUsernameOrderBySalaryAsc(String user);
Not — 等价于 SQL 中的 “! =”,比如 findByUsernameNot(String user);
In — 等价于 SQL 中的 “in”,比如 findByUsernameIn(Collection userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数;
NotIn — 等价于 SQL 中的 “not in”,比如 findByUsernameNotIn(Collection userList) ,方法的参数可以是 Collection 类型,也可以是数组或者不定长参数;
public interface PersonRepository extends Repository<User, Long> { List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname); // Enables the distinct flag for the query List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname); List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname); // Enabling ignoring case for an individual property List<Person> findByLastnameIgnoreCase(String lastname); // Enabling ignoring case for all suitable properties List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname); // Enabling static ORDER BY for a query List<Person> findByLastnameOrderByFirstnameAsc(String lastname); List<Person> findByLastnameOrderByFirstnameDesc(String lastname);}
使用@Query注解的方式来定义语句
这个就不进行详细描述了,给出几组例子就明白了
类型1:以 ?1 来标记需要输入参数的地方,然后在方法中给出参数。
public interface UserDao extends Repository<AccountInfo, Long> { @Query("select a from AccountInfo a where a.accountId = ?1") public AccountInfo findByAccountId(Long accountId); @Query("select a from AccountInfo a where a.balance > ?1") public Page<AccountInfo> findByBalanceGreaterThan( Integer balance,Pageable pageable); }
类型2:使用 :id 这种方式标记参数,在参数列表中使用@Parameter来呼应。
public interface UserDao extends Repository<AccountInfo, Long> { public AccountInfo save(AccountInfo accountInfo); @Query("from AccountInfo a where a.accountId = :id") public AccountInfo findByAccountId(@Param("id")Long accountId);
问题三:既然查询解决了,那么 删除和修改,增加怎么解决呢?
//使用 @Modifying 将查询标识为修改 @Modifying @Query("update AccountInfo a set a.salary = ?1 where a.salary < ?2") public int increaseSalary(int after, int before);
Spring Data JPA 对事务的支持 也是一个很重要的问题,下次讨论。
- Spring Boot 学习第三步(Spring Data JPA学习汇总)
- Spring Boot 学习第三步(Spring Data JPA学习汇总)
- 《spring-boot学习》-07-spring data jpa
- Spring Boot [组件学习-Spring Data JPA]
- spring data jpa 学习
- Spring Data JPA学习
- Spring Boot 学习第二步 配置MySQL数据库+JPA
- Spring Boot 学习第二步 配置MySQL数据库+JPA
- Spring Boot 学习第二步 配置MySQL数据库+JPA
- spring-boot-data-jpa
- spring boot + jpa学习笔记
- 学习Spring Data JPA 小结
- Spring Data JPA简单学习
- Spring Data JPA 入门学习
- Spring Data JPA学习一
- 跟着问题学习Spring Data Jpa----Spring Data Jpa配置
- Spring Boot+Spring Data JPA+Spring Cache
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 修改jsp、Java文件不用重启Tomcat服务的方法
- SpringJPA(Hibernate) 处理基于基类的关联关系
- Android进阶_WebView与JS进行交互
- zookeeper源码分析----节点创建流程
- CCClipingNode的使用
- Spring Boot 学习第三步(Spring Data JPA学习汇总)
- Minimum Depth of Binary Tree
- java去除字符串中的空格、回车、换行符、制表符
- selenium面试题
- Git 菜鸟变大神(二) 如何提交文件和历史版本恢复
- lcd驱动。。解决开发板小企鹅logo跑偏问题
- Resharper的使用
- 欢迎使用CSDN-markdown编辑器
- SecureCRT下如何解决vi背景色的问题