springDataJPA entry

A: JPA

    1.1 jpa is a development specification is the development of a normative framework for ORM is the Chinese name of the company SUN defined: JAVA Persistence API

orm: object-relational mapping is a problem-solving ideas operations on the object is the operation of the database
Two : What is SpringDataJPA
Spring Data JPA framework is aimed primarily Spring is not the only simplify the business logic code developers even only achieve lasting work-tier business logic are saved, the only thing to do is born out of the persistence layer interfaces are handed over to other Spring Data JPA to help complete
JPA specification is not based on native api had jpa the package again
If you are using SpringDataJpa also need to use Hibernate
Three: entry procedures
      1 Demand
Insert a row into customers
If you use Jpa framework can be built without first table you can use the framework to generate tables
  
<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.hibernate.version>5.0.7.Final</project.hibernate.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </ dependency> 
        <-! jpa's support for Hibernate ->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${project.hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>${project.hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
    </dependencies>
b key to create a configuration file
Configuration file path must be: META-INF / persistence.xml
Configure configuration database connection
<? xml Version = " 1.0 " encoding = " UTF-8 " ?> 
<Persistence xmlns = " http://java.sun.com/xml/ns/persistence " Version = " 2.0 " > 
    <-! Configuration lasting unit in the configuration file with at least a 
       name persistence unit name 
       Transaction - type object type 
       things RESOURCE_LOCAL single database 
       object JTA distributed transaction data across multiple databases things


     -> 
    <persistence-unit name = " myjpa " = of the type-Transaction " RESOURCE_LOCAL " >"
        <properties>
            <property name=javax.persistence.jdbc.user" value="用户"/>
            <property name="javax.persistence.jdbc.password" value="数据库密码"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/数据库名"/>
        <-! Hibernate configuration property ->
            <Property name = " the hibernate.show_sql " value = " to true " > </ Property> 
            <-! if the SQL statement format -> 
            <Property name = " hibernate.format_sql " value = " to true " /> 
            <! - - whether to automatically create database tables 
              value optional value update none the create 
              the create the program to automatically create database tables if there is to create a table after you remove the first 
              update program automatically creates a database table if the table does not exist create 
              none will not be created
             -> 
            <Property name = " Hibernate .hbm2ddl.auto " value="create " />
        </properties>
    </persistence-unit>
</persistence>

c: a key record in the database corresponding to the Entry class for each table creating an entity class key

(This section will complain directly copy, do not know why)

* the javax.persistence Import;.

@Entity   // class is jpa entity class
the @Table (name="cust_customer")   // configuration name corresponding to the mapping relationship and the entity class database table name table
public classthe Customer { // Configure self-generating growth strategies GenerationType.IDENTITY primary key @GeneratedValue (at strategy =GenerationType.IDENTITY)
the @Id  
//configure the relationship between property and field names @Column (name ="cust_id") Private LongcustId;
@Column (name
="CUST_NAME") Private String custName;
@Column(name
="cust_source") private String custSource;
@Column(name
="cust_industry") private String custIndustry;
@Column(name
="cust_level") private String custLevel;
@Column(name
="cust_address") private String custAddress;
@Column(name
="cust_phone") private String custPhone; public long getCustId() { return custId; } public void setCustId(long custId) { this.custId = custId; } public String getCustNmae() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; } }

   d: write test programs to achieve added data

1 to create a key target EntityManagerFactory finished using closed
2 using a factory object is connected EntityManagerFactory
         3 open things
4 Key Customer object record
5 Entitymanager object using a method of adding data to the database persist
6 things submission
7 Close the connection
 
     
import org.junit.Test;
 
     
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public
class JpaTest { @Test public void firstTest () { // . 1 EntityManagerFactory object using a key record finished off EntityManagerFactory Persistence.createEntityManagerFactory Factory = ( " myjpa " ); // 2 using a factory object EntityManagerFactory is connected to the EntityManager entityManager = Factory. createEntityManager (); // 3 open things the EntityTransaction Transaction = entityManager.getTransaction (); Transaction.Begin (); // 4 record key Customer object Customer the Customer = new newThe Customer (); customer.setCustName ( " ruirui " ); customer.setCustLevel ( " VIP " ); customer.setCustSource ( " network " ); customer.setCustPhone ( " 123456 " ); customer.setCustAddress ( " lazy Center " ) ; // 5 Entitymanager using a method of adding objects to the database data persist EntityManager.persist (Customer); // . 6 being filed transaction.commit (); // . 7 close the connection entityManager.close (); factory.close ();

 

 

If an error Welcome to the great God who pointed out that, thank you! ! !

Guess you like

Origin www.cnblogs.com/dragonyl/p/11352930.html