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程序员可以使用面向对象的编程思维来操纵数据库,它通过对象属性和数据库表字段之间的映射关系,将对象 ...
随机推荐
- 百度地图 js 调用
百度地图key 的获取 进入 http://lbsyun.baidu.com/i 登录你的账号 点击进入控制台,复制 ak 的值 <!doctype html> <html> ...
- Python的安装位置与Python库
如何查看Python的安装位置: 输入 where python ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- P1060 开心的金明(动态规划背包问题)
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NN元钱 ...
- poj2513连接木棍(字典树+欧拉回路+并查集)
题目传送门 题目大意:给你一堆木棍,每根木管都有两种颜色,相同颜色的部分可以连接起来,问你这堆木棍可不可以连接成1根. 思路:大致的思路很好想,就是判断欧拉回路的方法(1.联通,2,要么顶点读书全为偶 ...
- POJ:2976 Dropping tests(二分+最大化平均值)
Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...
- 原 tomcat的server.xml配置文件中三个端口的作用
以Tomcat7.0为例, 在安装目录下. conf/server.xml 中可以配置三个端口号, 如果使用多个tomcat 是需要配置这三个. 该Connector 用于监听请求. protocol ...
- my01_Mysql router 安装
Mysql router 主要用途是读写分离,主主故障自动切换,负载均衡,连接池等.安装如下 下载地址:https://dev.mysql.com/downloads/router/ tar -zxv ...
- nginx + uwsgi 配置参考
参考 http://www.runoob.com/django/django-nginx-uwsgi.html ####### 20181029 cd ~wget http://python.org/ ...
- 前端技巧-w3c
1.使用全等===比较符 if (zeroAsAString === 0) { // 判断为false }在和null进行比较的时候,允许使用 == 比较符 2.使用 .parseInt() 的时候, ...
- VC6.0开发中一些链接错误的解决方法
(1)error LNK2001: unresolved external symbol _main 编号:LNK2001 直译:未解决的外部符号:_main. 错误分析:缺少main函数.看看mai ...