(1) 创建Maven工程

可以使用Eclipse或IDEA创建

(2) 修改pom文件

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hibernate5guide</groupId>
<artifactId>hibernate5guide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<java-version>1.8</java-version>
</properties>
</project>

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

 <?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">xxx</property>
<property name="connection.password">xxx</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="hibernate5guide.entity.TestEvent" />
</session-factory>
</hibernate-configuration>

(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.<br>
* One benefit of this approach is JUnit test.<br>
*/
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<TestEvent> result = session.createQuery("from TestEvent").list();
session.getTransaction().commit();
session.close();

Hibernate5 Guide的更多相关文章

  1. hibernate5.2需要的最少jar文件

    hibernate5.2需要的最少jar文件: required文件夹中的所有jar文件 + mysql-connector-java-bin.jar.

  2. SSH(Struts2+Spring4+HIbernate5)的简化

    今天给大家带来的是一个简单的新闻发布系统 首先在学习过程中我是深有体会,做事情不要浮躁,不要想着一口吃下一个胖子, 最最重要的是理解,理解透了学什么东西都是随心所欲的. 开发环境:win10系统 jd ...

  3. Beennan的内嵌汇编指导(译)Brennan's Guide to Inline Assembly

    注:写在前面,这是一篇翻译文章,本人的英文水平很有限,但内嵌汇编是学习操作系统不可少的知识,本人也常去查看这方面的内容,本文是在做mit的jos实验中的一篇关于内嵌汇编的介绍.关于常用的内嵌汇编(AT ...

  4. The Practical Guide to Empathy Maps: 10-Minute User Personas

    That’s where the empathy map comes in. When created correctly, empathy maps serve as the perfect lea ...

  5. Scrum Guide - Scrum指南中文版

    现在公司在使用敏捷开发模式进行日常的开发和管理工作,所以我看了下Ken Schwaber的<Scrum Guide>这本小册子,原本是英文的,这里提供中文的,以供日后复习和参考. Scru ...

  6. The Hacker's Guide To Python 单元测试

    The Hacker's Guide To Python 单元测试 基本方式 python中提供了非常简单的单元测试方式,利用nose包中的nosetests命令可以实现简单的批量测试. 安装nose ...

  7. A Beginner's Guide to Paxos

    Google Drive: A Beginner's Guide to Paxos The code ideas of Paxos protocol: 1) Optimistic concurrenc ...

  8. pipedata3d User Guide

    pipedata3d User Guide 1. Introduction 在管道设计过程中,会使用到大量的标准,如ASME,DIN,GB,CB,HG,SH等等.管道设计人员在设计过程中,需要翻阅相关 ...

  9. Flume官方文档翻译——Flume 1.7.0 User Guide (unreleased version)中一些知识点

    Flume官方文档翻译--Flume 1.7.0 User Guide (unreleased version)(一) Flume官方文档翻译--Flume 1.7.0 User Guide (unr ...

随机推荐

  1. PHP工程师学习计划

    从开始学习PHP到现在,只是大致的对PHP的一些基础的东西了解一下,从没有制定一个较为完整的学习计划,所以自己的编程水平一直都处在基本的入门阶段,所以结合自己的实际情况制定了一个感觉还算合理的学习计划 ...

  2. libusb bulk

    https://github.com/IzyaSoft/EasyUsb https://github.com/ztguang/libusb-usbip-bulktransfer/blob/master ...

  3. 安装keras和解决python3退格显示^H的问题

    我的是centos7的版本,然后之前是安装好了3.5,默认安装了2.7 看了几篇博文和手上的书,发现安装没有那么麻烦,只需要 pip install tensorflow pip install ke ...

  4. main方法类 为何由AppClassLoader加载

    AppClassLoader AppClassLoader应用类加载器,又称系统类加载器,负责在JVM启动时加载来自命令java中的classpath或者java.class.path系统属性或者CL ...

  5. 自动生成SSM框架

    使用idea 新创建项目 然后 新创建 java .resources 文件夹...... 图上是项目结构 java文件夹下的 文件夹 命名规范 com.nf147(组织名)+ oukele(作者) ...

  6. Python之文字转图片

    Pygame模块一览表: 引入pygame模块 ,若本机没有请自行pip install pygame #载入必要的模块 import pygame #pygame初始化 pygame.init() ...

  7. BZOJ 2836: 魔法树 (树链剖分+线段树)

    板题-记得开longlong #include <cstdio> #include <cctype> #include <cstring> #include < ...

  8. Spring中 bean的生命周期

    为什么要了解Spring中 bean的生命周期? 有时候我们需要自定义bean的创建过程,因此了解Spring中 bean的生命周期非常重要. 二话不说先上图: 在谈具体流程之前先看看Spring官方 ...

  9. java内存区域以及GC回收

    参考资料: http://www.cnblogs.com/zhguang/p/3257367.html 概要: Java GC机制主要完成3件事:确定哪些内存需要回收,确定什么时候需要执行GC,如何执 ...

  10. TTTTTTTTTTTTTT CF 95 B 构造4,7幸运数字 贪心 构造 string

    B. Lucky Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...