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>
hibernate 学习的更多相关文章
- Hibernate学习之——搭建log4j日志环境
		
昨天讲了Hibernate开发环境的搭建以及实现一个Hibernate的基础示例,但是你会发现运行输出只有sql语句,很多输出信息都看不见.这是因为用到的是slf4j-nop-1.6.1.jar的实现 ...
 - Hibernate学习笔记(二)
		
2016/4/22 23:19:44 Hibernate学习笔记(二) 1.1 Hibernate的持久化类状态 1.1.1 Hibernate的持久化类状态 持久化:就是一个实体类与数据库表建立了映 ...
 - Hibernate学习笔记(一)
		
2016/4/18 19:58:58 Hibernate学习笔记(一) 1.Hibernate框架的概述: 就是一个持久层的ORM框架. ORM:对象关系映射.将Java中实体对象与关系型数据库中表建 ...
 - Hibernate 学习笔记一
		
Hibernate 学习笔记一 今天学习了hibernate的一点入门知识,主要是配置domain对象和表的关系映射,hibernate的一些常用的配置,以及对应的一个向数据库插入数据的小例子.期间碰 ...
 - Hibernate学习笔记-Hibernate HQL查询
		
Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库 ...
 - 我的hibernate学习记录(二)
		
通过上一篇文章我的hibernate学习记录(一)基本上的入门了hibernate,但是,里面还有还多东西是通过迷迷糊糊的记忆,或者说copy直接弄进去的,所以这篇文章就需要对上篇的一些文件.对象进行 ...
 - Hibernate学习(二)关系映射----基于外键的单向一对一
		
事实上,单向1-1与N-1的实质是相同的,1-1是N-1的特例,单向1-1与N-1的映射配置也非常相似.只需要将原来的many-to-one元素增加unique="true"属性, ...
 - Hibernate学习一:Hibernate注解CascadeType
		
http://zy19982004.iteye.com/blog/1721846 ———————————————————————————————————————————————————————— Hi ...
 - Hibernate学习---缓存机制
		
前言:这些天学习效率比较慢,可能是手头的事情比较多,所以学习进度比较慢. 在之前的Hibernate学习中,我们无论是CURD,对单表查询还是检索优化,我们好像都离不开session,session我 ...
 - hibernate学习系列-----(2)hibernate核心接口和工作机制
		
在上一篇文章hibernate学习系列-----(1)开发环境搭建中,大致总结了hibernate的开发环境的搭建步骤,今天,我们继续了解有关hibernate的知识,先说说这篇文章的主要内容吧: C ...
 
随机推荐
- pycharm 虚拟环境virtualenv迁移到别的机器 无法读取包的问题
			
将virtualenv迁移到别的机器时,发现pycharm 总是无法读取目录下所在的包,后来经过实验终于找到了问题所在: 将自己所建的虚拟环境目录下的orig-prefix.txt中保存的路径,改成新 ...
 - 排错:expected unqualified-id before string constant
			
一个低级但是不好定位的编译错误,常见的问题是: 1. 语句的 { 括号不匹配. 2. 缺少 : , 特别是类的定义或声明,枚举的定义. 3. 变量名或函数名使用了保留字.
 - 106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树
			
给定一棵树的中序遍历与后序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出中序遍历 = [9,3,15,20,7]后序遍历 = [9,15,7,20,3]返回如下的二叉树: ...
 - selenium登录 京东滑动验证码
			
京东的滑动验证码在页面上是没有原图的,所有我是用ps把他们拼成一个的. from selenium import webdriver from selenium.webdriver import Ac ...
 - awk一些简单命令
			
最简单地说, AWK 是一种用于处理文本的编程语言工具.AWK 在很多方面类似于 shell 编程语言,尽管 AWK 具有完全属于其本身的语法. 尽管操作可能会很复杂,但命令的语法始终是: awk ' ...
 - 我的NopCommerce之旅(9): 编写Plugin实例
			
一.基础介绍 ——In computing, a plug-in (or plugin) is a set of software components that add specific abili ...
 - [转]nopcommerce之权限模块
			
本文转自:http://www.nopchina.net/category/%E6%9E%B6%E6%9E%84.html 这篇文章简单介绍一下nopcommerce的权限模块,nopcommerce ...
 - CodeSmith Generator 7.0.2
			
[工具]CodeSmith Generator 7.0.2激活步骤 只看楼主 收藏 回复 M炎骫毒逆天T c#攻城狮 8 学过三层的人应该认识CodeSmith Generator吧, ...
 - SQL SERVER 2008 系列问题:无法访问,混合模式
			
转载请注明:http://www.cnblogs.com/dachen408/p/7878494.html 使用本机服务器名'.'登录,使用windows模式: 1.修改登录模式为混合模式:右键服务器 ...
 - red5 重新分配 ip
			
root@hett-OptiPlex-7040:~# ll /usr/local/src/red5/conf/total 144drwxr-xr-x 2 root root 4096 1月 9 ...