The interface provided by spring data JPA is also the core concept of spring data JPA: 1:repository: The topmost interface is an empty interface that is designed to unify all Repository types and to automatically recognize components when they are scanned. 2:crudrepository: is a repository sub-interface that provides CRUD functionality 3:pagingandsortingrepository: is a crudrepository sub-interface, Add pagination and Sorting features 4:jparepository: is the Pagingandsortingrepository sub-interface, added some useful functions, such as: batch operation. 5:jpaspecificationexecutor: Used to do query interface 6:specification: is a query specification provided by Spring Data JPA, to do complex queries, simply set the query criteria around this specification can be n Environment constructs a common Java project in eclipse, mainly to add a bunch of jar packages. 1: First download Spring data Common and spring data JPA packages to the website and add the Dist jar package to the project. Here is Spring-data-commons-1.5.0.release.jar and spring-data-jpa-1.3.2.release.jar2: Add Spring3.2.3 jar package to the project 3:JPA implementation is chosen Hibernate4 .2.0, a total of the following additional Jar:antlr-2.7.7.jar, Aopalliance-1.0.jar, Asm-3.2.jar, Aspectjrt-1.7.1.jar, Aspectjweaver-1.7.1.jar, Commons-beanutils-1.8.3.jar, Commons-codec-1.7.jar, Commons-collections-3.2.1.jar, Commons-dbcp-1.4.jar, Commons-fileupload-1.2.2.jar, Commons-io-2.4.jar, Commons-lang3-3.1.jar, Commons-logging-1.1.1.jar, Commons-pool-1.6.jar, Dom4j-1.6.1.jar, hibernate-commons-annotations-4.0.1.final.jar, Hibernate-core-4.2.0.final.jar, Hibernate-entitymanager-4.2.0.final.jar, Hibernate-jpa-2.0-api-1.0.1.final.jar, Javassist-3.15.0-ga.jar, Jboss-logging-3.1.0.ga.jar, Jboss-transaction-api _1.1_spec-1.0.0.final.jar, Mysql-connector-java-5.1.9.jar, slf4j-api-1.7.3.jar n entity object, is the previous implementation way
@Entity //Declaration entity @table (name= "Tbl_user") //corresponding table public class Usermodel {//define primary key @id private Integer uuid; Private String name;private Integer age;//omitted Getter/setter}
interface of the Ndao
Public interface Userrepository extends Jparepository<usermodel, integer>{//empty, can not write anything}
Without providing implementations, Spring Data JPA will take care of everything N write a service for the logic layer, which is actually equivalent to the DAO client, used to test
@Service @transactionalpublic class Client {@Autowiredprivate userrepository ur;public void Testadd (Usermodel um) { Ur.save (UM); } public static void Main (string[] args) {ApplicationContext ctx = new Classpathxmlapplicationcontext (" Applicationcontext.xml "); Client C = (client) Ctx.getbean ("Client"); Usermodel um = new Usermodel (); Um.setage (1); Um.setname ("Zhang San"); Um.setuuid (1); C.testadd (UM);} }
n also needs to be configured in the spring configuration file, similar to the configuration using annotations:
<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 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-3.0.xsdhttp://www.springframework.org/schema/ Context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/ SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/SPRING-AOP-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/SPRING-TX-3.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http:/ /www.springframework.org/schema/data/jpa/spring-jpa.xsd "><!--define the service layer code package storage path, the class that automatically find @service annotation under the package-->< Context:componenT-scan base-package= "com ..." ><context:exclude-filter type= "annotation" expression= " Org.springframework.stereotype.Controller "/></context:component-scan><aop:aspectj-autoproxy Proxy-target-class= "true"/><!--opening the annotation transaction is only valid for the current profile--><tx:annotation-driven transaction-manager= " TransactionManager "proxy-target-class=" true "/><!--define the Repository interface to store the directory--><!--define the suffix of the interface implementation, usually impl--> <!--define a reference to the Entity factory--><!--define the transaction manager reference--<jpa:repositories base-package= "Com......repository" repository-impl-postfix= "Impl" entity-manager-factory-ref= "Entitymanagerfactory" Transaction-ma nager-ref= "TransactionManager" > </jpa:repositories> <!--define entity factory bean--> <bean id= "Entitymanagerfa Ctory "class=" Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean "> <property name=". .... "ref=" ... "/> <property name=" ... "value=" ... "/> <property". "."Name= "" ... "> <bean class=" ... "/> </property> </bean><!--transaction Manager configuration--- <bean id= "TransactionManager" class= "Org.springframework.orm.jpa.JpaTransactionManager" > <property nam E= "Entitymanagerfactory" ref= "entitymanagerfactory"/> </bean> </beans>
After the configuration is complete, you can run the client test, of course, the database and tables need to be prepared or can be added under <jpa:repositories> filter, such as: <repositories base-package= " Com.acme.repositories "><context:exclude-filter type=" regex "expression=". *somerepository "/></ Repositories>
Spring Data JPA Configuration