005-spring-data-elasticsearch 3.0.0.0使用【三】-spring-data之Spring数据扩展

1.8、Spring数据扩展

  这些扩展使Spring Data在各种环境下的使用成为可能。目前大部分的整合都是针对Spring MVC。

1.8.1、Querydsl扩展

  Querydsl是一个框架,它可以通过流畅的API构建静态类型的SQL查询。

  几个Spring Data模块通过QueryDslPredicateExecutor提供与Querydsl的集成。

示例、QueryDslPredicateExecutor接口

public interface QueryDslPredicateExecutor<T> {
//查找并返回与Predicate匹配的单个实体
  Optional<T> findById(Predicate predicate);  
//查找并返回与谓词匹配的所有实体
  Iterable<T> findAll(Predicate predicate);   
//返回匹配Predicate的实体的数量
  long count(Predicate predicate);            
//判断返回与Predicate匹配的实体是否存在
  boolean exists(Predicate predicate);        

  // … more functionality omitted.
}
View Code

要使用Querydsl支持,只需在存储库接口上扩展QueryDslPredicateExecutor即可。

interface UserRepository extends CrudRepository<User, Long>, QueryDslPredicateExecutor<User> {
}

以上使用Querydsl谓词可以编写类型安全查询。

Predicate predicate = user.firstname.equalsIgnoreCase("dave").and(user.lastname.startsWithIgnoreCase("mathews"));

userRepository.findAll(predicate);

1.8.2、web支持

  如果模块支持存储库编程模型,则Spring Data模块附带各种Web支持。与Web相关的东西需要类路径上的Spring MVC JAR,其中一些甚至提供了与Spring HATEOAS的集成。通常,通过在JavaConfig配置类中使用@EnableSpringDataWebSupport注释来启用集成支持。

  启用web支持  

@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
class WebConfiguration {}

  或者xml配置

<bean class="org.springframework.data.web.config.SpringDataWebConfiguration" />

<!-- If you're using Spring HATEOAS as well register this one *instead* of the former -->
<bean class="org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration" />
View Code

基本的web支持

  上面显示的配置设置将注册一些基本组件:

    DomainClassConverter使Spring MVC能够根据请求参数或路径变量来解析存储库管理的域类的实例。

    HandlerMethodArgumentResolver实现,以便Spring MVC根据请求参数解析Pageable和Sort实例。

DomainClassConverter

  DomainClassConverter允许您直接在Spring MVC控制器方法签名中使用域类型,因此您不必通过存储库手动查找实例:

@Controller
@RequestMapping("/users")
class UserController {

  @RequestMapping("/{id}")
  String showUserForm(@PathVariable("id") User user, Model model) {

    model.addAttribute("user", user);
    return "userForm";
  }
}
View Code

  正如你所看到的,该方法直接接收一个用户实例,不需要进一步查找。通过让Spring MVC首先将路径变量转换为域类的id类型并最终通过在为域类型注册的存储库实例上调用findById(...)来访问实例,可以解决该实例。

  目前,存储库必须实施CrudRepository才有资格被发现用于转换。

HandlerMethodArgumentResolvers for Pageable and Sort

  上面的配置片段还注册了PageableHandlerMethodArgumentResolver以及SortHandlerMethodArgumentResolver的一个实例。注册使页面和排序成为有效的控制器方法参数  

@Controller
@RequestMapping("/users")
class UserController {

  private final UserRepository repository;

  UserController(UserRepository repository) {
    this.repository = repository;
  }

  @RequestMapping
  String showUsers(Model model, Pageable pageable) {

    model.addAttribute("users", repository.findAll(pageable));
    return "users";
  }
}
View Code

  此方法签名将导致Spring MVC尝试使用以下默认配置从请求参数派生Pageable实例:

表1.对Pageable实例评估的请求参数

page

您想要检索的页面,索引为0,默认为0。

size

要检索的页面大小,默认为20。

sort

属性应该按格式属性property(,ASC | DESC)排序。默认排序方向是升序。如果您想切换路线,请使用多个排序参数,例如 ?sort=firstname&sort=lastname,asc.

更多spring扩展以及支持web支持请查看

 

posted @ 2018-03-03 15:52  bjlhx15  阅读(495)  评论(0编辑  收藏  举报
Copyright ©2011~2020 JD-李宏旭