(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. ADO.net 增删改查封装DBhelper

    using System; using System.Collections.Generic; using System.Data.SqlClient;//引用数据库客户端 using System. ...

  2. UVa400 Unix is

    The computer company you work for is introducing a brand new computer line and is developing a new U ...

  3. springboot2.x下actuator模块

    一.简介    spring-boot-starter-actuator模块是一个spring提供的监控模块.我们在开运行发过程中,需要实时和定时监控服务的各项状态和可用性.Spring Boot的s ...

  4. 《Learning Structured Representation for Text Classification via Reinforcement Learning》论文翻译.md

    摘要 表征学习是自然语言处理中的一个基本问题.本文研究了如何学习文本分类的结构化表示.与大多数既不使用结构又依赖于预先指定结构的现有表示模型不同,我们提出了一种强化学习(RL)方法,通过自动覆盖优化结 ...

  5. Module parse failed: Export 'instance' is not defined (35:19)

    Module parse failed: Export 'instance' is not defined (35:19) 使用npm出现的这错误,用yarn就可以了 这种情况rm node_modu ...

  6. java怎样实现重载一个方法

    重载(重新载选方法): java允许在一个类中,存在多个方法拥有相同的名字,但在名字相同的同时,必须有不同的参数,这就是重载,编译器会根据实际情况挑选出正确的方法,如果编译器找不到匹配的参数或者找出多 ...

  7. mysql清表时有外键关联处理办法

    可以忽视关联的情况下: 先取消主外键关系验证:SET FOREIGN_KEY_CHECKS = 0; 然后删除需要删除的数据:truncate table table_name; 最后恢复:SET F ...

  8. python-获取程序的路径

    python获取当前路径 import os,sys 使用sys.path[0].sys.argv[0].os.getcwd().os.path.abspath(file).os.path.realp ...

  9. FRP

    使用 FRP 反向代理实现 Windows 远程连接 互联网普及率的日渐攀升与 IPv4 资源的持续减少,现在大部分家庭宽带都不会分配公网 IP ,这使一些网络应用的实现多了些困难,像个人的 NAS ...

  10. SQL复杂查询语句-SELECT * FROM cs WHERE score>70 GROUP BY s_id HAVING COUNT(*)>1

    如果同时存在where,group by,的时候的执行顺序应该是这样的: 1,首先where后面添加条件把数据进行了过滤,返回一个结果集 2,然后group by将上面返回的结果集进行分组,返回一个结 ...