Hibernate5 Guide
(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的更多相关文章
- hibernate5.2需要的最少jar文件
hibernate5.2需要的最少jar文件: required文件夹中的所有jar文件 + mysql-connector-java-bin.jar.
- SSH(Struts2+Spring4+HIbernate5)的简化
今天给大家带来的是一个简单的新闻发布系统 首先在学习过程中我是深有体会,做事情不要浮躁,不要想着一口吃下一个胖子, 最最重要的是理解,理解透了学什么东西都是随心所欲的. 开发环境:win10系统 jd ...
- Beennan的内嵌汇编指导(译)Brennan's Guide to Inline Assembly
注:写在前面,这是一篇翻译文章,本人的英文水平很有限,但内嵌汇编是学习操作系统不可少的知识,本人也常去查看这方面的内容,本文是在做mit的jos实验中的一篇关于内嵌汇编的介绍.关于常用的内嵌汇编(AT ...
- 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 ...
- Scrum Guide - Scrum指南中文版
现在公司在使用敏捷开发模式进行日常的开发和管理工作,所以我看了下Ken Schwaber的<Scrum Guide>这本小册子,原本是英文的,这里提供中文的,以供日后复习和参考. Scru ...
- The Hacker's Guide To Python 单元测试
The Hacker's Guide To Python 单元测试 基本方式 python中提供了非常简单的单元测试方式,利用nose包中的nosetests命令可以实现简单的批量测试. 安装nose ...
- A Beginner's Guide to Paxos
Google Drive: A Beginner's Guide to Paxos The code ideas of Paxos protocol: 1) Optimistic concurrenc ...
- pipedata3d User Guide
pipedata3d User Guide 1. Introduction 在管道设计过程中,会使用到大量的标准,如ASME,DIN,GB,CB,HG,SH等等.管道设计人员在设计过程中,需要翻阅相关 ...
- 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 ...
随机推荐
- Java语言基础(10)
1 方法(三) 案例:Demo1 import java.util.Scanner; public class Demo1 { static int min(int num1,int num2){ i ...
- 阿里云端安装mysql
首先查看系统版本,是64位的centos7 file /sbin/init 安装指南如下 https://www.cnblogs.com/thinkingandworkinghard/p/671125 ...
- python OS模块 对操作系统接口调用
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cdos.curdir ...
- 一次使用自定义 Http Header 引发的血案
一次使用自定义 Http Header 引发的血案 HttpClient Http Header 自定义 nginx 不转发 起因 最近在整理我们产品的 OpenAPI Demo (Python.C ...
- SSH框架整合报错org.springframework.web.context.ContextLoaderListener
是因为在导入的jar包中缺少了一个 org.springframework.web_3.1.4.release.jar 在网上下载即可 下载地址: http://www.java2s.com/Code ...
- linux 下 svn 重新定位 url
linux下重新定位SVN URL方法: 如果更换了SVN服务器,就需要重新定位,指向新的SVN URL. 重新定位命令:svn switch --relocate 原svn地址 新svn地址 ...
- ESP8266—“ICACHE_FLASH_ATTR”宏
问:ESP8266_NONOS_SDK中ICACHE_FLASH_ATTR宏的用途是什么?我看到它取决于ICACHE_FLASH,但我不知道何时应该定义该符号.什么时候需要包括它?答:对于ESP826 ...
- thinkjs 安装笔记
1.首先安装thinkjsnpm install -g thinkjs(-g是指全局安装)检查是否安装成功:thinkjs -v 2.创建项目进入项目目录,初始化项目:thinkjs new proj ...
- JAVA访问Zabbix API
Zabbix 一.Zabbix 概述 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的 ...
- No message错误
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message 错误原因是因为表单提交的 ...