第一步:建立一个普通的java工程
第二步:选定工程,右击 Myeclipse--->Add Hibernate Annotation...导入Hibernate相关的包,及数据库驱动包,我用的是oracle11g,驱动程序包为:ojdbc6.jar
第三步:自定义两个包,示例:cn.com.dao 和 cn.com.pojo,在cn.com.pojo包下建立UserPojo.java类,在cn.com.dao目录下建立UserDao.java类,代码分别如下:
package cn.com.pojo;
import java.io.Serializable;
public class UserPojo implements Serializable{
private int userId;
private String userName;
private int age;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package cn.com.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.com.pojo.UserPojo;
public class UserDao {
private static SessionFactory sf = null;
static {
// 读取hibernate.cfg.xml
Configuration config = new Configuration().configure();
// 创建SessionFactory
sf = config.buildSessionFactory();
}
/**
* 添加用户方法
*/
public static void addUser() {
Session session = null;
// 事务处理
Transaction tr = null;
try {
session = sf.openSession();
tr = session.beginTransaction();
UserPojo user = new UserPojo();
user.setAge(21);
user.setUserName("lili");
session.save(user);
tr.commit();
System.out.println("添加成功!");
} catch (Exception e) {
System.out.println("添加失败!");
tr.rollback();
} finally {
session.close();
}
}
/**
* 修改信息方法
*/
public static void updateUser() {
Session session = null;
Transaction tr = null;
try {
session = sf.openSession();
tr = session.beginTransaction();
UserPojo user = (UserPojo) session.get(UserPojo.class, 1);
user.setAge(12);
session.save(user);
tr.commit();
System.out.println("修改成功!");
} catch (Exception e) {
tr.rollback();
System.out.println("修改失败!");
} finally {
session.close();
}
}
/**
* 删除用户方法
*/
public static void deletUser() {
Session session = null;
Transaction tr = null;
try {
session = sf.openSession();
tr = session.beginTransaction();
UserPojo user = (UserPojo) session.get(UserPojo.class, 2);
session.delete(user);
tr.commit();
System.out.println("删除成功!");
} catch (Exception e) {
tr.rollback();
System.out.println("删除失败!");
} finally {
session.close();
}
}
/**
* 查询用户
*/
public static void queryUser() {
Session session = null;
try {
session = sf.openSession();
UserPojo user = (UserPojo) session.get(UserPojo.class, 1);
System.out.println(user.getUserName());
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// addUser();
// updateUser();
// deletUser();
queryUser();
}
}
注意:UserPojo.java类要实现序列化接口Serializable
第四步:在cn.com.pojo目录下建立UserPojo.hbm.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- 建立类名到表名的映射关系 -->
<class name="cn.com.pojo.UserPojo" table="h_user">
<!-- 主键字段的映射 -->
<id name="userId" column="userid" type="int">
<!-- 指定主键生成策略 -->
<generator class="increment"></generator>
</id>
<!-- 普通字段映射 -->
<property name="userName" column="username" type="string"></property>
<property name="age" column="userage" type="int"></property>
</class>
</hibernate-mapping>
第五步:创建跟UserPojo.java类对应的数据库表,表名为:h_user 字段分别为:userid username userage
第六步:建立java对象跟数据库的关联,对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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 配置方言为Oracle,告诉Hibernate底层生成的sql语句符合Oracle数据库规范 -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- 配置数据库 -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:oracle</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<!-- 配置show_sql为true,在控制台打印出Hibernate生成的sql语句 -->
<property name="show_sql">true</property>
<mapping resource="cn/com/pojo/UserPojo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
到此,工程完成,工程结构示意图如下:
此时,转到UserDao.java类下,main函数处做测试吧!!!