jpa入门案例--使用jpa对数据库进行添加操作

注意:使用jpa对数据库进行增删改查的优点就是,不需要建表,也不需要写sql语句,前提是得有数据库

一、创建maven工程

二、引入依赖



    4.0.0

    cn.dzl
    jpa
    1.0-SNAPSHOT

    
        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
        
    


三、创建配置文件

在resources包下创建META-INF目录,在该目录下创建配置文件persistence.xml



    
    
    
    

    

        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    

四、创建实体类

package cn.dzl.jpa.entity;

import javax.persistence.*;

@Entity
@Table(name="cst_customer")
public class Customer {
    // 配置主键生成的策略
    @GeneratedValue(strategy = GenerationType.IDENTITY)
//    cust_id
//    cust_name
//    cust_source
//    cust_industry
//    cust_level
//    cust_address
//    cust_phone
    // 配置主键使用的字段
    @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 getCustName() {
        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 + '\'' +
                '}';
    }
}

 五、编写测试类

package cn.dzl;

import cn.dzl.jpa.entity.Customer;
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 firstJpa(){
//        1  创建EntityManagerFactory
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("myjpa");
//
//        2 使用工厂对象  创建一个EntityManager对象
        EntityManager entityManager = factory.createEntityManager();
//        3 开启事物
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
//        4 EntityManager对象persist方法插入数据库
        Customer customer = new Customer();
        customer.setCustName("9502");
        customer.setCustLevel("黑金vip");
        customer.setCustSource("抖音");
        customer.setCustPhone("6666666");
        customer.setCustAddress("兰德中心");
        entityManager.persist(customer);
//        5 事物提交
        transaction.commit();

//        6 关闭连接
        entityManager.close();
        factory.close();

    }
}

  

你可能感兴趣的:(jpa入门案例--使用jpa对数据库进行添加操作)