提示:仅供个人学习记录,欢迎纠正
文章目录- Spring-data jpa学习之路(第一站)
- Hibernate&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配置详情
com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/springdata_jpa?serverTimezone=UTC&characterEncoding=UTF-8 root root true true update org.hibernate.dialect.MySQL8Dialect
persistence.xml配置详情
org.hibernate.jpa.HibernatePersistenceProvider com.alice.pojo.Customer
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 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 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();
}
}
依赖
总结junit junit4.13 test org.hibernate hibernate-entitymanager5.4.32.Final mysql mysql-connector-java8.0.19 runtime
上述所讲主要是集成hibernate,之后以hibernate为基础继承Jpa



