新手上路之Hibernate:第一个Hibernate例子
一、Hibernate概述
(一)什么是Hibernate?
Hibernate核心内容是ORM(关系对象模型)。可以将对象自动的生成数据库中的信息,使得开发更加的面向对象。这样作为程序员就可以使用面向对象的思想来操作数据库,而不用关心繁琐的JDBC。所以,Hibernate处于三层架构中的D层(持久层)。
(二)使用Hibernate的优点
1、Hibernate可以使用在java的任何项目中,不一定非要使用在java web项目中。因为Hibernate不需要类似于tomact这些容器的支持,可以直接通过一个main方法进行测试。
2、通过下面的实例,可以发现使用Hibernate可以大大减少代码量。
3、由于使用了Hibernate,代码中不涉及具体的JDBC语句,所以就方便了代码的可移植性。
二、Hibernate开发的环境搭建
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory >
- <!-- mysql数据库驱动 -->
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <!-- mysql数据库名称 -->
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
- <!-- 数据库的登陆用户名 -->
- <property name="hibernate.connection.username">root</property>
- <!-- 数据库的登陆密码 -->
- <property name="hibernate.connection.password">root</property>
- <!-- 方言:为每一种数据库提供适配器,方便转换 -->
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- </session-factory>
- </hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory >
<!-- mysql数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql数据库名称 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<!-- 数据库的登陆用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库的登陆密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 方言:为每一种数据库提供适配器,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory>
</hibernate-configuration>
三、HIbernate第一个实例
- import java.util.Date;
- public class User {
- private String id;
- private String username;
- private String password;
- private Date createTime;
- private Date expireTime;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String userName) {
- this.username = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Date getCreateTime() {
- return createTime;
- }
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
- public Date getExpireTime() {
- return expireTime;
- }
- public void setExpireTime(Date expireTime) {
- this.expireTime = expireTime;
- }
- }
import java.util.Date;
public class User {
private String id;
private String username;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String userName) {
this.username = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.example.hibernate.User">
- <id name="id">
- <generator class="uuid"/>
- </id>
- <property name="username"/>
- <property name="password"/>
- <property name="createTime"/>
- <property name="expireTime"/>
- </class>
- </hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="com.example.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
其中的property标签是将要生成是数据库表中的字段,在这里不用关心各个字段是什么类型的。因为Hibernate会根据上面的实体类中属性的类型来决定将来表中字段的类型
- <hibernate-configuration>
- <session-factory >
- <!-- mysql数据库驱动 -->
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <!-- mysql数据库名称 -->
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
- <!-- 数据库的登陆用户名 -->
- <property name="hibernate.connection.username">root</property>
- <!-- 数据库的登陆密码 -->
- <property name="hibernate.connection.password">root</property>
- <!-- 方言:为每一种数据库提供适配器,方便转换 -->
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <SPAN style="COLOR: #ff0000"><mapping resource="com/example/hibernate/User.hbm.xml"/></SPAN>
- </session-factory>
- </hibernate-configuration>
<hibernate-configuration>
<session-factory >
<!-- mysql数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql数据库名称 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<!-- 数据库的登陆用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库的登陆密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 方言:为每一种数据库提供适配器,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource="com/example/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
注意:必须是“/”而不能是“.”。
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- /**
- * 将hbm生成ddl
- * @author BCH
- *
- */
- public class ExoprtDB {
- public static void main(String[] args) {
- //默认读取hibernate.cfg.xml文件
- Configuration cfr = new Configuration().configure();
- SchemaExport export = new SchemaExport(cfr);
- export.create(true, true);
- }
- }
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl
* @author BCH
*
*/
public class ExoprtDB { public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfr = new Configuration().configure(); SchemaExport export = new SchemaExport(cfr);
export.create(true, true);
}
}
到这里就可以生成User表了,但是如果直接运行ExoprtDB.java文件是不能生成User表的。因为在mysql数据中还没有建立数据库Hibernate-first。所以在mysql控制台中通过create database hibernate-first; use hibernate-first;之后再执行ExoprtDB.java文件就可以生成表了。
- import java.util.Date;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class Client {
- public static void main(String[] args) {
- //读取配置文件
- Configuration cfg = new Configuration().configure();
- SessionFactory factory = cfg.buildSessionFactory();
- Session session = null;
- try{
- session = factory.openSession();
- //开启事务
- session.beginTransaction();
- User user = new User();
- user.setUsername("用户名");
- user.setPassword("123");
- user.setCreateTime(new Date());
- user.setExpireTime(new Date());
- session.save(user);
- //提交事务
- session.getTransaction().commit();
- }catch(Exception e){
- e.printStackTrace();
- //回滚事务
- session.getTransaction().rollback();
- }finally{
- if(session != null){
- if(session.isOpen()){
- //关闭session
- session.close();
- }
- }
- }
- }
- }
import java.util.Date; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class Client {
public static void main(String[] args) {
//读取配置文件
Configuration cfg = new Configuration().configure(); SessionFactory factory = cfg.buildSessionFactory(); Session session = null;
try{
session = factory.openSession();
//开启事务
session.beginTransaction(); User user = new User();
user.setUsername("用户名");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date()); session.save(user);
//提交事务
session.getTransaction().commit(); }catch(Exception e){
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally{
if(session != null){
if(session.isOpen()){
//关闭session
session.close();
}
}
}
}
}
(四)总结
新手上路之Hibernate:第一个Hibernate例子的更多相关文章
- hibernate部分源码解析and解决工作上关于hibernate的一个问题例子(包含oracle中新建表为何列名全转为大写且通过hibernate取数时如何不用再次遍历将列名(key)值转为小写)
最近在研究系统启动时将数据加载到内存非常耗时,想着是否有办法优化!经过日志打印测试发现查询时间(查询时间:将数据库数据查询到系统中并转为List<Map>或List<*.Class& ...
- 这是一个hibernate 联合主键的例子
package com.bird.entity; import java.io.Serializable; import javax.persistence.Entity; import javax. ...
- Hibernate入门1 - Hibernate概述及第一个小例子
一.什么是ORM? ORM,即Object Relational Mapping.我们知道,利用面向对象的思想编写的数据库应用程序最终都是把对象信息保存在关系型数据库中,于是需要编写与底层数据库相关的 ...
- Spring和Hibernate结合的一个小例子
1.新建一个SpringHibernate的maven项目 2.pom文件的依赖为 <dependency> <groupId>junit</groupId> &l ...
- Hibernate的一个简单应用例子
Hibernate是一个开源的ORM框架,顾名思义,它的核心思想即ORM(Object Relational Mapping,对象关系映射),可以通过对象来操作数据库中的信息,据说开发者一开始是不太熟 ...
- Hibernate入门(1)-第一个Hibernate程序
Hibernate入门(1)-第一个Hibernate程序 Hibernate是最著名的ORM工具之一,本系列文章主要学习Hibernate的用法,不涉及Hibernate的原理.本文介绍第一个Hib ...
- 一个Hibernate小程序
基本步骤 在前一篇博文Hibernate环境搭建中为大家详细的介绍如何搭建一个学习新类库的学习环境.今天,为大家带来一个Hibernate小例子,让大家能够快速上手. 步骤如下: 1.配置hibern ...
- hibernate安装和配置和第一个Hibernate应用
ssh的各个版本要搭配好,struts2.3一般搭配hibernate4.x,我下的是4.3. Hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了Hiber ...
- 配置和创建一个hibernate简单应用
1.配置 到http://hibernate.org/orm/下载hibernate包然后解压 在eclipse中新建一个java project,如名为hibernate_test 再所建工程中新建 ...
随机推荐
- SDL播放声音
extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> ...
- JQuery知识点链接
1.深入理解jQuery插件开发 http://learn.jquery.com/plugins/basic-plugin-creation/ 2.jQuer ...
- 利用闪回查看Oracle表历史时刻数据
利用闪回查看Oracle表历史时刻数据 1.查看表历史时刻数据 select * from tab_test AS OF TIMESTAMP to_timestamp('20140917 10:00: ...
- POJ 3026(BFS+prim)
http://poj.org/problem?id=3026 题意:任意两个字母可以连线,求把所有字母串联起来和最小. 很明显这就是一个最小生成树,不过这个题有毒.他的输入有问题.在输入m和N后面,可 ...
- VIM基础
http://www.cnblogs.com/wawlian/archive/2012/05/22/2512801.html http://www.cnblogs.com/wawlian/archiv ...
- Android常用控件
Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑 ...
- Unity3d《Shader篇》法线贴图
效果图 贴图 法线贴图 //代码 Shader "Custom/NormalMap" { Properties { _MainTex ("Texture", 2 ...
- the XMLHttpRequest Object
Ajax的核心是XMLHttpRequest对象.XMLHttpRequest对象允许异步发送请求并且以文本的形式返回结果.发送什么样的请求(what),在服务器端怎样处理这个请求(how),返回什么 ...
- ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)
这一题是练习广度优先搜索很好的例题,在很多广搜教学中经常用到,放在这里供学习搜索算法的孩纸们看看= = 题目大意:一维数轴上,农夫在N点,牛在K点,假定牛不会移动,农夫要找到这头牛只能够进行以下三种移 ...
- Python 之 【re模块的正则表达式学习】
摘要: re模块包括操作正则表达式的函数,一些工作中都需要用到,现在说明下使用方法. 使用说明: 一,re模块下的函数: 函数 描述 compile(pa ...