hibernate_HelloWorld
环境准备
1、下载 hibernate 3.3.2;
2、下载 hibernate 3.4.0;
3、注意阅读 hibernate compatibility matrix(hibernate 网站,download),hibernate 3.5以后注解包和核心包打包在一起就可以不用管这条了;
4、下载 slf1.5.8。
下载链接: http://pan.baidu.com/s/1qYjmu5m 密码: pn25

第一个hibernate应用程序
1、建立新的 java 项目,名为:hibernate_0100_HelloWorld
2、学习建立 User-library - hibernate,并加入相应的jar包
a) 项目右键-build path-configure build path-add library
b) 选择User-library,在其中新建library,命名为hibernate
详细步骤:http://www.cnblogs.com/ShawnYang/p/6691935.html
c) 在该library中加入 hibernate 所需 jar 包
i. hibernate core
ii./required
iii.slfnop jar



3、引入 mysql 的 JDBC 驱动包
4、在 mysql中建立对应的数据库以及表
a) create database hibernate;
b) user hibernate;
c) create table Student(id int primary key,name varchar(20),age int);
5、建立 hibernate 配置文件 hibernate.cfg.xml
a) 从参考文档中copy
b) 修改对应的数据库连接
c) 注释掉暂时用不上的内容
<?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> <!-- Database connection settings -->
<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">root</property> <!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
6、建立 Student 类
package com.bjsxt.hibernate.model;
public class Student {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
7、建立 Student 映射文件 Student.hbm.xml
a)参考文档
<?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 package="com.bjsxt.hibernate.model">
<class name="Student" table="student">
<id name="id" column="id"/>
<property name="name" column="name"/>
<property name="age" column="age"/>
</class>
</hibernate-mapping>
8、将映射文件加入到 hibernate.cfg.xml中
a) 参考文档
<mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>
9、写测试类Main,在Main中对Student对象进行直接的存储测试
a) 参考文档
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.bjsxt.hibernate.model.Student; public class StudentTest { public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(1); Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction(); session.save(s); session.getTransaction().commit();
session.close();
sf.close();
} }
10、FAQ:
a) 要调用 new Configuration().configure().buildSessionFactory(),而不是省略configure,
否则会出 hibernate hibernate dialect must be sef 的异常。
11、Note:
a) 请务必建立自己动手查文档的能力
b) 重要的是
i.要建立自己动手查一手文档的信心;
ii.还有建立自己动手查一手文档的习惯;
iii.主动学习,放弃被动接收灌输的习惯。
链接: http://pan.baidu.com/s/1mi0VuPi 密码: 8dyv
示例项目所需jar包链接: http://pan.baidu.com/s/1qY6SRQ8 密码: vzpm
hibernate_HelloWorld的更多相关文章
- HIbernate学习笔记(一) 了解hibernate并搭建环境建立第一个hello world程序
Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了轻量级的封装,Java程序员可以使用面向对象的编程思维来操纵数据库,它通过对象属性和数据库表字段之间的映射关系,将对象 ...
随机推荐
- javascript的offset、client、scroll、screen使用方法
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAHuCAYAAABpm/53AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw
- 用勤哲excel服务器开发旅行社管理软件
做这个旅行社管理软件之前,旅行社给我的印象就是“拉客”与“接客”,业务模式应该比较简单.但做起这样一个旅行社管理软件,才发现麻雀虽小.五脏俱全,一个旅行社的运作,牵扯到的方方面面远远超出自己之前的理解 ...
- VS2010 简单ATL COM开发
http://blog.csdn.net/wangwenjing90/article/details/8771934#reply http://blog.csdn.net/wangwenjing90/ ...
- Jira的安装使用
1.什么是JIRA JIRA是目前比较流行的基于Java架构的管理系统,由于Atlassian公司对很多开源项目实行免费提供缺陷跟踪服务,因此在开源领域,其认知度比其他的产品要高得多,而且易用性也好一 ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_new新建对象
CLR使用 new 操作符来创建新对象,例如:Employee e=new Employee("Param1"); 以下是 new 操作符所做的事情. 它计算类型及其所有基类型 ...
- 关于box-shadow、border-radius不兼容ie8的解决办法
本来从css3兼容ie9+挺好的,可是总有一些共识要求ie8+,于是就有了我们的苦逼的找解决办法.之前在网上查到一些说用 PIE.htc. But 我就是按照他说的写的没有管用.请教了一下别人才会写了 ...
- 转 Python 操作 MySQL 数据库
#########http://www.runoob.com/python/python-mysql.html Python 标准数据库接口为 Python DB-API,Python DB-API为 ...
- swagger2使用日志
------------------------------------------------------------------------------------ 安装.配置.使用 参考:htt ...
- JqGrid查询数据为空时给表格添加提示信息
在JqGrid的loadComplete事件中添加下面的代码就可以实现上图的效果 loadComplete: function () { var rowNum = $("#purchaser ...
- python面向对象实例
——王宇阳 总结 (Code_boy)2018年11月16日 class Person: name='xxx' age=20 p=Person() #p为实例对象 print(p.name,p.age ...