1. 创建项目 jpa
2. 在src下创建META-INF 并创建 persistence.xml 并写入以下内容
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<!-- 配置JPA持久化单元,RESOURCE_LOCAL表示本地事务,可以设置全局事务 -->
<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
<!-- 配置JPA的实现提供者 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- 配置Hibernate/JPA的参数,如果此处不配置则必须在classpath下定义hibernate.properties文件 -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/jpatest?useUnicode=true&characterEncoding=UTF-8" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="123456" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
</persistence>
3. 创建数据库 jpatest
4. 创建User类
package cn.itcast.jpa.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User implements Serializable {
static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;
@Column(nullable = false,length = 20,unique = false)
private String name;
public User(){};
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "id = " + this.id + " name = " + this.name ;
}
}
5. 写一个带main方法的类进行保存数据
package cn.itcast.jpa;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import cn.itcast.jpa.domain.Department;
import cn.itcast.jpa.domain.Employee;
import cn.itcast.jpa.domain.Sales;
import cn.itcast.jpa.domain.Skiller;
import cn.itcast.jpa.domain.User;
public class BaseTest {
public static void main(String[] args) {
addUser();
}
static void addUser() {
EntityManager em = null;
try {
em = Persistence.createEntityManagerFactory("jpa").createEntityManager();
User user = new User();
//user.setId("fdsafsd");
user.setName("hwz");
EntityTransaction tran = em.getTransaction();
tran.begin();
em.persist(user);
tran.commit();
}finally {
if(em != null) {
em.close();
}
}
}
}
要引入的包如下:
目录结构如下:
要注意的地方:
1. persistence.xml 中
<property name="hibernate.show_sql" value="true" /> 是指运行时是否显示sql语句
<property name="hibernate.hbm2ddl.auto" value="create" />
上面的这句的意思是表结构生成方式,一共有四种方式:note,validate,create,create-drop,update
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载 hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构
要谨慎使用这个参数设置