向客户中插入一条数据
如果使用Jpa框架可以不用先建表 可以使用框架生成表
2 实现步骤
a 创建工程 使用maven管理工程
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
配置连接数据库的相关配置
c: 创键一个Entry类 对应数据库中每个表创键一个实体类
package cn.lijun.jpa.entity;
import javax.persistence.*;
/**
* @author lijun
* @date 2019/8/14 10:38
*/
// 该类是jpa的实体类
@Entity
// 配置实体类和数据库表中映射关系 name 对应的表名
@Table(name="cust_customer")
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 关闭连接
package cn.lijun.jpa;
import cn.lijun.jpa.entity.Customer;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
/**
* @author lijun
* @date 2019/8/14 10:52
*/
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();
}
}