环境准备
  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的更多相关文章

  1. HIbernate学习笔记(一) 了解hibernate并搭建环境建立第一个hello world程序

    Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了轻量级的封装,Java程序员可以使用面向对象的编程思维来操纵数据库,它通过对象属性和数据库表字段之间的映射关系,将对象 ...

随机推荐

  1. springboot整合dubbo注解方式(四)

    在笔者换jar包时候出现问题: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/zook ...

  2. Python自动化开发学习20-Django的form组件

    武沛齐老师的Django的FORM组件:http://www.cnblogs.com/wupeiqi/articles/6144178.html 转自:http://blog.51cto.com/st ...

  3. Django工程创建

    方法一: 1.win+r进入cmd命令窗口: 2.找到Django的安装地址: 3.cmd窗口中利用cd 进入相应的文件夹,再输入命令如下: django-admin.exe startproject ...

  4. struts1学习

    转载:https://blog.csdn.net/toyouheart/article/details/4509466

  5. java——哈希表 HashTable

    在一个类中重写hashCode()和equals() package Date_pacage.hash; public class Student { private int grade; priva ...

  6. Vue.js-----轻量高效的MVVM框架(八、使用组件)

    什么是组件? 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能.在有 ...

  7. 关于vue路由嵌套遇到的坑~

    关键在于子路由中的path问题,path之前不要放/ <!DOCTYPE html> <html lang="en"> <head> <m ...

  8. JedisCluster 链接redis集群

    先贴代码: <!-- redis客户端 --><dependency>  <groupId>redis.clients</groupId>  <a ...

  9. RTT设备驱动之看门狗

    看门狗的喂狗一般放在空闲任务钩子函数里面. rt_thread_idle_sethook(idle_hook); static void idle_hook(void) { /* 在空闲线程的回调函数 ...

  10. Unity 去除场景中的雾效果

    Windows——Lighting——Setting,然后出现下面窗口,把Other Setting下,Fog的对勾去掉就可以了.