1.新建项目
2.New User Library
a.Window--->Preferences--->Java--->Build Path--->User Library--->点击New,输入hibernate3.3;
b.Add JARs
hibernate-distribution-3.3.2.GA\hibernate3.jar;
hibernate-distribution-3.3.2.GA\lib\required下所有jar;
slf4j-1.5.8\slf4j-nop-1.5.8.jar。
c.选择工程--->new--->Build Path--->Add Library--->User Library,next--->选中hibernate3.3,Finish。
3.导入数据库驱动包
mysql-connector-java-5.0.8-bin.jar
4.建立数据库及表
show databases;
create database hibernate;
create table student(
id int primary key,
name varchar(20),
age int,
birthday date
);
5.在src建立hibernate.cfg.xml
<?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"></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">create</property>
<mapping resource="com/fengyun/shopping/entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6.包com.fengyun.shopping.entity下建立Student类
package com.fengyun.shopping.entity;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
super();
}
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
<?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>
<class name="com.fengyun.shopping.entity.Student">
<id name="id"/>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>
8.将映射文件添加到hibernate.cfg.xml中
<mapping resource="com/fengyun/shopping/entity/Student.hbm.xml"/>
9.测试
package com.fengyun.shopping.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("wanghao");
s.setAge(12);
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.建议
启动Hibernate,此过程包括创建一个全局的SessoinFactory,并把它储存在应用程序代码容易访问的地方。SessionFactory可以创建并打开新的Session。一个Session代表一个单线程的单元操作,SessionFactory则是个线程安全的全局对象,只需要被实例化一次。
我们将创建一个HibernateUtil辅助类(helper class)来负责启动Hibernate和更方便地操作SessionFactory
package util;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
分享到:
相关推荐
Hibernate入门到精通,分享Hibernate的干货,带你入门,走向精通。
解压后包含hibernate入门所需jar (hibernate常用jar,mysql驱动,Junit4jar)
Hibernate入门案例,提供MyEclipse和Eclipse的项目源文件以及war文件
hibernate入门
Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门...
总共分5部分:1和2为hibernate入门,3为hibernate实体映射,4为hibernate复合主键,5为hibernate实体层设计.
hibernate入门小例子,采用mysql(sql-front)数据库
Hibernate入门教程.pdf
hibernate 入门详细操作步骤 包括hibernate包,实例实现文档····
这是一个关于hibernate入门到精通的书,希望可以帮到你
其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持。然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西,看了这个希望能给你点启示!
struts+hibernate入门实例(eclipse版) author:朱千平 phone:13522080786 qq:200896066
hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例
hibernate hibernate入门 hibernate学习 hibernate基础
hibernate入门教程 介绍如何引入Hibernate和搭建
Hibernate入门 - 基础配置,Hibernate入门 - 基础配置
NULL 博文链接:https://moshow.iteye.com/blog/1587008