persistence-demo之jpa
作者:互联网
0、前言
目的:旨在记录jpa操作数据库,持久层的hibernate框架实现了jpa
项目结构:
1、导入pom文件
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.17.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
2、编写核心配置文件persistence.xml
文件要求的目录是:src/main/resources/META-INF
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="hibernate-jpa">
<!-- 1、jpa是一个规范,是一个接口,它没有任何的具体实现,这里需要指定一个jpa具体实现 -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<!-- 2、连接数据库的信息 -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://10.10.50.160:3306/test?characterEncoding=UTF-8" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="abc123456#" />
<!-- 3、jpa的具体实现,hibernate的基本属性 -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<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>
3、编写实体类
package cn.itcast.orm.entity;
import javax.persistence.*;
/**
* @author jiangl
* @ClassName Book.java
* @Description 实体类,图书
* @createTime 2022年06月03日 11:21
*/
@Entity
@Table(name = "t_book")
public class Book
{
@Id
@Column(name = "bid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;//主键
@Column(name = "bname")
private String name;//图书名称
@Column(name = "author")
private String author;//作者
@Column(name = "price")
private double price;//价格
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
@Override
public String toString()
{
return "Book{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + '}';
}
}
4、编写测试类
import cn.itcast.orm.entity.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
/**
* @author jiangl
* @ClassName BookTest.java
* @Description 测试类
* @createTime 2022年06月03日 12:46
*/
public class BookTest
{
@Test
public void testSave()
{
//1.创建entityManagerFactory -- 参数为<persistence-unit name="hibernate-jpa">的name值
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-jpa");
//2.创建EntityManager
EntityManager entityManager = entityManagerFactory.createEntityManager();
//3.开启事务处理
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
//4.执行数据的CRUD
Book book = new Book();
book.setName("c#从入门到精通");
book.setAuthor("传智播客");
book.setPrice(9.8);
entityManager.persist(book);
//5.提交事务
transaction.commit();
//6.关闭entityManager和entityManagerFactory
entityManager.close();
entityManagerFactory.close();
}
}
5、测试
保存数据成功。
标签:hibernate,name,author,jpa,demo,price,import,public,persistence 来源: https://www.cnblogs.com/jspider/p/16347146.html