Getting Started with spring data

Source: Internet
Author: User
Tags aop uuid jboss neo4j
What is spring data? Spring data is an open source framework for simplifying database access and supporting cloud services. Its primary goal is to make access to data easy and efficient, and to support map-reduce frameworks and cloud computing data Services. Spring Data contains a number of subprojects: Commons-Provides a shared infrastructure for use by individual subprojects, support for persistent JPA across databases-simplifies creating JPA data access layers and persistent layer functionality across storage Hadoop-based on Spring's Hadoop job Configuration and a MapReduce job key-value  for a POJO programming model-integrates Redis and Riak, provides a simple package document in several common scenarios-integrated documentation database: CouchDB and MongoDB and provides base This configuration mapping and database support graph-Integrated neo4j provides a powerful POJO programming model Graph Roo Addon-roo support for neo4j JDBC Extensions-Supports Oracle RAD, high Level queues and advanced data types Mapping-Grails provides object mapping frameworks that support different database examples-sample programs, documents, and Diagram databases guidance-advanced document Spring Data JPA is what spring provides for a To simplify the JPA development Framework nspring data JPA can greatly simplify JPA's writing, and can achieve access to and operation of data with little written implementation. In addition to crud, there are some common features such as paging, sorting, and so on. What does spring data JPA look at the interfaces provided by spring data JPA and the core concept of spring data JPA: 1:repository: The topmost interface, an empty interface, The aim is to unify all types of repository 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 that adds paging and sorting functions 4:jparepository: Is the Pagingandsortingrepository interface, added some useful functions, such as: bulk operation. 5:jpAspecificationexecutor: The interface used to do the query 6:specification: is a query specification provided by Spring Data JPA, to do complex queries, simply set query conditions around this specification   HelloWorld n Environments build an ordinary Java project in eclipse, mainly to add a bunch of jar packages. 1: First go to the website to download the spring data Common and Spring Data JPA package, the inside Dist jar package into the project, This is Spring-data-commons-1.5.0.release.jar and Spring-data-jpa-1.3.2.release.jar 2: Add the Spring3.2.3 jar package to the project The implementation of 3:JPA is Hibernate4.2.0, and a total of additional jar:antlr-2.7.7.jar, Aopalliance-1.0.jar, Asm-3.2.jar, Aspectjrt-1.7.1.jar are required , 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, which is the previous implementation @Entity @Table (name= "Tbl_user") public class Usermodel {@Id private Integer uuid; private String Name Private Integer age; Omit Getter/setter} Ndao interface public interface Userrepository extends Jparepository<usermodel, integer>{//Empty, You can write nothing without having to provide implementations, Spring Data JPA will fix everything for us. The Service of writing a logical layer is actually equivalent to the DAO client, which is used to test @Service @Transactional public class Client { @Autowired private userrepository ur;   public void Testadd (Usermodel um) {ur.save (UM):}   public static void main (string[] args) {Applicationcontex T ctx = new Classpathxmlapplicationcontext ("Applicationcontext.xml");   Client C = (client) Ctx.getbean ("Client"); Usermodel um = new Usermodel (); Um.setage (1); Um.setname ("John"); Um.setuuid (1);   C.testadd (UM); } n also needs to be configured in the spring configuration file, basically 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.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/ Spring-jpa.xsd "> <context:component-scan base-package=" Cn.javass "> <context:exclude-filter type=" Annotation "expression=" Org.springframework.stereotype.Controller "/> </context:component-scan> <aop: Aspectj-autoproxy proxy-target-class= "true"/&Gt <!--open annotation transactions are valid only for the current configuration file--> <tx:annotation-driven transaction-manager= "TransactionManager" Proxy-target-class= "true"/>     <jpa:repositories              base-package= "Cn.javass"              repository-impl-postfix= "Impl"             entity-manager-factory-ref= "Entitymanagerfactory"              transaction-manager-ref= "TransactionManager" >     </jpa:repositories>           <bean id= "Entitymanagerfactory"            class= "Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >          <property name= "DataSource" ref= "DataSource"/>          &Lt;property name= "Packagestoscan" value= "Cn.javass"/>         <property Name= "Persistenceprovider" >             <bean class= "Org.hibernate.ejb.HibernatePersistence"/>         </property>          <property name= "Jpavendoradapter" >              <bean class= " Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter ">                  <property name= "Generateddl" value= "false"/>                  <property name= "Database" value = "MYSQL"/>                 < Property Name= "Databaseplatform" value= "org.hibernate.dialEct. Mysql5innodbdialect "/>                  <property name= "Showsql" value= "true"/>              </bean>         </property>          <property name= "Jpadialect" >             <bean class= "Org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>         </property>         <property name= "Jpapropertymap" >              <map>                  <entry key= "Hibernate.query.substitutions" value= "true 1, False 0 "/>                <entry key= "hibernate.default_batch_fetch_size" value= "/>"                  <entry key= "hibernate.max_fetch_depth" value= "2"/>                  <entry key= " Hibernate.generate_statistics "value=" true "/>                  <entry key= "Hibernate.bytecode.use_reflection_optimizer" value= "true"/>                 <entry key= " Hibernate.cache.use_second_level_cache "value=" false "/>                  <entry key= "Hibernate.cache.use_query_cache" value= "false"/>             </map>          &LT;/PROPERTY&GT     </bean> <!--transaction Manager configuration-->     <bean id= "TransactionManager" class= " Org.springframework.orm.jpa.JpaTransactionManager ">         <property Name= "Entitymanagerfactory" ref= "entitymanagerfactory"/>     </bean>   <bean name= " DataSource "class=" Org.apache.commons.dbcp.BasicDataSource "> <property name=" driverclassname "><value" >org.gjt.mm.mysql.Driver</value></property> <property name= "url" ><value>jdbc:mysql:// Localhost:3306/cc?useunicode=true&amp;characterencoding=utf-8</value></property> <property Name= "username" > <value>root</value> </property> <property name= "password" value= "cc"/> </bean> </beans>   Configuration is complete, you can run the client test, of course, the database and tables need to be ready to add filter under <jpa:repositories>, Form: <repositories base-package= "Com.acme.repositories" > <context:exclude-filter type= "reGex "expression=". *somerepository "/> </repositories>     Chapter II: Basic Functions of JparepositoryJparepository's basic function demonstrates the specific look code demo: The implementation class of the Pageable interface is the Pagerequest,page interface implementation class is Pageimpl.  Examples are as follows: page<usermodel> p = ur.findall (new pagerequest (0,2,new Sort) (Direction. DESC, "uuid")));  System. Out.println ("list=" +p.getcontent ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.