【Hibernate】Re04 JPA规范使用
都忘了前面一些小前提,就是数据库需要是存在的,不过写链接参数都会写上的
JPA实现就是和Hibernate类似,也需要对应的配置文件等等。。。
1、配置文件必须命名【persistence.xml】且放在classpath目录下的META-INF目录下
<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"> <persistence-unit name="jpa.persist" transaction-type="RESOURCE_LOCAL"> <!-- name名称可以自定义。事务类型,本地资源? --> <!-- 配置javax.persistence.spi.PersistenceProvider的实现类 如果只有一个实现,其实也可以不写 --> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <class>cn.zeal4j.domain.Student</class> <!-- 对应的实体类 --> <properties> <!-- 链接参数 --> <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://IP:Port/jpa?serverTimezone=Asia/Shanghai"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="Password"/> <!-- 规范实现者的相关配置 也就是供应商 --> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
2、编写配置对应的实体类,因为本来就是JPA规范,所以和上一篇的注解使用没有改动
package cn.zeal4j.domain; /** * @author Administrator * @file Hibernate * @create 2020 09 23 22:09 */ import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; import java.io.Serializable; import java.util.Date; @Entity @Table(name = "user") public class User implements Serializable { private Integer id; private String username; private String password; private Date reg_time; public User() { } public User(Integer id, String username, String password) { this.id = id; this.username = username; this.password = password; } public User(Integer id, String username, String password, Date reg_time) { this(id, username, password); this.reg_time = reg_time; } @Id // 该注解定义实体标识符的属性 @GeneratedValue(generator = "increment", strategy = GenerationType.AUTO) // 两个主键生成策略,指示Hibernate如何生成 @GenericGenerator(name = "increment", strategy = "increment") public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Temporal(TemporalType.DATE) // 可以对时间类型的数据注解JPA提供的枚举时间类型 DATE, TIME, TIMESTAMP; @Column(name = "reg_time") // 标注类属性对应的表字段名称 public Date getReg_time() { return reg_time; } public void setReg_time(Date reg_time) { this.reg_time = reg_time; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } }
3、测试运行:
@Test public void JpaSpecification() { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa.persist"); EntityManager entityManager = null; EntityTransaction transaction = null; try { entityManager = entityManagerFactory.createEntityManager(); transaction = entityManager.getTransaction(); transaction.begin(); entityManager.persist(new Student(null, "学生Zeal4J", 18, 0, new Date(), "12343215531")); transaction.commit(); } catch (Exception exception) { exception.printStackTrace(); transaction.rollback(); } finally { entityManager.close(); } }