hibernate 学习

  hibernate.cg.xml 

可以通过myeclipse自动生成,添加数据库信息;

<?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="dialect">
            org.hibernate.dialect.MySQLDialect</property>        <!-- 数据库方言 -->
        <property name="connection.url">
            jdbc:mysql://localhost:3306/db_examsystem</property><!-- 数据库连接URL -->
        <property name="connection.username">***</property>    <!-- 数据库用户名 -->
        <property name="connection.password">***</property>    <!-- 数据库用户密码 -->
        <property name="connection.driver_class">                <!-- 数据库驱动类 -->
            com.mysql.jdbc.Driver</property>
        <mapping resource="com/sanqing/po/Student.hbm.xml"/>
        <mapping resource="com/sanqing/po/Teacher.hbm.xml"/>
        <mapping resource="com/sanqing/po/Subject.hbm.xml"/>    
    </session-factory>
</hibernate-configuration>

HibernateSessionFactory.java   也可以通过myeclipde自动生成,主要为设置和获得配置文件信息、获得Session和建立SessionFactory的方法类封装

DAO方法

举例 SubjectDAO.java同servlet

SubjectDAOImpl.java 存取数据库差别,只是以Hibernate(觉得是把JDBC封装,以对象方式存取数据)

    public void addSubject(Subject subject) {
        Session session = HibernateSessionFactory.getSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(subject);
            transaction.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            transaction.rollback();
        }
        HibernateSessionFactory.closeSession();
    }
    public Subject findSubjectByTitle(String title) {
        Session session = HibernateSessionFactory.getSession();
        Query query = session.createQuery("from Subject as sub where sub.subjectTitle=?");
        query.setString(0, title);
        List list = query.list();
        HibernateSessionFactory.closeSession();
        if(list.size() == 0) {
            return null;
        } else {
            return (Subject)list.get(0);
        }
    }
    public List<Subject> findSubjectByPage(Page page) {
        Session session = HibernateSessionFactory.getSession();
        Query query = session.createQuery("from subject");
        query.setMaxResults(page.getEveryPage());
        query.setFirstResult(page.getBeginIndex());
        List list = query.list();
        HibernateSessionFactory.closeSession();
        return list;
        
    }
    public int findSubjectCount() {
        Session session = HibernateSessionFactory.getSession();
        Query query = session.createQuery("from subject");
        List list = query.list();
        int count = list.size();
        HibernateSessionFactory.closeSession();
        return count;
    }
    public Subject findSubjectByID(int subjectID) {
        Session session = HibernateSessionFactory.getSession();
        Subject subject = (Subject) session.get(Subject.class, subjectID);
        HibernateSessionFactory.closeSession();
        return subject;
        
    }
    public void uppdateSubject(Subject subject) {
        Session session = HibernateSessionFactory.getSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(subject);
            transaction.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            transaction.rollback();
        }
        HibernateSessionFactory.closeSession();
    }
    public void deleteSubject(int subjectID) {
        Session session = HibernateSessionFactory.getSession();
        Transaction transaction = null;
        Subject subject = (Subject) session.get(Subject.class, subjectID);
        try {
            transaction = session.beginTransaction();
            session.delete(subject);
            transaction.commit();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            transaction.rollback();
        }
    }

action不能直接调用dao,而要通过一个业务逻辑层

example :SubjectServiceImpl.java

<?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="dialect">        org.hibernate.dialect.MySQLDialect</property><!-- 数据库方言 -->        <property name="connection.url">        jdbc:mysql://localhost:3306/db_examsystem</property><!-- 数据库连接URL -->        <property name="connection.username">root</property><!-- 数据库用户名 -->        <property name="connection.password">123456</property><!-- 数据库用户密码 -->        <property name="connection.driver_class"><!-- 数据库驱动类 -->        com.mysql.jdbc.Driver</property>        <mapping resource="com/sanqing/po/Student.hbm.xml"/>        <mapping resource="com/sanqing/po/Teacher.hbm.xml"/>        <mapping resource="com/sanqing/po/Subject.hbm.xml"/>    </session-factory></hibernate-configuration>

原文地址:https://www.cnblogs.com/ouyangping/p/6614090.html