建立Hibernate总流程

来源:互联网 发布:网络女主播去世感动 编辑:程序博客网 时间:2024/04/30 06:45

1MyEclipseadd Hibernate Capibility

2配置好hibernate.cfg.xml文件(下页),Dialect是对不同数据库的适配器,如分页,在不同数据库的SQL语句写法不同,则通过Dialect可以翻译成对应的SQL语句。Properties中设show_sql=”true”,用于运行时输出用到的SQL语句。         

<?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>

    <property name="connection.username">root</property>

    <property name="connection.url">

           jdbc:mysql://localhost:3306/test3

    </property>

    <property name="dialect">

           org.hibernate.dialect.MySQLDialect

    </property>

    <property name="myeclipse.connection.profile">MySQL</property>

    <property name="connection.password">123456</property>

    <property name="connection.driver_class">

           com.mysql.jdbc.Driver

    </property>

    <property name="show_sql">true</property>

    <mapping resource="pojo/User.hbm.xml" /> 

</session-factory>

</hibernate-configuration>

3MyEclipse生成HibernateSessionFactory类,其初始化时加载hibernate.cfg.xml文件,用于生成线程内唯一的Session对象。

4加载数据库包。

5开启Log4J日记:复制log4j.properties文件到src目录下。用于查看hibernate的执行过程(hibernateLog4J记录日记)。无则看不到错误信息(包括hibernate的异常)。

6,使用 建立POJO对象类,再映射构造数据库表 的方式(下有详讲)。

 

1、建立POJO对象,即一普通java类,有idnameprivate对象,使用MyEclipse的生成SetterGetter方法。

2、在POJO同包内建立Pojo.hbm.xml文件,模板见下页。

3、把该hbm文件注册到hibernate.cfg.xml文件中。

4、运行TableExpore类(见下下页)建表到数据库。

 

Pojo对象写法:

public class Pojo{

       Private String id;

     Private String name;

     Public void setId(id){

                this.id=id;

     }

 

     Public String getId(){

             return this.id;

     }

 

     Public void setName(name){

this.name=name;

}

 

 Public String getName(){

             return this.name;

}

}

Pojo.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 package="pojo">

       <class name="Test" table="test">

              <id name="id">

                     <generator class="native" />

              </id>

       <property name="name"></property>

       </class>

</hibernate-mapping>

TableExpore

package hibernate;

 

import org.hibernate.cfg.Configuration;

import org.hibernate.tool.hbm2ddl.SchemaExport;

 

public class TableExpore {

    public static void main(String[] args) {

           Configuration config = new Configuration().configure();

           SchemaExport se = new SchemaExport(config);

           se.create(true, true);

    }

}

 7POJO配置文件(xxx.hbm.xml)加载到hibernate.cfg.xml

 8建立DAO文件,从而不用重复写样板式代码。

 PojoDao类的写法:

public class PojoDao<T> {

       Class      clazz;

       String    className;

       // 根据ID获取POJO,无则返回null

       public T getByPK(int id) {

              Session session = HibernateSessionFactory.getSession();

              return (T) session.createQuery(" from " + className + " where id=? ")

                            .setInteger(0, id).uniqueResult();

       }

       // 获取所有记录,按id递增顺序

       public List<T> getAllRecords() {

              Session session = HibernateSessionFactory.getSession();

              Query query = session.createQuery("from " + className);

              return query.list();

       }

       // 新增

       // hibernate中不提供自动事务提交,即下面语句必须包含在一事务中才有效

       public String add(T e) {

              Session session = HibernateSessionFactory.getSession();

              return String.valueOf(session.save(e));

       }

       // 删除,根据ID

       // 返回删除的记录条数

       public int deleteByID(int id) {

              Session session = HibernateSessionFactory.getSession();

              return session.createQuery("delete from " + className + " where id=? ")

                            .setInteger(0, id).executeUpdate();

       }

       // 删除,输入POJO(根据POJOgetID

       public void deleteByPojo(T e) {

              Session session = HibernateSessionFactory.getSession();

              session.delete(e);

}

       public void updateByPojo(T e) {

              Session session = HibernateSessionFactory.getSession();

              session.update(e);

       }

}

9Enjoy your hibernate utility……..

Hibernate调用过程

业务逻辑中调用SessionFactory,该类初始化时会找到hibernate.cfg.xml文件,从而知道数据库连接方式,且找到所有XXX.hbm.xml文件,从而知道所有POJO类所在及其用法。