• 一步步学习 Spring Data 系列之JPA(一)


    引入: 

    Spring Data是SpringSource基金会下的一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得数据库的访问变得方便快捷,并支持map-reduce框架和云计算数据服务。对于拥有海量数据的项目,可以用Spring Data来简化项目的开发。 

    然而针对不同的数据储存访问使用相对的类库来操作访问。Spring Data中已经为我们提供了很多业务中常用的一些接口和实现类来帮我们快速构建项目,比如分页、排序、DAO一些常用的操作。 

    今天主要是对Spring Data下的JPA模块进行讲解。 

    为什么说Spring Data能帮助我们快速构建项目呢,因为Spring Data已经在数据库访问层上帮我们实现了公用功能了,而我们只需写一个接口去继承Spring Data提供给我们接口,便可实现对数据库的访问及操作,类似于spring-orm的TemplateDAO。 

    ----------------------------------------------邪恶的分割线------------------------------------------------------------------------------ 
    核心接口: 

    下面来看一下Repository的最顶层接口: 

    Java代码  收藏代码
    1. public interface Repository<T, ID extends Serializable> {  
    2.   
    3. }  


    这个接口只是一个空的接口,目的是为了统一所有Repository的类型,其接口类型使用了泛型,泛型参数中T代表实体类型,ID则是实体中id的类型。 

    再来看一下Repository的直接子接口CrudRepository中的方法: 

    Java代码  收藏代码
    1. public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {  
    2.   
    3.     <S extends T> S save(S entity);  
    4.   
    5.     <S extends T> Iterable<S> save(Iterable<S> entities);  
    6.   
    7.     T findOne(ID id);  
    8.   
    9.     boolean exists(ID id);  
    10.   
    11.     Iterable<T> findAll();  
    12.   
    13.     Iterable<T> findAll(Iterable<ID> ids);  
    14.   
    15.     long count();  
    16.   
    17.     void delete(ID id);  
    18.   
    19.     void delete(T entity);  
    20.   
    21.     void delete(Iterable<? extends T> entities);  
    22.   
    23.     void deleteAll();  
    24. }  


    此接口中的方法大多是我们在访问数据库中常用的一些方法,如果我们要写自己的DAO类的时候,只需定义个接口来集成它便可使用了。 

    再来看看Spring Data未我们提供分页和排序的Repository的接口PagingAndSortingRepository: 

    Java代码  收藏代码
    1. public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {  
    2.   
    3.     Iterable<T> findAll(Sort sort);  
    4.   
    5.     Page<T> findAll(Pageable pageable);  
    6. }  


    这些Repository都是spring-data-commons提供给我们的核心接口,spring-data-commons是Spring Data的核心包。这个接口中为我们提供了数据的分页方法,以及排序方法。看吧,spring-data让我们省了很多心了,一切都按照这个规范进行构造,就连业务系统中常用到的一些操作都为我们考虑到了,而我们只需更用心的去关注业务逻辑层。spring-data将repository的颗粒度划得很细,其实我觉得spring的框架中将每个类的颗粒度都划得很细,这主要也是为了责任分离。 


    ----------------------------------------------邪恶的分割线------------------------------------------------------------------------------ 
    JPA实现: 
    针对spring-data-jpa又提供了一系列repository接口,其中有JpaRepository和JpaSpecificationExecutor,这2个接口又有什么区别呢,我们分别来看看这2个接口的源码。 

    JpaRepository.class 

    Java代码  收藏代码
    1. public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {  
    2.   
    3.     List<T> findAll();  
    4.   
    5.     List<T> findAll(Sort sort);  
    6.   
    7.     <S extends T> List<S> save(Iterable<S> entities);  
    8.     void flush();  
    9.   
    10.     T saveAndFlush(T entity);  
    11.   
    12.     void deleteInBatch(Iterable<T> entities);  
    13.   
    14.     void deleteAllInBatch();  


    这个类继承自PagingAndSortingRepository,看其中的方法,可以看出里面的方法都是一些简单的操作,并未涉及到复杂的逻辑。当你在处理一些简单的数据逻辑时,便可继承此接口,看一个小例子吧。本文JPA供应者选择的是Hibernate EntityManager,当然读者们也可以选择其他的JPA供应者,比如EclipseLink、OpenJPA,反正JPA是个标准,在无须修改的情况下便可移植。 

    先定义一用户实体类User.class: 

    Java代码  收藏代码
    1. @Entity  
    2. @Table( name = "spring_data_user" )  
    3. @PrimaryKeyJoinColumn( name = "id" )  
    4. public class User extends IdGenerator{  
    5.   
    6.     private static final long serialVersionUID = 1L;  
    7.       
    8.     private String name;  
    9.     private String username;  
    10.     private String password;  
    11.     private String sex;  
    12.     private Date birth;  
    13.     private String address;  
    14.     private String zip;  
    15.           
    16.         //省略getter和setter  
    17. }  


    Id生成策略是采用的表生成策略,这里就不贴代码了,spring的配置文件我也就不贴出来了,反正就那些东西,网上一查,遍地都是。后续我会在将demo附上来。 

    实体类是有了,现在得写一个持久层,这样才能操作数据库啊,现在我们来看一下持久层。IUserDao.class: 

    Java代码  收藏代码
    1. @Repository("userDao")  
    2. public interface IUserDao extends JpaRepository<User, Long>{}  


    再在spring的配置文件中加上以下代码。 

    Xml代码  收藏代码
    1. <beans xmlns="http://www.springframework.org/schema/beans"  
    2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    3.     xmlns:jpa="http://www.springframework.org/schema/data/jpa"  
    4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    5.     http://www.springframework.org/schema/data/jpa  
    6.     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">  
    7.   
    8.     <jpa:repositories base-package="org.tea.springdata.**.dao" />  
    9. </beans>  


    加上这段后Spring就会将指定包中@Repository的类注册为bean,将bean托管给Spring。这样定义完了就OK了!哦,就这样就可以操作数据库了? 
    是的,前面我就已经说了,Spring data已经帮我们写好一个实现类了,而简单的操作我们只须这样继承JpaRepository就可以做CRUD操作了。再写个业务类来测试一把吧。由于我用的Cglib来动态代理,所以就不定义接口了,直接定义类UserService.class: 

    Java代码  收藏代码
    1. @Service("userService")  
    2. public class UserService {  
    3.       
    4.     @Autowired  
    5.     private IUserDao dao;  
    6.       
    7.     public void save(User user) {  
    8.         dao.save(user);  
    9.     }  
    10.   
    11.     public void delete(Long id) {  
    12.         dao.delete(id);  
    13.     }  
    14.   
    15.     public void update(User user) {  
    16.         dao.saveAndFlush(user);  
    17.     }  
    18.   
    19.     public List<User> findAll() {  
    20.         return dao.findAll();  
    21.     }  
    22. }  


    来写一单元测试。 

    Java代码  收藏代码
    1. public class UserServiceTest {  
    2.       
    3.     private static ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
    4.       
    5.     private static UserService userService = (UserService) context.getBean("userService");  
    6.       
    7.     public void saveUser() {  
    8.         StopWatch sw = new StopWatch(getClass().getSimpleName());  
    9.         sw.start("Add a user information.");  
    10.         User u = new User();  
    11.         u.setName("John");  
    12.         u.setSex("Man");  
    13.         u.setUsername("JohnZhang");  
    14.         u.setPassword("123456");  
    15.         u.setBirth(new Date());  
    16.         userService.save(u);  
    17.         sw.stop();  
    18.         System.err.println(sw.prettyPrint());  
    19.     }  
    20.   
    21.      public static void main(String[] args) {  
    22.         UserServiceTest test = new UserServiceTest();  
    23.         test.saveUser();  
    24.     }  
    25. }  


    绿了,高兴了,测试通过! 
    额,都没用Junit怎么会绿呢,开个玩笑。 
    其余继承下来的操作方法,大家都可以自己测试一下,如没意外,应该都会测试通过。 

    好吧,今天就暂时分享到这了,你千万别以为Spring-data就这么点功能,这只是spring-data中最入门级的知识,后续还有很多东西值得学习,在下一篇文章中我会列出spring-data-jpa中精华的部分,对其进行讲解,并附上Demo供下载。 

  • 相关阅读:
    Eclipse 代码模板
    Eclipse 安装插件
    Eclipse 任务管理
    Eclipse 添加书签
    Eclipse 重构菜单
    Eclipse 浏览(Navigate)菜单浏览 Eclipse 工作空间
    Eclipse 查找
    Eclipse 悬浮提示
    Eclipse 快速修复
    Eclipse 内容辅助
  • 原文地址:https://www.cnblogs.com/simazilin/p/5645947.html
Copyright © 2020-2023  润新知