springDataJPA入门

一:JPA

    1.1 jpa 是一个开发规范   是一个ORM框架的开发规范   是SUN 公司定义的  中文名:JAVA持久层API

​      orm :  对象关系映射   是一个解决问题的思路 对对象的操作就是对数据库的操作
:什么是SpringDataJPA
​  Spring  Data  JPA框架主要针对的是Spring 唯一没有简化到业务逻辑代码   开发者连仅实现持久层业务逻辑的工作都省了,唯一要做的就是生明持久层的接口    其它的都交给Spring Data JPA来帮完成
​  并不是JPA规范实现  基于原生的jpa 的api   进行了再次的封装
​  如果要使用SpringDataJpa    还需要使用Hibernate
三:入门程序
      1需求
​           向客户中插入一条数据
​           如果使用Jpa框架可以不用先建表  可以使用框架生成表
  

        UTF-8
        5.0.7.Final
        1.8
        1.8
    
    
        
            junit
            junit
            4.12
        
        
        
            org.hibernate
            hibernate-entitymanager
            ${project.hibernate.version}
        
        
            org.hibernate
            hibernate-c3p0
            ${project.hibernate.version}
        
        
            log4j
            log4j
            1.2.17
        
        
            mysql
            mysql-connector-java
            5.1.6
        
    
b   创键一个配置文件
​    配置文件的路径必须是:META-INF/persistence.xml
​    配置连接数据库的相关配置
"1.0" encoding="UTF-8"?>
"http://java.sun.com/xml/ns/persistence" version="2.0">
    
    "myjpa" transaction-type="RESOURCE_LOCAL">
        
            "javax.persistence.jdbc.user" value="用户"/>
            "javax.persistence.jdbc.password" value="数据库密码"/>
            "javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            "javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/数据库名"/>
        
            "hibernate.show_sql" value="true">
            
            "hibernate.format_sql" value="true"/>
            
            "hibernate.hbm2ddl.auto" value="create"/>
        
    

c:  创键一个Entry类  对应数据库中每个表创键一个实体类

(这一段直接复制会报错,也不知道为啥)

import javax.persistence.*;

@Entity  // 该类是jpa的实体类
@Table(name="cust_customer")  // 配置实体类和数据库表中映射关系   name   对应的表名
public class Customer { // 配置主键的生成策略 GenerationType.IDENTITY 自增长 @GeneratedValue(strategy = GenerationType.IDENTITY)
@Id  
//配置属性和字段名之间关系 @Column(name="cust_id") private long custId;
@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: 编写测试程序   实现数据的添加

​        1  创键一个EntityManagerFactory  对象    使用完关闭
​         2  使用工厂对象EntityManagerFactory  就是一个连接
         3  开启事物
​         4  创键  Customer  对象
​        5 使用Entitymanager 对象  的persist 方法向数据库添加数据
​        6  事物提交
​        7  关闭连接

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 对象 使用完关闭 EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa"); // 2 使用工厂对象EntityManagerFactory 就是一个连接 EntityManager entityManager = factory.createEntityManager(); // 3 开启事物 EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); // 4 创键 Customer 对象 Customer customer = new Customer(); customer.setCustName("ruirui"); customer.setCustLevel("vip"); customer.setCustSource("网络"); customer.setCustPhone("123456"); customer.setCustAddress("懒人中心"); // 5 使用Entitymanager 对象 的persist 方法向数据库添加数据 entityManager.persist(customer); // 6 事物提交 transaction.commit(); // 7 关闭连接 entityManager.close(); factory.close();

 

 

如果错误欢迎大神们指出,谢谢!!!

转载于:https://www.cnblogs.com/dragonyl/p/11352930.html

你可能感兴趣的:(springDataJPA入门)