这段时间做一个项目,用到了hibernate进行持久层操作,因为是自己学,在配置的过程中遇到很多问题,所以写一下自己来复习一下:
第一步:准备jar包:http://download.csdn.net/download/xiaoshusheng10/10041963这是准备对hibernate的jar包
第二步:
将jar包复制到webINF/lib目录下,如果是java工程,则添加到引用包里面
第三步:在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>
<property name="connection.driver_class"><!-- 配置数据库的驱动类 -->
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url"><!-- 配置数据库的连接路径 -->
jdbc:oracle:thin:@127.0.0.1:1521:ORCL
</property>
<property name="connection.username">username</property><!-- 配置数据库的连接用户名 -->
<property name="connection.password">password</property><!-- 配置数据库的连接密码,这里密码为空,在这种情况下也可以省略该行配置代码 -->
<property name="dialect"><!-- 配置数据库使用的方言 -->
org.hibernate.dialect.OracleDialect
</property>
<property name="show_sql">true</property><!-- 配置在控制台显示SQL语句 -->
<property name="format_sql">true</property><!-- 配置对输出的SQL语句进行格式化 -->
<property name="use_sql_comments">true</property><!-- 配置在输出的SQL语句前面添加提示信息 -->
</session-factory>
</hibernate-configuration>
第四步:创建数据库连接工厂类:public class HibernateSessionFactory {
private static SessionFactory sessionFactory; //工厂实体
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); //存放数据库session的连接
static {
try {
Configuration config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
} catch (Exception e) {
System.out.println("------在加载Hibernate配置文件时抛出异常,内容如下:");
e.printStackTrace();
}
}
public static Session getSession() {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null || session.isOpen()) {
session.close();
}
}
第五步:创建数据库操作类
public class BaseDao {
// query
protected Object queryObject(String hql) {
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);
Object object = query.uniqueResult();
return object;
}
protected List queryList(String hql) {
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);
List list = query.list();
return list;
}
//
public static void main(String[] args) {
BaseDao dao = new BaseDao();
dao.queryList("from TbAccessionForm");
}
// filter
public List filterSet(Set set, String hql) {
Session session = HibernateSessionFactory.getSession();
Query query = session.createFilter(set, hql);
List list = query.list();
return list;
}
// save
public boolean saveObject(Object obj) {
boolean isSave = true;
Session session = HibernateSessionFactory.getSession();
Transaction tr = session.beginTransaction();
try {
session.save(obj);
tr.commit();
} catch (HibernateException e) {
isSave = false;
e.printStackTrace();
}
return isSave;
}
// update
public boolean updateObject(Object obj) {
boolean isUpdate = true;
Session session = HibernateSessionFactory.getSession();
Transaction tr = session.beginTransaction();
try {
session.update(obj);
tr.commit();
} catch (HibernateException e) {
isUpdate = false;
tr.rollback();
e.printStackTrace();
}
return isUpdate;
}
// saveOrUpdate
public boolean saveOrUpdateObject(Object obj) {
boolean isUpdate = true;
Session session = HibernateSessionFactory.getSession();
Transaction tr = session.beginTransaction();
try {
session.saveOrUpdate(obj);
tr.commit();
} catch (HibernateException e) {
isUpdate = false;
tr.rollback();
e.printStackTrace();
}
return isUpdate;
}
// delete
public boolean deleteObject(Object obj) {
boolean isDelete = true;
Session session = HibernateSessionFactory.getSession();
Transaction tr = session.beginTransaction();
try {
session.delete(obj);
tr.commit();
} catch (HibernateException e) {
isDelete = false;
tr.rollback();
e.printStackTrace();
}
return isDelete;
}
// deleteOfBatch
public boolean deleteOfBatch(String hql) {
boolean isDelete = true;
Session session = HibernateSessionFactory.getSession();
Transaction tr = session.beginTransaction();
try {
Query query = session.createQuery(hql);
query.executeUpdate();
tr.commit();
} catch (HibernateException e) {
isDelete = false;
tr.rollback();
e.printStackTrace();
}
return isDelete;
}
}
}
第六步创建数据库表:如学生信息表:使用hibernate创建的表格一定以id int为主键
create table student(id int primary key,name varchar(20));
第七步创建bean类:
public class Student {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
第八步:创建映射文件:Student.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="com.fei.Student" table="student">
<id name="id" type="java.lang.Integer"><!-- Student对应的属性名(实体域),类型 -->
<column name="id">
</column>
<generator class="increment"/><!-- 自增 -->
</id>
<property name="name" type="java.lang.String">
<column name="name"/>
</property>
</class>
</hibernate-mapping>