(一)问候Hibernate4
第一节:Hibernate 简介
官网:http://hibernate.org/
Hibernate 是一个开放源代码的对象关系映射框架,它对JDBC 进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。
Hibernate 可应用在任何使用JDBC 的场合,既可以在Java 的客户端程序使用,也可以在Servlet/JSP 的Web 应用中使用,最具革命意义的是,Hibernate可以在应用EJB 的J2EE 架构中取代CMP,完成数据持化的重任。
学习重点:
ORM 框架,对象关系映射(Object/Relation Mapping)
最新版本已经是hibernate5了,我们这里学习hibernate4。
百度云下载:http://pan.baidu.com/s/1i531wjV
密码:j7rw
核心jar包:

mysql驱动包:

百度云下载 :http://pan.baidu.com/s/1qXG4ec8
密码:q0pm
第二节:Hibernate4 版Hello World 实现
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/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/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
Student.java
package com.wishwzp.model;
public class Student {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id> <property name="name"></property>
</class> </hibernate-mapping>
StudentTest.java
package com.wishwzp.service; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry; import com.wishwzp.model.Student; public class StudentTest { public static void main(String[] args) { // 实例化配置文件
Configuration configuration=new Configuration().configure();
// 实例化服务登记
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
// 获取Session工厂
SessionFactory sessionFactory=configuration.buildSessionFactory(serviceRegistry);
// 生成一个session
Session session=sessionFactory.openSession();
// 开启事务
session.beginTransaction(); Student s=new Student();
s.setName("张三");
session.save(s); // 提交事务
session.getTransaction().commit();
// 关闭session
session.close();
// 关闭session工厂
sessionFactory.close();
}
}
结果显示:


控制台显示:

(一)问候Hibernate4的更多相关文章
- spring hibernate4 c3p0连接池配置
c3p0-0.9.1.2.jar,c3p0-oracle-thin-extras-0.9.1.2.jar,点此下载 <bean id="dataSource" class=& ...
- Hibernate4.0之HibernateSessionFactory源码详解
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Conf ...
- [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合
原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版 ...
- 基于Spring4+Hibernate4的通用数据访问层+业务逻辑层(Dao层+Service层)设计与实现!
基于泛型的依赖注入.当我们的项目中有很多的Model时,相应的Dao(DaoImpl),Service(ServiceImpl)也会增多. 而我们对这些Model的操作很多都是类似的,下面是我举出的一 ...
- ssh中org.springframework.orm.hibernate4.support.OpenSessionInViewFilter的作用及配置
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 是Spring为我们解决Hibernate的Session的关闭 ...
- 基于Spring4+SpringMVC4+Mybatis3+Hibernate4+Junit4框架构建高性能企业级的部标GPS监控平台
开发企业级的部标GPS监控平台,投入的开发力量很大,开发周期也很长,选择主流的开发语言以及成熟的开源技术框架来构建基础平台,是最恰当不过的事情,在设计之初就避免掉了技术选型的风险,避免以后在开发过程中 ...
- Hibernate3 和Hibernate4 在配置文件上的区别
在使用hibernate之前要首先对hibernate进行一些基础的配置信息,像映射文件XXX.hbm.xml XXX代表当前的domain的模型类名 <?xml version=" ...
- spring4+hibernate4+maven环境搭建
本文主要介绍利用maven搭建spring4+hibernate4开发环境. 首先我们创建一个maven项目,具体步骤就不详细介绍了,看看我们pom.xml文件 <project xmlns=& ...
- Spring3 整合 Hibernate4实现数据库操作(1)
Hibernate知识学习:http://justsee.iteye.com/blog/1061576 注意Hibernate4在开发当中的一些改变 :http://snake-hand.iteye ...
随机推荐
- 查询显示MSSQL表结构 [转]
SELECT 表名 = Case When A.colorder= Then D.name Else '' End, 表说明 = Case When A.colorder= Then isnull(F ...
- delphi 提取字符中的数字
Function Setstring(cString:string):string; {提取数字} VAr i:integer; str:string; begin str:='' ...
- CF_402C Searching for Graph 乱搞题
题目链接:http://codeforces.com/problemset/problem/402/C /**算法分析: 乱搞题,不明白题目想考什么 */ #include<bits/stdc+ ...
- [三]SpringMvc学习-封装、乱码问题、重定向、转发
1.对象属性自动封装 前台input 用对象的属性名,后台自动会封装为对象,类似struts 2.解决post乱码问题 在web.xml中配置过滤器 <filter> <filter ...
- [html]js打开指定页面
1.在当前窗口打开 location.href = "http://www.baidu.com"; 2.可以设置开发方式 window.open("http://www. ...
- 引入less报错解决方法以及浏览器设计不同的地方
XMLHttpRequest cannot load file:///C:/Users/PAXST/Desktop/805/first.less. Cross origin requests are ...
- Xcode7 低版本iOS系统上下有黑边的问题
在使用Xcode7开发时,默认的启动页改成了 Launch Screen storyboard.通常情况下还是习惯使用 LaunchImage,介绍下Xcode7 下如何改为启动页是LaunchIma ...
- AMQP与QPID简介
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- node.js在windows下的学习笔记(2)---简单熟悉一些命令
1.打开如下的安装 2.输入node -v,显示node的版本号 3.输入node --help.显示帮助命令 4.在命令行中输入node,按下回车键,当出现>符号的时候即进入了node的REP ...
- java16 程序、进程、线程
一.程序.进程.线程 .程序:程序就是我们编写的代码,也就是指令集,是一个静态概念. .进程:操作系统调度我们的程序,动态概念,一个程序就是一个进程.进程是程序的一次动态执行过程,占用特定的地址空间, ...