产生原因:如果单独使用hibernate的API来进行持久化操作,则不能随意切换其他ORM框架
1 操作步骤
1.1 导入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.6.8.Final</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?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"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/springdata_jpa"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="19980617pyr"/> <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> </properties> </persistence-unit> </persistence>
|
1.3 增加测试实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package entity;
import javax.persistence.*; import java.io.Serializable;
@Entity @Table(name = "cst_customer") public class Customer{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "cust_id") private Long custId;
@Column(name = "cust_name") private String custName;
public void setCustId(Long custId) { this.custId = custId; }
public void setCustName(String custName) { this.custName = custName; }
@Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + '}'; } }
|
1.4 测试
准备工作
1 2 3 4 5 6 7 8 9
| public class Test { EntityManager entityManager;
@Before public void init() { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hibernateJPA"); entityManager = entityManagerFactory.createEntityManager(); } }
|
Save
1 2 3 4 5 6 7 8 9 10 11 12
| @org.junit.Test public void testSave() { EntityTransaction transaction = entityManager.getTransaction(); transaction.begin();
Customer customer = new Customer(); customer.setCustName("张三"); entityManager.persist(customer);
transaction.commit(); entityManager.close(); }
|
Delete
1 2 3 4 5 6 7 8 9 10 11
| @org.junit.Test public void testRemove() { EntityTransaction transaction = entityManager.getTransaction(); transaction.begin();
Customer customer = entityManager.find(Customer.class, 3L); entityManager.remove(customer); transaction.commit(); entityManager.close(); }
|
⚠️:这里要删除的对象需要是数据库里查询到的,和entityManager发生过关联的对象,即持久状态。或者使用HQL。new出来的对象无法完成删除逻辑。
Update
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @org.junit.Test public void testUpdate() { EntityTransaction transaction = entityManager.getTransaction(); transaction.begin();
Customer customer = new Customer(); customer.setCustName("张三123"); customer.setCustId(3L); Customer rs = entityManager.merge(customer); System.out.println(rs); transaction.commit(); entityManager.close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @org.junit.Test public void testHQLUpdate() { EntityTransaction transaction = entityManager.getTransaction(); transaction.begin();
String sqlQuery = "update Customer set custName=:name where custId=:id"; entityManager.createQuery(sqlQuery) .setParameter("name", "徐庶143") .setParameter("id", 2L) .executeUpdate();
transaction.commit(); entityManager.close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @org.junit.Test public void testSQLUpdate() { EntityTransaction transaction = entityManager.getTransaction(); transaction.begin();
String sqlQuery = "update cst_customer set cust_id=:name where cust_name=:id"; entityManager.createNativeQuery(sqlQuery) .setParameter("name", "徐庶123") .setParameter("id", 2L) .executeUpdate();
transaction.commit(); entityManager.close(); }
|
⚠️:这里使用createNativeQuery,表明和字段名都换成小写
Select
1 2 3 4 5 6 7 8 9 10 11
| @org.junit.Test public void testFind() { EntityTransaction transaction = entityManager.getTransaction(); transaction.begin();
Customer rs = entityManager.find(Customer.class, 3L); System.out.println("========================"); System.out.println(rs); transaction.commit(); entityManager.close(); }
|
1 2 3 4 5 6 7 8 9 10 11
| @org.junit.Test public void testLazyFind() { EntityTransaction transaction = entityManager.getTransaction(); transaction.begin();
Customer rs = entityManager.getReference(Customer.class, 3L); System.out.println("========================"); System.out.println(rs); transaction.commit(); entityManager.close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| @org.junit.Test public void testHQLSelect() { EntityTransaction transaction = entityManager.getTransaction(); transaction.begin();
String sqlQuery = "select c from Customer c"; Query query = entityManager.createQuery(sqlQuery); List<Customer> resultList = query.getResultList(); resultList.forEach(System.out::println);
transaction.commit(); entityManager.close(); }
|