使用jpa的步骤

来源:互联网 发布:mac mini 更新系统 编辑:程序博客网 时间:2024/04/28 19:55

在 Spring Data JPA下进行数据存储非常简单

1

Declare an interface extending Repository or one of its subinterfaces and type it to the domain class and ID type that it will handle.


interface PersonRepository extends Repository<Person, Long> { … }
声明一个接口继承自Repository或其子接口,制定Domain Class 和 ID 类型


2

interface PersonRepository extends Repository<Person, Long> {
  List<Person> findByLastname(String lastname);
}

在接口中声明查询方法


3

Set up Spring to create proxy instances for those interfaces. Either via JavaConfig:

初始化spring 为接口创建代理对象(通过java类的形式或者XML的形式)


import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
class Config {}
or via XML configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/data/jpa
     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

   <jpa:repositories base-package="com.acme.repositories"/>

</beans>

4

获得注入的Repository对象,并使用

Get the repository instance injected and use it.

public class SomeClient {

  @Autowired
  private PersonRepository repository;

  public void doSomething() {
    List<Person> persons = repository.findByLastname("Matthews");
  }
}

/////////////////////以上就是我们相识用spring data jpa 的四个步骤

以下是一些知识点

持久层接口继承 Repository 并不是唯一选择。Repository 接口是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法。与继承 Repository 等价的一种方式,就是在持久层接口上使用 @RepositoryDefinition 注解,并为其指定 domainClass 和 idClass 属性。如下两种方式是完全等价的:
清单 15. 两种等价的继承接口方式示例
 public interface UserDao extends Repository<AccountInfo, Long> { …… }

 @RepositoryDefinition(domainClass = AccountInfo.class, idClass = Long.class)
 public interface UserDao { …… }


不翻译了应该都可以看懂

Extending CrudRepository exposes a complete set of methods to manipulate your entities. If you prefer to be selective about the methods being exposed, simply copy the ones you want to expose from CrudRepository into your domain repository.

@NoRepositoryBean
interface MyBaseRepository<T, ID extends Serializable> extends Repository<T, ID> {

  T findOne(ID id);

  T save(T entity);
}

interface UserRepository extends MyBaseRepository<User, Long> {
  User findByEmailAddress(EmailAddress emailAddress);
}


1.2.2定义查询方法


SpringData通过方法名有两种方式去解析出用户的查询意图:一种是直接通过方法的命名规则去解析,第二种是通过Query去解析,那么当同时存在几种方式时,SpringData怎么去选择这两

种方式呢?好了,SpringData有一个策略去决定到底使用哪种方式

查询策略:接下来我们将介绍策略的信息,你可以通过配置<repository>query-lookup-strategy属性来决定。

CREATE

通过解析方法名字来创建查询。这个策略是删除方法中固定的前缀,然后再来解析其余的部分。

USE_DECLARED_QUERY

它会根据已经定义好的语句去查询,如果找不到,则会抛出异常信息。这个语句可以在某个注解或者方法上定义。根据给定的规范来查找可用选项,如果在方法被调用时没有找到定义的查询,那么会抛出异常。

CREATE_IF_NOT_FOUND(默认)这个策略结合了以上两个策略。他会优先查询是否有定义好的查询语句,如果没有,就根据方法的名字去构建查询。这是一个默认策略,如果不特别指定其他策略,那么这个策略会在项目

中沿用。



1.通过解析方法名创建查询

框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如 find、findBy、read、readBy、get、getBy,然后对剩下部分进行解析。并且如果方法的最后一个参数是 Sort 或者 Pageable 类型,也会提取相关的信息,以便按规则进行排序或者分页查询。

在创建查询时,我们通过在方法名中使用属性名称来表达,比如 findByUserAddressZip ()。框架在解析该方法时,首先剔除 findBy,然后对剩下的属性进行解析,详细规则如下(此处假设该方法针对的域对象为 AccountInfo 类型):

1   先判断 userAddressZip (根据 POJO 规范,首字母变为小写,下同)是否为 AccountInfo 的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,继续第二步;
2   从右往左截取第一个大写字母开头的字符串(此处为 Zip),然后检查剩下的字符串是否为 AccountInfo 的一个属性,如果是,则表示根据该属性进行查询;如果没有该属性,则重复第二步,继续从右往左截取;最后假设 user 为 AccountInfo 的一个属性;
3   接着处理剩下部分( AddressZip ),先判断 user 所对应的类型是否有 addressZip 属性,如果有,则表示该方法最终是根据 "AccountInfo.user.addressZip" 的取值进行查询;否则继续按照步骤 2 的规则从右往左截取,最终表示根据 "AccountInfo.user.address.zip" 的值进行查询。

一下自己揣摩联系


根据方法名解析的查询结果跟数据库是相关,但是,还有几个问题需要注意:

多个属性的查询可以通过连接操作来完成,例如And,Or。当然还有其他的,例如Between,LessThan,GreaterThan,Like。这些操作时跟数据库相关的,当然你还需要看看相关的数据库文档是否支持这些操作。
你可以使用
IngoreCase来忽略被标记的属性的大小写,也可以使用AllIgnoreCase来忽略全部的属性,当然这个也是需要数据库支持才允许的。你可以使用OrderBy来进行排序查询,排序的方向是AscDesc,如果需要动态排序,请看后面的章节。








0 0
原创粉丝点击