Hibernate5 Guide
阅读原文时间:2023年07月15日阅读:1

(1) 创建Maven工程

可以使用Eclipse或IDEA创建

(2) 修改pom文件

http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.hibernate5guide hibernate5guide 0.0.1-SNAPSHOT org.hibernate hibernate-core 5.2.12.Final mysql mysql-connector-java 5.1.44 org.apache.maven.plugins maven-compiler-plugin ${java-version} ${java-version} 1.8

(3) 添加hibernate.cfg.xml文件



com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/test xxx xxx org.hibernate.dialect.MySQL5Dialect true update

(4) 创建SessionFactoryBuilder拿到单例的SessionFactory

package hibernate5guide.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

/**
* An example to obtain the org.hibernate.SessionFactory.

* One benefit of this approach is JUnit test.

*/
public class SessionFactoryBuilder {

 private static SessionFactory instance;

 private SessionFactoryBuilder() {  
 }

 public static synchronized SessionFactory getInstance() {  
     return instance == null ? setUpSessionFactory() : instance;  
 }

 private static SessionFactory setUpSessionFactory() {  
     // A SessionFactory is set up once for an application!  
     // configures settings from hibernate.cfg.xml  
     final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();  
     try {  
         instance = new MetadataSources(registry).buildMetadata().buildSessionFactory();  
     } catch (Exception e) {  
         // The registry would be destroyed by the SessionFactory, but we had  
         // trouble building the SessionFactory, so destroy it manually.  
         StandardServiceRegistryBuilder.destroy(registry);  
     }

     return instance;  
 }  

}

(5) 创建实体类

package hibernate5guide.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "test_event")
public class TestEvent implements Serializable {

 private static final long serialVersionUID = 1L;

 @Id  
 @Column(name = "event\_id")  
 @GeneratedValue(generator = "increment")  
 @GenericGenerator(name = "increment", strategy = "increment")  
 private Long eventId;

 @Column(name = "event\_title")  
 private String eventTitle;

 @Column(name = "event\_date")  
 @Temporal(TemporalType.TIMESTAMP)  
 private Date eventDate;

 // a default constructor for entity must exist  
 public TestEvent() {  
 }

 public TestEvent(String eventTitle, Date eventDate) {  
     this.eventTitle = eventTitle;  
     this.eventDate = eventDate;  
 }

 public TestEvent(Long eventId, String eventTitle, Date eventDate) {  
     this.eventId = eventId;  
     this.eventTitle = eventTitle;  
     this.eventDate = eventDate;  
 }

 public Long getEventId() {  
     return eventId;  
 }

 public void setEventId(Long eventId) {  
     this.eventId = eventId;  
 }

 public String getEventTitle() {  
     return eventTitle;  
 }

 public void setEventTitle(String eventTitle) {  
     this.eventTitle = eventTitle;  
 }

 public Date getEventDate() {  
     return eventDate;  
 }

 public void setEventDate(Date eventDate) {  
     this.eventDate = eventDate;  
 }

 @Override  
 public String toString() {  
     return "TestEvent \[eventId=" + eventId + ", eventTitle=" + eventTitle + ", eventDate=" + eventDate + "\]";  
 }  

}

(6) 添加和查询测试

INSERT

Session session = getSessionFactory().openSession();
session.beginTransaction();
session.save(new TestEvent("first event!", new Date()));
session.save(new TestEvent("Second event!", new Date()));
session.getTransaction().commit();
session.close();

SELECT

Session session = getSessionFactory().openSession();
session.beginTransaction();
List result = session.createQuery("from TestEvent").list();
session.getTransaction().commit();
session.close();

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章