一、使用Hibernate的3个准备和7个步骤准备1:导入Hibernate库(jar包);准备2:添加配置文件-Hibernate.cfg.xml<session-factory><property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;Database=zf</property> <property name="ername">sa</property><property name="connection.password">pwd</property><property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property> #配置数据库链接<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>#数据库方言<property name="show_sql">true</property>#设置运行时是否在控制台显示SQL语句<mapping resource="com/aptech/jb/entity/User.hbm.xml" />#映射文件,可以有多个</session-factory>准备3:添加实体类和映射文件(User.hbm.xml)类:public class User implements java.io.Serializable {//要实现Serializableprivate Integer uid;private String uname;private String upass;public User(){// 要有默认构造方法}// Getter and setter}User.hbm.xml:<?xml version="1.0"encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="er"table="userinfo"><!-- 上面class为实体类到表的映射--><!-- 下面id表示主键property表示非主键,type要表示为类类型--><id name="uid"type="ng.Integer"><column name="uid"/>列名<generator class="increment"/></id><property name="uname"type="ng.String"><column name="uname"/></property><property name="sex"type="ng.String"><column name="sex"/></property><property name="age"type="ng.Integer"><column name="age"/></property><property name="birth"type="java.util.Date"><column name="birth"/></property></class></hibernate-mapping>1、Configeration(读取Hibernate.cfg.xml)2、创建SessionFactory(创建和销毁都相当耗费资源,通常一个系统内一个数据库只创建一个)3、打开Session(类似于JDBC中的Connection)4、开始一个事物5、持久化操作6、提交事物7、关闭Sessionpublic static void main(String[] args) {Configuration conf = new Configuration().configure();//1、读取配置文件SessionFactory sf = conf.buildSessionFactory();// 2、创建SessionFactorySession session = sf.openSession();// 3、打开SessionTransaction tx = null;try{tx = session.beginTransaction();// 4、开始一个事务// 5、持久化操作User user = new User();user.setUname("Hibernate user");user.setUpass("password");session.save(user);mit();// 6、提交事务}catch(Exception e){if (null!=tx){tx.rollback();}e.printStackTrace();}finally{session.close();// 7、关闭Session}}二、使用Hibernate实现数据的加载、删除、修改根据主键加载(并没有更新数据,所以不需要进行事物控制)Session session = sf.openSession();User user = (User)session.get(User.class, id);session.close();System.out.println(user.getUname() + "," + user.getUpass()); 修改(先加载,再更新,不需要繁琐的逐字段编码)tx = session.beginTransaction();User user = this.testLoad(id);user.setUname("new Name");session.update(user);mit();删除(先加载,再删除)tx = session.beginTransaction();User user = this.testLoad(id);session.delete(user);mit();三、使用MyEclipse简化Hibernate开发1、给项目添加Hibernate支持(自动添加jar包)选中项目--菜单MyEclipse--Project Capabilities--Add Hibernate Capabilities 2、自动生成hibernate.cfg.xml配置文件3、生成实体类、映射文件四、代码的简化add方法和del、update方法的代码存在重复,可以采取什么方法精简呢1、增加一个Base接口,写入相关方法public abstract class BaseHibernateDAO {protected void add(Object item){Transaction tx = null;Session session = HibernateSessionFactory.getSession();try {tx = session.beginTransaction();session.save(item);mit();} catch (Exception e) {if(null!=tx){ tx.rollback(); }e.printStackTrace();}finally{session.close();}}// update,delete,get 方法与之类似…}2、最终的代码简化如下public class FwxxDAOHibImpl extends BaseHibernateDAO implements FwxxDAO { public FWXX get (int fwid) {super.get(FWXX.class,fwid);}public void add(FWXX fwxx) {super.add(fwxx);}public void del (int fwid) {super.del(FWXX.class, fwid);}public void update(FWXX fwxx) {super.update(fwxx);}…}。