科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道spring中对hibernate的支持是非常强大的

spring中对hibernate的支持是非常强大的

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

spring中对hibernate的支持是非常强大的,从一个简单的例子就看得出来,从这个例子中我们还将对所谓的轻量级容器做一些讨论。

作者:Imain 来源:CSDN 2008年2月29日

关键字: java Hibernate Spring

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共3页)

    该类只演示了如何增加一个Student,HibernateTemplate还封装了很多有用的方法,请查阅spring文档。 StudentManager中的sessionFactory是由spring注入的,但是StudentManager并没有对 sessionFactory做任何的处理,这是因为所有的处理都被HibernateDaoSupport.getHibernateTemplate ()封装。整个StudentManager中也看不到任何的异常处理,他们也都被基类封装了。

  最后一个步骤就是在spring中注册StudentManger,然后向它注入sessionFactory:

  1. <bean id="studentManager" class="net.bromon.spring.examer.student.StudentManager">
  2.   <property name="sessionFactory">
  3.     <ref bean="sessionFactory"/>
  4.   </property>
  5. </bean>


  所有的配置都完成了,下面做单元测试:

  1. /*
  2.  * 创建日期 2005-3-17
  3.  */
  4. package net.bromon.spring.examer.student.test;
  5. import java.io.FileInputStream;
  6. import org.springframework.beans.factory.xml.XmlBeanFactory;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. import net.bromon.spring.examer.pojo.Student;
  10. import net.bromon.spring.examer.student.StudentManager;
  11. import junit.framework.TestCase;
  12. /**
  13.  * @author Bromon
  14.  */
  15. public class TestStudentManager extends TestCase {
  16.     public void testAdd() 
  17.     {
  18.         try
  19.         {
  20.             ApplicationContext context =new ClassPathXmlApplicationContext("springConfig.xml");
  21.             
  22.             Student s=new Student();
  23.             s.setName("bromon");
  24.             s.setPassword("123");
  25.             s.setGrade(3);
  26.             s.setSex(true);
  27.             
  28.             ((StudentManager)context.getBean("studentManager")).add(s);
  29.         }catch(Exception e)
  30.         {
  31.             e.printStackTrace();
  32.         }
  33.     }
  34. }

  Spring已经将hibernate的操作简化到了非常高的程度,最关键的是整个开发可以由设计来驱动,如果一个团队对spring有足够的熟悉,那么完全可以由设计师规划所有的类,整理清楚类之间的关系,写成配置文件,然后编写hibernate映射文件,将数据表与pojo关联,成员就可以完全在设计方案内工作,利用spring封装好的hibernate模版,开发起来速度非常快,调试也很容易。它能够解决如何在团队内贯彻设计方案的问题。

  由于本文不讲解hibernate的使用,所以相关内容请查阅hibernate文档。
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章