JPA的配置文件
JPA的配置文件
一、引入包
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.8-dmr</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.17.Final</version>
</dependency>
</dependencies>
二、配置resources/META-INF/persistence.xml的书写
<?xml version="1.0" encoding="utf-8" ?>
<!--导入schema约束,此约束来源:复制hibernate-core:5.2.16.Final包下的/org/hibernate/jpa/persistence_2_0.xsd文件中的这一段出来即可。-->
<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_2_0.xsd"
version="2.0">
<!--配置持久化单元(可以配置多个,名称不能重复)
name:用于指定持久化单元的名称
transcation-type:指定事务的类型。
JTA:Java Transcation API
RESOURCE_LOCAL:指的是本地代码事务
-->
<persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<!--JPA规范提供商,可以不写-->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!--指定Jpa注解的实体类型位置,可以不写-->
<class>com.demo.domain.Customer</class>
<!--连接相关的一些配置,都是用hibernate的。-->
<properties>
<!--第一部分,连接数据库信息-->
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"></property>
<property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/jpademodb?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai"></property>
<property name="hibernate.connection.username" value="root"></property>
<property name="hibernate.connection.password" value="123456"></property>
<!--说明:数据库的方言,用于存放不同数据库之间的SQL语句差异。-->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect"></property>
<!--第二部分,hibernate的可选配置-->
<!--是否显示hiberante的生成的SQL语句-->
<property name="hibernate.show_sql" value="true"></property>
<!--是否使用格式化输出SQL语句到控制台-->
<property name="hibernate.format_sql" value="false"></property>
<!--采用何种方式生成DDL语句,update表示检测实体类的映射配置与数据库表结构是否一致,不一致,则更新数据库。-->
<property name="hibernate.hbm2ddl.auto" value="update"></property>
<!--连接池的配置,这里使用的是c3p0连接池,常用的还有阿里的-->
<property name="hibernate.connection.provider_class" value="org.hibernate.c3p0.internal.C3P0ConnectionProvider"></property>
</properties>
</persistence-unit>
</persistence>
三、实体类写法
package domains;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
@Entity
@Table(name = "student")
@DynamicUpdate(true)
public class Student {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id",length = 36)
private String id;
@Column(name = "name", length = 255)
private String name;
@Column(name = "age")
private Integer age;
@ManyToOne(targetEntity = Teacher.class)
@JoinColumn(name = "teacherId", referencedColumnName = "id")
private Teacher teacher;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
package domains;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@Entity
@Table(name = "teacher")
public class Teacher {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id",length = 36)
private String id;
@Column(name = "name", length = 255)
private String name;
@OneToMany(targetEntity = Student.class, mappedBy = "teacher")
private Set<Student> students = new HashSet<Student>(0);
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
四、调用处
package business;
import domains.Teacher;
import javax.persistence.*;
import java.util.List;
public class Main {
public static void main(String[] args) {
EntityManagerFactory entryManagerFactory = Persistence.createEntityManagerFactory("testdb-persistence-unit");
EntityManager manager = entryManagerFactory.createEntityManager();
Query query = manager.createNativeQuery("select * from student");
List result = query.getResultList();
System.out.println(result);
EntityTransaction trans = manager.getTransaction();
trans.begin();
Teacher t = new Teacher();
t.setName("李老师");
manager.persist(t);
trans.commit();
manager.close();
}
}
JPA的配置文件
https://www.dearcloud.cn/2018/05/02/20200310-cnblogs-old-posts/20180502-JPA的配置文件/