Spring-data jpa学习之路(第一站)
提示:仅供个人学习记录,欢迎纠正
Hibernate&Jpa的快速创建
实体类Customer:
package com.alice.pojo;
import javax.persistence.*;
@Entity
@Table(name = "td_customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cust_id")
private Long custId;
@Column(name = "cust_name")
private String custName;
@Column(name = "cust_address")
private String custAddress;
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 getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
@Override
public String toString() {
return "Customer{" +
"custId=" + custId +
", custName='" + custName + '\'' +
", custAddress='" + custAddress + '\'' +
'}';
}
}
hibernate.cfg.xml配置详情
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置数据库的相关信息 -->
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/springdata_jpa?serverTimezone=UTC&characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 配置hibernate的相关信息 -->
<!-- 是否显示底层的sql语句 -->
<property name="show_sql">true</property>
<!-- 是否格式化sql语句 -->
<property name="format_sql">true</property>
<!-- hibernate会帮自己创建数据库表,默认不会创建,需要配置。
值 update: 如果数据库里面不存在表,创建;如果已经存在,更新 -->
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>
<!-- 配置事务自动提交 -->
<!-- <property name="hibernate.connection.autocommit">true</property> -->
<!-- 引入映射配置文件 -->
<!-- <mapping resource="com/meng/entity/Users.hbm.xml" />-->
<!--指定那些pojo需要进行ORM映射 -->
<mapping class="com.alice.pojo.Customer"></mapping>
</session-factory>
</hibernate-configuration>
persistence.xml配置详情
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="hibernateJPA" transaction-type="RESOURCE_LOCAL">
<!--配置使用什么 ORM 产品来作为 JPA 的实现 -->
<!--5.2版本以后,无法使用HibernatePersistence-->
<!--如果JPA项目中只有一个实现产品,可以配置不这个节点-->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!--添加持久化类-->
<class>com.alice.pojo.Customer</class>
<properties>
<!-- 连接数据库的基本信息 -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/springdata_jpa?serverTimezone=UTC&characterEncoding=UTF-8"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<!-- 配置 JPA 实现产品的基本属性. 配置 hibernate 的基本属性 -->
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
</properties>
</persistence-unit>
</persistence>
HibernateTest测试类
package com.alice.test;
import com.alice.pojo.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
public class HibernateTest {
//session工厂,:数据库的一个会话,代码持久化操作数据库的桥梁
private SessionFactory sf;
@Before
public void init(){
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("/hibernate.cfg.xml").build();
sf = new MetadataSources(registry).buildMetadata().buildSessionFactory();
}
@Test
public void testC(){
try(Session session = sf.openSession()){
Transaction transaction = session.beginTransaction();
Customer customer = new Customer();
customer.setCustName("AIX");
session.save(customer);
transaction.commit();
}
}
@Test
public void testR(){
try(Session session = sf.openSession()){
Transaction transaction = session.beginTransaction();
String hql = " FROM Customer ";
List<Customer> resultList = session.createQuery(hql, Customer.class).getResultList();
System.out.println(resultList);
transaction.commit();
}
}
@Test
public void testR2(){
try(Session session = sf.openSession()){
Transaction transaction = session.beginTransaction();
String hql = " FROM Customer where custId=:id";
List<Customer> resultList = session.createQuery(hql, Customer.class)
.setParameter("id",1L)
.getResultList();
System.out.println(resultList);
transaction.commit();
}
}
}
JpaTest测试类
package com.alice.test;
import com.alice.pojo.Customer;
import org.junit.Before;
import org.junit.Test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class JpaTest {
EntityManagerFactory factory;
@Before
public void before(){
factory = Persistence.createEntityManagerFactory("hibernateJPA");
}
@Test
public void testC(){
EntityManager entityManager = factory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
Customer customer = new Customer();
customer.setCustName("张三");
entityManager.persist(customer);
tx.commit();
}
}
依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.32.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<scope>runtime</scope>
</dependency>
</dependencies>
总结
上述所讲主要是集成hibernate,之后以hibernate为基础继承Jpa