环境准备
  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. node mysql问题:Client does not support authentication protocol requested by server; consider upgrading MySQL client!

    node后台 mysql处理模块(版本:2.16.0) 执行connect方法时报错: Client does not support authentication protocol requeste ...

  2. kuangbin专题十六 KMP&&扩展KMP HDU3294 Girls' research

    One day, sailormoon girls are so delighted that they intend to research about palindromic strings. O ...

  3. opencv-图片合成视频

    无论视频的合成还是分解我们都需要进行解码器或者是编码器(因为视频不是一帧一帧进行存储的,而是进行过压缩编码.) import cv2 img = cv2.imread('image1.jpg') im ...

  4. django组件之中间件

    中间件的概念 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用,用不好会影响到 ...

  5. DJ

    必知必会13条 all():查询所有结果 filter(**kwargs)   过滤,取到符合条件的对象,比get方法好,找不到会返回空 get(**kwargs)      取到符合条件对象,对象有 ...

  6. linu samba服务

    关闭防火墙并且重启网络yum install samba  samba-client samba-commmon -ysystemctl start smb smbclient -L //172.25 ...

  7. day 007 深浅拷贝

    今日内容: 1.字符串操作补充: join # 遍历列表 例: lst = ['汪峰','吴君如','章子怡'] s = '*-/@'.join(lst) prints(s) 结果为汪峰*-/@吴君如 ...

  8. Apache HttpClient 4.3.6 API

    官网:http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/overview-summary.html 使用教程转载: ...

  9. 安装Office 2013 时提示找不到 Office.zh-cn\OfficeLR.cab

    今天安装office2013的时候总是过会就提示找不到OfficeLR.cab文件 在网上找了好多方法不行,后来将注册表里的office选项全部删除就可以了(HKEY_CURRENT_USER\Sof ...

  10. Android studio配置push commits

    最近更新了Android studio到3.2.1最新版本 我们代码用的是Git来管理,提交代码用Android studio自带,发现最新版本的push commits不能保存 把默认的develo ...