spring整合hibernate

 Spring和Hibernate技术的整合
 
第一步:
 
   --创建工程,引入开发包
   (spring开发包,ojdbc.jar,dbcp连接池,hibernate框架开发包)
  
第二步:  
  
   --在src下添加applicationContext.xml
  
第三步: 
 
   --针对数据表添加实体类和映射描述文件
     (Cost类,Cost.hbm.xml)
    
第四步:   
    
   --编写CostDAO接口
  
第五步: 
  
   --编写实现类HibernateCostDAO
    (继承HibernateDaoSupport,采用HibernateTemplate的方法完成增删改查)
     save():添加
     update():更新
     delete():删除
     load(),get():按主键查询
     find():执行HQL语句,返回的都是list
    
第六步:   
    
   --在Spring容器配置HibernateCostDAO
    需要事先定义DataSource,SessionFactory组件对象,
   
    按DataSource-注入->SessionFactory-注入->HibernateCostDAO顺序建立关联

--------

备注:
 
1.Spring使用原始的session
//Session的使用
public void useSession(){
 //第一种方法
  Session session = this.getSession();
  //使用Session对象操作
  session.close();//注意释放该Session对象
  //第二种方法(推荐)
  List list = (List)this.getHibernateTemplate().execute(
    new HibernateCallback(){
     //回调函数
    public Object doInHibernate(Session session) throws HibernateException, SQLException {
          //使用Session对象操作
     Query query = session.createQuery("from Cost");
          return query.list();//该值当做template.execute方法结果返回
          }
     
     }
   );
 }
 
 
 
2.HibernateCostDAO例子:

 //查询总记录数
 public int count() {
   String hql = "select count(*) from Cost";
   List list = this.getHibernateTemplate().find(hql);
   int c = Integer.parseInt(list.get(0).toString());
   return c;
 }
 //删除
 public void delete(Cost cost) {
   this.getHibernateTemplate().delete(cost);
 }
 //查询所有记录
 public List<Cost> findAll() {
   String hql = "from Cost";
   List<Cost> list = this.getHibernateTemplate().find(hql);
   return list;
    }
 //通过id查询,两种方式
 public Cost findById(int id) {
  //Cost cost = (Cost)
  //this.getHibernateTemplate().get(Cost.class, id);
  //return cost;
  String hql = "from Cost where id=?";
  Object[] params = {id};
  List<Cost> list =
   this.getHibernateTemplate().find(hql,params);
  if(list.isEmpty()){
   return null;
  }else{
   return list.get(0);
  }
 }
 //增加
 public void save(Cost cost) {
   this.getHibernateTemplate().save(cost);
    }
 //更新
 public void update(Cost cost) {
   this.getHibernateTemplate().update(cost);
    }
 
 //Session的使用
 public void useSession(){
  //第一种方法
   Session session = this.getSession();
   //使用Session对象操作
   session.close();//注意释放该Session对象
   //第二种方法(推荐)
   List list = (List)this.getHibernateTemplate().execute(
     new HibernateCallback(){
       //回调函数
      public Object doInHibernate(Session session)
        throws HibernateException, SQLException {
       //使用Session对象操作
       Query query = session.createQuery("from Cost");
       return query.list();//该值当做template.execute方法结果返回
      }
     }
    );
    }
    }

 
 
 
3.配置例子:
 
 <!--DataSource-->
   
  <bean id="myDataSource"
   class="org.apache.commons.dbcp.BasicDataSource">
   <property name="username" value="jsd1303"></property>
   <property name="password" value="jsd1303"></property>
   <property name="driverClassName"
    value="oracle.jdbc.OracleDriver">
   </property>
   <property name="url"
    value="jdbc:oracle:thin:@172.16.9.98:1521:boson1">
   </property>
  </bean>
  
 <!--SessionFactory-->
 
  <bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <!-- 连接参数信息 -->
   <property name="dataSource" ref="myDataSource">
   </property>
   <!-- hibernate框架参数 -->
   <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">
      org.hibernate.dialect.OracleDialect
     </prop>
     <prop key="hibernate.show_sql">true</prop>
     <prop key="hibernate.format_sql">true</prop>
    </props>
   </property>
   <!-- 指定hbm.xml映射描述 -->
   <property name="mappingResources">
    <list>
     <value>com/google/entity/Cost.hbm.xml</value>
    </list>
   </property>
  </bean>

 <!--HibernateCostDAO-->

  <bean id="costDao" scope="singleton"
   class="com.google.dao.impl.HibernateCostDAO">
   <property name="sessionFactory"
    ref="sessionFactory">
   </property>
  </bean>

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值