jpa入门案例--使用jpa对数据库进行添加操作
注意:使用jpa对数据库进行增删改查的优点就是,不需要建表,也不需要写sql语句,前提是得有数据库
一、创建maven工程
二、引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.dzl</groupId>
<artifactId>jpa</artifactId>
<version>1.0-SNAPSHOT</version>
<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>
<!-- hibernate对jpa的支持包 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${project.hibernate.version}</version>
</dependency>
<!--c3p0-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${project.hibernate.version}</version>
</dependency>
<!-- log日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- Mysql and MariaDB -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
三、创建配置文件
在resources包下创建META-INF目录,在该目录下创建配置文件persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<!--配置持久化单元 name:持久化单元的名称-->
<!--transaction-type:事物的类型-->
<!--RESOURCE_LOCAL 单数据库的事物-->
<!--JTA: 分布式事物 跨数据库的事物-->
<persistence-unit name="myjpa" transaction-type="RESOURCE_LOCAL">
<properties>
<!--数据库连接属性配置-->
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="ROOT"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/dzl"/>
<!--hibernate属性配置-->
<!--是否显示sql 语句 在控制台-->
<property name="hibernate.show_sql" value="true"/>
<!--是否格式化sql 语句-->
<property name="hibernate.format_sql" value="true"/>
<!--是否自动 创建数据库的表-->
<!--值:update 程序自动创建表 假如有这个表就不在创建-->
<!--create 程序自动创建表 有 先删 再创建-->
<!--none 不会创建表-->
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
四、创建实体类
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();
}
}

浙公网安备 33010602011771号