Spring Data JPA Sample (IntelliJ maven project)

Source: Internet
Author: User

1. Create a new MAVEN project in IntelliJ

Give a good example, (the business logic component UserService is omitted in this example)

2. Configuring Dependencies in Pom.xml

includes: Spring-contextspring-ormspring-data-jpahibernate-corehibernate -entitymanagermysql-connector-javacommons-dbcpjunitspring-test

The code is as follows:

<dependencies> <!--Spring & Spring Data JPA--<dependency> <groupid>org.spring Framework</groupid> <artifactId>spring-context</artifactId> &LT;VERSION&GT;4.2.3.RELEASE&L        t;/version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.2.3.RELEASE</version> </dependency&    Gt <dependency> <groupId>org.springframework.data</groupId> &LT;ARTIFACTID&GT;SPRING-DATA-JPA     </artifactId> <version>1.9.1.RELEASE</version> </dependency> <!--Hibernate-- <dependency> <groupId>org.hibernate</groupId> <artifactid>hibernate-core</art ifactid> <version>4.3.11.Final</version> </dependency> <dependency> <gro Upid>org.hiberNate</groupid> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.11.fina L</version> </dependency> <!--MySQL--<dependency> <groupid>mysql</gro    Upid> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactid>comm Ons-dbcp</artifactid> <version>1.4</version> </dependency> <!--Junit-to-&L T;dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> < version>4.12</version> </dependency> <dependency> <groupId>org.springframework<    /groupid> <artifactId>spring-test</artifactId> <version>4.2.3.RELEASE</version> </dependency></dependencies> 

3. Resources Right-click New an XML Configuration file--spring Config profile: Spring-config.xml(or Applicationcontext.xml)

Configure DataSource, Entitymanagerfactory, TransactionManager, and more.

<?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:jpa= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA" xmlns : tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http ://www.springframework.org/schema/data/jpa/spring-jpa.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/http Www.springframework.org/schema/tx/spring-tx.xsd "> <jpa:repositories base-package=" com.xin "/> <bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <property name= "Driverc Lassname "value=" Com.mysql.jdbc.Driver "/> <property name=" url "value=" Jdbc:mysql://localhost:3306/javaee "/&G        T <property name= "username" value= "root"/> <p roperty name= "password" value= "root"/> </bean> <bean id= "entitymanagerfactory" class= "Org.springframew        Ork.orm.jpa.LocalContainerEntityManagerFactoryBean "> <property name=" dataSource "ref=" DataSource "/> <property name= "Packagestoscan" value= "Com.xin"/> <property name= "Jpavendoradapter" > <be An class= "Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" > <property name= "Database" va Lue= "MYSQL"/> <property name= "Generateddl" value= "true"/> <property name= "showsq L "value=" true "/> <property name=" databaseplatform "value=" Org.hibernate.dialect.MySQL5InnoDBDialect " /> </bean> </property> <property name= "Jpaproperties" > <value&                Gt Hibernate.dialect=org.hibernate.dialect.mysql5innodbdialect hibernate.hbm2ddl.auto=update Hi BernaTe.show_sql=true <!--The settings here can override settings in Jpavendoradapter-<!--hibernate.format_sql=true--> </value> </property> </bean> <!--Configuring a local transaction manager for JPA--<bean id= "Transacti Onmanager "class=" Org.springframework.orm.jpa.JpaTransactionManager "> <property name=" entitymanagerfactory " ref= "Entitymanagerfactory"/> </bean> <!--generate transaction agent based on transaction annotations--<tx:annotation-driven Transaction-man Ager= "TransactionManager"/></beans>

4. Create a new user entity class and make annotation annotations

@Entitypublic class User {    @Id    @GeneratedValue (strategy = generationtype.auto)    private Long Id;    Private String firstname, LastName;    private int age;    Private String address;//ellipsis construction method and setter, getter method}

5. New DAO Component: Userrepository interface

Let this interface inherit repository, Crudrepository, pagingandsortingrepositories, or jparepository. The Jparepository interface has an implementation class simplejparepository, and the methods described above are implemented by this class.

Public interface Userrepository extends Jparepository<user, long> {    //You can make a derived query based on the method name, or you can customize the query.    @Query ("Select u from User u where u.lastname=?1")    List<user> Findbylastname (String lastname);        Long Countbylastname (String lastname);}

6. the junit unit test class can then be used to test the

@RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations = "Classpath:spring-config.xml") public Class Userrepositorytest {    @Autowired    private userrepository userrepository;    @Test public    void Testfindbylastname () throws Exception {        User user = new User ("Lunhui", "Wei", "Nanle");        Userrepository.save (user);}    }

Spring Data JPA Sample (IntelliJ maven project)

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.