先来简单介绍下Spring-data-rest :
参考文档:https://docs.spring.io/spring-data/rest/docs/current/reference/html/
REST web services have become the number one means for application integration on the web. In
its core, REST defines that a system consists of resources that clients interact with. These resources
are implemented in a hypermedia driven way. Spring MVC offers a solid foundation to build theses
kinds of services. But implementing even the simplest tenet of REST web services for a multi-
domain object system can be quite tedious and result in a lot of boilerplate code.
Spring Data REST builds on top of Spring Data repositories and automatically exports those as REST
resources. It leverages hypermedia to allow clients to find functionality exposed by the repositories
and integrates these resources into related hypermedia based functionality automatically.
大致翻译如下:
REST web 服务已经成为了集成web应用的首选工具。REST的核心内容是:REST 定义了 一个由资源组成的系统,该资源就是客户端与之交互的资源。这些资源的实现方式是通过超媒体驱动实现的。Spring MVC 提供了固定的基本模型来创建这些服务。但是哪怕是为一个多领域对象系统实现最简单的一个REST web 服务都将会是一个及其繁琐的事情,并且会导致很多模板化的代码。Spring Data REST 构建在Spring data 库之上,并且自动导出这些接口作为REST 资源。它使用超媒体允许客户端找到由库所暴露出的资源,并且自动将这些资源以超媒体的形式集成起来。
说的再简单一点,使用spring-data-rest它帮我们省掉了很大一部分控制层的代码,因为满足它的规则的实体都会用它自己的控制层,并且和原有的Spring MVC 不会冲突。
一些基本的用法在此不在叙述,有需要了解的同学自行参考文档说明。
再来说一说Spring-data-jpa :
参考文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
说到使用spring-data-jpa 的目的,它的原话是这样的:
The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate
code required to implement data access layers for various persistence stores.
spring data 库抽象 的目标是针对各种各样的持久化存储,最大程度上减少用来实现数据库访问层的模板化代码。
现在我们不妨来做一下对比:有这样一个实体MyEntity,想要添加一条数据,我们用两种不同的方式去实现它
1.Spring MVC + Hibernate
基本流程:
<1>
@Entity
public class MyEntity {
UUID id;
String name;
}
<2>
public interface MyDao {
boolean persist(MyEntity entity);
MyEntity findById(UUID id);
}
<3>
public class MyDaoImpl {
private SessionFactory sessionFactory;
boolean persist(MyEntity entity){
this.sessionFactory.getCurrentSession().persist(entity);
}
MyEntity findById(UUID id){
return this.sessionFactory.getCurrentSession().get(MyEntity.class ,id);
}
}
<4>
public interface MyService {
boolean postEntity(MyEntity entity);
MyEntity findById(UUID id);
}
<5>
public class MyServiceImpl implements MyService {
private MyDaoImpl daoImpl;
public boolean postEntity(MyEntity entity) {
return daoImpl.persist(entity);
}
public MyEntity findById(UUID id){
return daoImpl.findById(id);
}
}
<6>
@Controller
public class MyController {
private MyService servie;
@RequestMapping(value = "/save.shtml", method = RequestMethod.POST)
public void doSave(MyEntity entity, HttpServletRequest request, HttpServletResponse response)
throws ParseException {
service.postEntity(entity);
}
@RequestMapping("/get.shtml")
public ModelAndView doGet(UUID id,HttpServletRequest req){
ModelAndView mv = new ModelAndView("/business/reminders/initial_duban");
mv.addObject("command", activitiParameter);
return mv;
}
}
针对以上情形我们已经习以为常,觉得应该就是这样。但是接着往下看:
2.data-jpa+data-rest
<1>
@Entity
public class MyEntity {
UUID id;
String name;
}
<2>
public interface MyEnityRepository extends CrudRepository {
MyEnity findById(UUID id);
}
get 操作的url: http://localost:8080/demo/api/myEntities/{id}
post操作的url:http://localost:8080/demo/api/myEntities?name="a"&id="a uuid"
同样的get和post 请求,但是代码量谁多谁少一目了然。当然这只是一个简单的例子,针对于简单的操作确实要比第一种方式省了很多力气。前提就是我们在service 层没有处理什么逻辑,但这也足以说明了Spring-data-jpa 和Spring-data-rest 的好处了。
下一篇我们来实现Spring-data-jpa 和Spring-data-rest 的动态条件查询,也就是说把类似于MyEntityRepository 里的findById()等方法通过传入的参数来构造查询条件,从而省去在每个Repository里编写自己的查询。