Spring Data JPA的两种等价的写法:
第一种:继承Respository类
第二种:利用注解
public interface UserDao extends Repository<AccountInfo, Long> { …… } @RepositoryDefinition(domainClass = AccountInfo.class, idClass = Long.class) public interface UserDao { …… }
两种方式都可以实现Spring Data JPA持久化以下运用Spring Data JPA实现简单的案例第一步:建立Maven项目,引入spring,hibernate,spring-data-jpa相关的依赖jar包第二部:配置web.xml,用于监听spring配置文件 applicationContext.xml,web.xml添加以下内容:<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>第三部:根目录下配置spring配置文件applicationContext.xml<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd"> <context:component-scan base-package="org.com.xd"/> <tx:annotation-driven transaction-manager="transactionManager"/> <jpa:repositories base-package="org.com.xd.dao" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="generateDdl" value="false"/> <property name="showSql" value="true"/> </bean> </property> </bean> </beans>第四步:配置Spring Data JPA的必须的配置文件persistence.xml. --注:persistence.xml文件必须定义在classpath路径下的META-INF文件夹中。内容如下:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="SimplePU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- <class>org.com.xd.vo.UserInfo</class> <class>org.com.xd.vo.AccountInfo</class> --> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/world"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value="root"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.use_sql_comments" value="false"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
第五步:配置vo,service,dao层的相关接口以及实现类
(1)利用hibernate实现实体类与表的映射。
(2)定义接口层service,相关的方法的定义
(3)定义接口层dao,继承Repository<AccountInfo, Long>类,定义相关方法
(4)定义实现类serviceImpl,调用dao层方法持久化数据,进行增删改查。
以上步骤可实现运用Spring Data JPA的操作。简化了企业开发中针对持久化数据过程繁琐的逻辑处理。提高开发效率,简化代码,方便管理。