zoukankan      html  css  js  c++  java
  • spring mvc 的jpa JpaRepository数据层 访问方式汇总

    本文转载:http://perfy315.iteye.com/blog/1460226和http://jishiweili.iteye.com/blog/2088265

    AppleFramework在数据访问控制层采用了Spring Data作为这一层的解决方案,下面就对Spring Data相关知识作一个较为详细的描述。 
    1.Spring Data所解决的问题 
    Spring Data :提供了一整套数据访问层(DAO)的解决方案,致力于减少数据访问层(DAO)的开发量。它使用一个叫作Repository的接口类为基础,它被定义为访问底层数据模型的超级接口。而对于某种具体的数据访问操作,则在其子接口中定义。 
    public interface Repository<T, ID extends Serializable> { 

    所有继承这个接口的interface都被spring所管理,此接口作为标识接口,功能就是用来控制domain模型的。 
    Spring Data可以让我们只定义接口,只要遵循spring data的规范,就无需写实现类。 

    2.什么是Repository? 
    2.1 Repository(资源库):通过用来访问领域对象的一个类似集合的接口,在领域与数据映射层之间进行协调。这个叫法就类似于我们通常所说的DAO,在这里,我们就按照这一习惯把数据访问层叫Repository 
    Spring Data给我们提供几个Repository,基础的Repository提供了最基本的数据访问功能,其几个子接口则扩展了一些功能。它们的继承关系如下: 
    Repository: 仅仅是一个标识,表明任何继承它的均为仓库接口类,方便Spring自动扫描识别 
    CrudRepository: 继承Repository,实现了一组CRUD相关的方法 
    PagingAndSortingRepository: 继承CrudRepository,实现了一组分页排序相关的方法 
    JpaRepository: 继承PagingAndSortingRepository,实现一组JPA规范相关的方法 
    JpaSpecificationExecutor: 比较特殊,不属于Repository体系,实现一组JPA Criteria查询相关的方法 
    我们自己定义的XxxxRepository需要继承JpaRepository,这样我们的XxxxRepository接口就具备了通用的数据访问控制层的能力。 
    2.2 JpaRepository 所提供的基本功能 
    2.2.1 CrudRepository<T, ID extends Serializable>: 
    这个接口提供了最基本的对实体类的添删改查操作 
    T save(T entity);//保存单个实体 
            Iterable<T> save(Iterable<? extends T> entities);//保存集合 
            T findOne(ID id);//根据id查找实体 
            boolean exists(ID id);//根据id判断实体是否存在 
            Iterable<T> findAll();//查询所有实体,不用或慎用! 
            long count();//查询实体数量 
            void delete(ID id);//根据Id删除实体 
            void delete(T entity);//删除一个实体 
    void delete(Iterable<? extends T> entities);//删除一个实体的集合 
            void deleteAll();//删除所有实体,不用或慎用! 
    2.2.2 PagingAndSortingRepository<T, ID extends Serializable> 
    这个接口提供了分页与排序功能 
    Iterable<T> findAll(Sort sort);//排序 
            Page<T> findAll(Pageable pageable);//分页查询(含排序功能) 
    2.2.3 JpaRepository<T, ID extends Serializable> 
    这个接口提供了JPA的相关功能 
    List<T> findAll();//查找所有实体 
            List<T> findAll(Sort sort);//排序 查找所有实体 
            List<T> save(Iterable<? extends T> entities);//保存集合 
            void flush();//执行缓存与数据库同步 
            T saveAndFlush(T entity);//强制执行持久化 
    void deleteInBatch(Iterable<T> entities);//删除一个实体集合 
    3.Spring data 查询 
    3.1 简单条件查询:查询某一个实体类或者集合 
    按照Spring data 定义的规则,查询方法以find|read|get开头 
    涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性以首字母大写其余字母小写为规定。 
    例如:定义一个Entity实体类 
    class User{ 
    private String firstname; 
    private String lastname; 
    } 
    使用And条件连接时,应这样写: 
    findByLastnameAndFirstname(String lastname,String firstname); 
    条件的属性名称与个数要与参数的位置与个数一一对应 

    3.2 使用JPA NamedQueries (标准规范实现) 
    这种查询是标准的JPA规范所定义的,直接声明在Entity实体类上,调用时采用在接口中定义与命名查询对应的method,由Spring Data根据方法名自动完成命名查询的寻找。 
    (1)在Entity实体类上使用@NamedQuery注解直接声明命名查询。 
    @Entity 
    @NamedQuery(name = "User.findByEmailAddress", 
      query = "select u from User u where u.emailAddress = ?1") 
    public class User { 


    注:定义多个时使用下面的注解 
    @NamedQueries(value = { 
                    @NamedQuery(name = User.QUERY_FIND_BY_LOGIN, 
                                            query = "select u from User u where u." + User.PROP_LOGIN 
                                                    + " = :username"), 
            @NamedQuery(name = "getUsernamePasswordToken", 
                            query = "select new com.aceona.weibo.vo.TokenBO(u.username,u.password) from User u where u." + User.PROP_LOGIN 
                                + " = :username")}) 
    (2)在interface中定义与(1)对应的方法 
    public interface UserRepository extends JpaRepository<User, Long> { 

      List<User> findByLastname(String lastname); 

      User findByEmailAddress(String emailAddress); 

    3.3 使用@Query自定义查询(Spring Data提供的) 
    这种查询可以声明在Repository方法中,摆脱像命名查询那样的约束,将查询直接在相应的接口方法中声明,结构更为清晰,这是Spring data的特有实现。 
    例如: 
    public interface UserRepository extends JpaRepository<User, Long> { 

      @Query("select u from User u where u.emailAddress = ?1") 
      User findByEmailAddress(String emailAddress); 

    3.4 @Query与 @Modifying 执行更新操作 
    这两个annotation一起声明,可定义个性化更新操作,例如只涉及某些字段更新时最为常用,示例如下: 
    @Modifying 
    @Query("update User u set u.firstname = ?1 where u.lastname = ?2") 
    int setFixedFirstnameFor(String firstname, String lastname); 

    3.5 索引参数与命名参数 
    (1)索引参数如下所示,索引值从1开始,查询中 ”?X” 个数需要与方法定义的参数个数相一致,并且顺序也要一致 
    @Modifying 
    @Query("update User u set u.firstname = ?1 where u.lastname = ?2") 
    int setFixedFirstnameFor(String firstname, String lastname); 

    (2)命名参数(推荐使用这种方式) 
    可以定义好参数名,赋值时采用@Param("参数名"),而不用管顺序。如下所示: 
    public interface UserRepository extends JpaRepository<User, Long> { 

      @Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname") 
      User findByLastnameOrFirstname(@Param("lastname") String lastname, 
                                     @Param("firstname") String firstname); 


    4. Transactionality(事务) 
    4.1 操作单个对象的事务 
    Spring Data提供了默认的事务处理方式,即所有的查询均声明为只读事务,对于持久化,更新与删除对象声明为有事务。 
    参见org.springframework.data.jpa.repository.support.SimpleJpaRepository<T, ID> 
    @org.springframework.stereotype.Repository 
    @Transactional(readOnly = true) 
    public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepository<T, ID>, 
                    JpaSpecificationExecutor<T> { 
    …… 
    @Transactional 
            public void delete(ID id) { 

                    delete(findOne(id)); 
            } 
    …… 

    对于自定义的方法,如需改变spring data提供的事务默认方式,可以在方法上注解@Transactional声明 

    4.2 涉及多个Repository的事务处理 
    进行多个Repository操作时,也应该使它们在同一个事务中处理,按照分层架构的思想,这部分属于业务逻辑层,因此,需要在Service层实现对多个Repository的调用,并在相应的方法上声明事务。 
    例如: 
    @Service(“userManagement”) 
    class UserManagementImpl implements UserManagement { 

      private final UserRepository userRepository; 
      private final RoleRepository roleRepository; 

      @Autowired 
      public UserManagementImpl(UserRepository userRepository, 
        RoleRepository roleRepository) { 
        this.userRepository = userRepository; 
        this.roleRepository = roleRepository; 
      } 

      @Transactional 
      public void addRoleToAllUsers(String roleName) { 

        Role role = roleRepository.findByName(roleName); 

        for (User user : userRepository.readAll()) { 
          user.addRole(role); 
          userRepository.save(user); 
        } 


    5.关于DAO层的规范 
    5.1对于不需要写实现类的情况:定义XxxxRepository 接口并继承JpaRepository接口,如果Spring data所提供的默认接口方法不够用,可以使用@Query在其中定义个性化的接口方法。 
    5.2对于需要写实现类的情况:定义XxxxDao 接口并继承com.aceona.appleframework.persistent.data.GenericDao 
    书写XxxxDaoImpl实现类并继承com.aceona.appleframework.persistent.data.GenericJpaDao,同时实现XxxxDao接口中的方法 

    在Service层调用XxxxRepository接口与XxxxDao接口完成相应的业务逻辑 

    关于配置:

    spring.xml

    Java代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
    4.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  
    5.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"  
    6.     xsi:schemaLocation="  
    7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    9.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd  
    10.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd  
    11.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
    12.         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"  
    13.     default-lazy-init="true">  
    14.   
    15.     <description>Spring公共配置</description>  
    16.   
    17.     <context:component-scan base-package="com.scu.book.shop">  
    18.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
    19.         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />  
    20.     </context:component-scan>  
    21.   
    22.     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
    23.         <property name="dataSource" ref="dataSource" />  
    24.         <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />  
    25.         <property name="packagesToScan" value="com.scu.book.shop" />  
    26.         <property name="jpaProperties">  
    27.             <props>  
    28.                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
    29.                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
    30.                 <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>  
    31.                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>  
    32.                 <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>  
    33.             </props>  
    34.         </property>  
    35.     </bean>  
    36.   
    37.     <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />  
    38.   
    39.     <jpa:repositories base-package="com.scu.book.shop" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory" />  
    40.   
    41.     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
    42.         <property name="entityManagerFactory" ref="entityManagerFactory" />  
    43.     </bean>  
    44.   
    45.     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />  
    46.   
    47. <!--   
    48.     <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />  
    49.  -->  
    50.    
    51.     <context:property-placeholder ignore-unresolvable="true"  
    52.         location="classpath*:/property/jdbc.properties,  
    53.                   classpath*:/property/hibernate.properties,  
    54.                   classpath*:/property/image.properties" />  
    55.   
    56.     <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"  
    57.         destroy-method="close">  
    58.   
    59.         <property name="driverClassName" value="${jdbc.driver}" />  
    60.         <property name="url" value="${jdbc.url}" />  
    61.         <property name="username" value="${jdbc.username}" />  
    62.         <property name="password" value="${jdbc.password}" />  
    63.   
    64.         <property name="maxActive" value="${jdbc.pool.maxActive}" />  
    65.         <property name="maxIdle" value="${jdbc.pool.maxIdle}" />  
    66.         <property name="testWhileIdle" value="true" />  
    67.         <property name="testOnBorrow" value="true" />  
    68.         <property name="testOnReturn" value="false" />  
    69.         <property name="validationQuery" value="select 1" />  
    70.         <property name="removeAbandoned" value="true" />  
    71.   
    72.         <property name="timeBetweenEvictionRunsMillis" value="900000" />  
    73.         <property name="minEvictableIdleTimeMillis" value="1800000" />  
    74.     </bean>  
    75.   
    76. </beans>  

     web.xml:

    Java代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    6.   
    7.     <display-name>book shop</display-name>  
    8.   
    9.     <listener>  
    10.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    11.     </listener>  
    12.     <context-param>  
    13.         <param-name>contextConfigLocation</param-name>  
    14.         <param-value>classpath:spring/*.xml</param-value>  
    15.     </context-param>  
    16.   
    17.     <filter>  
    18.         <filter-name>encodingFilter</filter-name>  
    19.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    20.         <init-param>  
    21.             <param-name>encoding</param-name>  
    22.             <param-value>UTF-8</param-value>  
    23.         </init-param>  
    24.     </filter>  
    25.     <filter-mapping>  
    26.         <filter-name>encodingFilter</filter-name>  
    27.         <url-pattern>/*</url-pattern>  
    28.     </filter-mapping>  
    29.   
    30.     <servlet>  
    31.         <servlet-name>springServlet</servlet-name>  
    32.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    33.         <init-param>  
    34.             <param-name>contextConfigLocation</param-name>  
    35.             <param-value>classpath:mvc/*.xml</param-value>  
    36.         </init-param>  
    37.         <load-on-startup>1</load-on-startup>  
    38.     </servlet>  
    39.     <servlet-mapping>  
    40.         <servlet-name>springServlet</servlet-name>  
    41.         <url-pattern>/</url-pattern>  
    42.     </servlet-mapping>  
    43.   
    44.     <filter>  
    45.         <filter-name>openEntityManagerInViewFilter</filter-name>  
    46.         <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>  
    47.     </filter>  
    48.     <filter-mapping>  
    49.         <filter-name>openEntityManagerInViewFilter</filter-name>  
    50.         <url-pattern>/*</url-pattern>  
    51.     </filter-mapping>  
    52.   
    53. </web-app>  

    mvc.xml

    Java代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:context="http://www.springframework.org/schema/context"  
    5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
    6.     xsi:schemaLocation="http://www.springframework.org/schema/mvc   
    7.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
    8.         http://www.springframework.org/schema/beans   
    9.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    10.         http://www.springframework.org/schema/context   
    11.         http://www.springframework.org/schema/context/spring-context-3.2.xsd"  
    12.         default-lazy-init="true">  
    13.           
    14.     <context:component-scan base-package="com.scu.book.shop" use-default-filters="false">  
    15.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
    16.         <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
    17.     </context:component-scan>   
    18.       
    19.     <mvc:annotation-driven>  
    20.         <mvc:message-converters register-defaults="true">  
    21.             <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
    22.                 <constructor-arg value="UTF-8" />  
    23.             </bean>  
    24.             <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">    
    25.                 <property name="objectMapper">  
    26.                     <bean class="com.fasterxml.jackson.databind.ObjectMapper">    
    27.                         <property name="dateFormat">    
    28.                             <bean class="java.text.SimpleDateFormat">    
    29.                                 <constructor-arg value="yyyy-MM-dd HH:mm:ss" />     
    30.                             </bean>    
    31.                         </property>    
    32.                     </bean>   
    33.                 </property>    
    34.             </bean>  
    35.         </mvc:message-converters>  
    36.     </mvc:annotation-driven>  
    37.       
    38.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    39.         <property name="prefix" value="/WEB-INF/views/"/>  
    40.         <property name="suffix" value=".jsp"/>  
    41.     </bean>  
    42.       
    43.     <mvc:default-servlet-handler/>  
    44.     <mvc:view-controller path="/" view-name="redirect:/task/list"/>  
    45.       
    46. </beans>  

     这里只粘配置文件,其他地方参考源码。

  • 相关阅读:
    Selenium(三)webdriver的API与定位元素
    代码验证和动手动脑
    课程作业01实验报告1,2,3
    动手动脑
    课程作业03实验报告
    课程作业02实验报告
    猜数字实验报告
    java各种问题总结和一些讨论
    java从命令行接受多个数字并求和
    java登陆界面实验报告
  • 原文地址:https://www.cnblogs.com/wj0816/p/7150491.html
Copyright © 2011-2022 走看看