Hibernate使用

---------------Hibernate3.0 配置--------------
1.Hibernate中配置参数
/**
* 注意:HQL中使用参数的方法:
* 1.根据参数名称来设置参数:匹配名称;
* 2.根据参数位置来设置参数:匹配位置;
*/
//根据参数名称来设置参数
Query query = session.createQuery("from UserManager u where u.userName = :uname and u.password = :upsw");
query.setString("uname", uName);
query.setString("upsw", psw);
   
//根据参数位置设置参数,注意起始值为0
Query query = session.createQuery("from UserManager u where u.userName = ? and u.password = ?");
query.setString(0, uName);
query.setString(1, psw);

2.通过Hibernate执行SQL
2.1 创建session
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();

2.2 创建sql
String sql = "select * from user_manager t where t.user_id = "+userId;

2.3 取得Connection
Connection conn = session.connection();   //从session中取得数据库中的配置;数据库中的配置放置在sessionFactory中;
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();

2.4 执行剩余数据操作
剩余的操作与采用直接连接数据的操作方式一致;

3.通过HibernateSessionFactory获取Session
3.1 添加Hibernate框架时候添加HibernateSessionFactory,放在struts目录下;
3.2 在程序使用的地方直接使用:
Session session = HibernateSessionFactory.getSession();
...
注意:Session变量不再使用:
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();

推荐使用HibernateSessionFactory来获取Session实例,系统专用资源较少;

4.显示SQL
4.1 在Struts+Hibernate环境
修改src/hibernate.cfg.xml中的
<hibernate-configuration>
        <session-factory>
                ...
  <property name="hibernate.show_sql">true</property>   //显示SQL
                ...

4.2 在Struts+Spring+Hibernate环境
修改/WEB-INF/applicationContext.xml中内容:
<bean id="sf"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="ds" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.Oracle9Dialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>  //显示SQL
   </props>

5 Hibernante事务管理

5.1 在DAO中使用Transaction(事务管理)
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
Transaction tran = session.beginTransaction();
try {
 session.save(userManager);
 tran.commit();
} catch (RuntimeException e) {
 this.printError("insert user is error");
 tran.rollback();
 e.printStackTrace();
}

5.2 JDBC 事务管理

5.2.1 取得连接
conn = ConnectOracleBase.getConnection(this.DBUSER, this.DBPSW, this.DBSERVER);

5.2.2 设置Connection的自动提交模式为false(手动提交)
conn.setAutoCommit(false); //这一部非常重要,不设置JDBC transaction不起作用;

5.2.3 进行相关操作,使用SQLException捕获异常,在异常部分进行回滚操作;
try{
  ...
  ...//sql

  conn.commit();  //完成进行提交
}  catch (SQLException e) {
 System.out.println("commit is error!");
 if(conn != null){
  conn.rollback();   //回滚操作;
 }
 e.printStackTrace();
} finally{
 ConnectOracleBase.closeAllConnection(ps, conn);
}

5.3 Hibernate的事务管理雷同5.1 ;

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值