首先我们先建立整体的项目组织结构,分包
①com.web.bean 存放实体类
②com.web.dao 用于与数据库进行交互
③com.web.hbm 存放映射文件
④com.web.util 存放工具包
⑤com.web.test 测试类
我们还是以oracle数据库的dept表为例
1、bean层有Dept类
package com.web.bean;
import java.io.Serializable;
//序列化+不是final类
public class Dept implements Serializable {
//属性名小写
private Integer deptno;
private String dname;
private Integer loc;
//有无参构造方法
public Dept() {
}
public Dept(Integer deptno, String dname, Integer loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
//getter//setter方法
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public Integer getLoc() {
return loc;
}
public void setLoc(Integer loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc
+ "]";
}
}
在dao层中
package com.web.dao;
import com.web.bean.Dept;
import com.web.util.HibernateBaseDao;
public class DeptDAO extends HibernateBaseDao<Dept>{
}
在hbm层中
<?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.web.bean.Dept" table="dept">
<id name="deptno" column="deptno">
<generator class="assigned"></generator>
</id>
<property name="dname" column="dname"></property>
<property name="loc" column="loc"></property>
</class>
</hibernate-mapping>
在util中
HibernateSessionFactory
package com.web.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static Session session = null;
static SessionFactory sessionFactory = null;
static{
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
}
public static Session getSession(){
return session = sessionFactory.openSession();
}
}
HibernateDao
package com.web.util;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class HibernateBaseDao<T> {
/**
*zeng
* @param t
*/
public void save(T t){
Session session = HibernateSessionFactory.getSession();
Transaction transaction = session.beginTransaction();
try {
session.save(t);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}finally{
session.close();
}
}
/**
* shan
*/
public void delete(T t){
Session session = HibernateSessionFactory.getSession();
Transaction transaction = session.beginTransaction();
try {
session.delete(t);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}finally{
session.close();
}
}
//gai
public void update(T t){
Session session = HibernateSessionFactory.getSession();
Transaction transaction = session.beginTransaction();
try {
session.update(t);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}finally{
session.close();
}
}
//cha
public T get(Class c,Serializable id){
Session session = HibernateSessionFactory.getSession();
T t = null;
try {
t = (T) session.get(c,id);
} catch (Exception e) {
e.printStackTrace();
}finally{
session.close();
}
return t;
}
// fan hui jihe
public List<T> list(Class c){
Session session = HibernateSessionFactory.getSession();
List<T> t = null;
try {
t = session.createCriteria(c).list();
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
public List<T> list(String hql){
Session session = HibernateSessionFactory.getSession();
List<T> list = session.createQuery(hql).list();
return list;
}
}
test
package com.web.test;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.web.bean.Dept;
import com.web.bean.Emp;
import com.web.bean.Product;
import com.web.dao.DeptDAO;
import com.web.dao.EmpDAO;
import com.web.dao.ProductDAO;
import com.web.util.HibernateSessionFactory;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
List<Object[]> list = HibernateSessionFactory.getSession().createQuery("select dname from Dept").list();
for(Object[] c:list){
System.out.println(c[0]);
}
}
}