dyerac  
          dyerac In Java
          公告

          日歷
          <2006年8月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789
          統(tǒng)計
          • 隨筆 - 36
          • 文章 - 10
          • 評論 - 94
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿(5)

          隨筆分類(49)

          隨筆檔案(36)

          文章分類(11)

          文章檔案(10)

          相冊

          dyerac

          搜索

          •  

          積分與排名

          • 積分 - 79471
          • 排名 - 705

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

           

          整合hibernate和spring這樣的文章已經(jīng)很多了,下面我們來看看如何利用myeclipse的功能為整合提速咯

          1.首先,創(chuàng)建工程,可以直接選創(chuàng)建J2EE web工程
          ?? (這....就不用貼圖了吧)

          2.導(dǎo)入spring, 選擇myeclipse的add spring capabilities,注意把copy .....打勾(注,如果想要在spring的配置文件中配置hibernate的話, 一定要先導(dǎo)入spring)




          3. 導(dǎo)入hibernate, 選擇myeclipse的add hibernatecapabilities,注意把copy .....打勾




          這時,myeclipse檢測到已有spring,會問如何處理hibernate配置信息,? 這里, 我們選擇把hibernate的配置信息寫在spring的配置信息中



          接著,既然選擇把在spring配置文件中配置hibernate信息,就需要設(shè)置hibernate的sessionfactory在配置文件中的bean id, 這里, 就設(shè)置為sessionFactory




          然后要配置sessionFactory對應(yīng)的數(shù)據(jù)源,注,數(shù)據(jù)源對應(yīng)的bean id也需要設(shè)置,可以簡單設(shè)置為dataSource
          就不貼圖咯




          最后,選擇sessionfactory對于的實現(xiàn)類,可以就用spring提供的LocalSessionFactory



          這樣, 我們就在項目中添加了spring和hibernate并將他們給予整合咯

          3.應(yīng)用:
          ? 配置好了環(huán)境,我們當然還得應(yīng)用咯.下面給出我的代碼
          首先創(chuàng)建pojo和對應(yīng)的hbm.xml

          package ?mapping;

          public ? class ?Test? {

          ????
          public ?Test()? {
          ????????
          super ();
          ????????
          // ?TODO?Auto-generated?constructor?stub
          ????}


          ????
          private ? int ?id;
          ????
          ????
          private ?String?name;

          ????
          public ? int ?getId()? {
          ????????
          return ?id;
          ????}


          ????
          public ? void ?setId( int ?id)? {
          ????????
          this .id? = ?id;
          ????}


          ????
          public ?String?getName()? {
          ????????
          return ?name;
          ????}


          ????
          public ? void ?setName(String?name)? {
          ????????
          this .name? = ?name;
          ????}

          ????
          ????
          }


          <? xml?version="1.0" ?>
          <! DOCTYPE?hibernate-mapping?PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
          >

          < hibernate-mapping? default-lazy ="false" ??auto-import ="true" ?package ="mapping" >
          ???
          < class? table ="test" ?name ="Test" >
          ?????
          < id? name ="id" ?column ="test_id" ?type ="int" >
          ???????
          < generator? class ="native" ></ generator >
          ?????
          </ id >
          ?????
          ?????
          < property? name ="name" ?type ="string" ?column ="name" ></ property >
          ???
          </ class >
          </ hibernate-mapping >


          然后開發(fā)對應(yīng)的DAO操作pojo, 因為我比較懶,所以直接使用HibernateTemplate進行操作

          package?mapping;
          ?
          import?java.util.List;

          import?org.hibernate.Criteria;
          import?org.springframework.orm.hibernate3.HibernateTemplate;

          public?class?TestDAO?{

          ????
          private?HibernateTemplate?hibernateTemplate;

          ????
          public?TestDAO()?{
          ????????
          super();
          ????????
          //?TODO?Auto-generated?constructor?stub
          ????}


          ????
          public?Test?getTest(String?name)?throws?Exception?{
          ????????Test?t
          =new?Test();
          ????????t.setName(name);
          ????????List?list?
          =?this.getHibernateTemplate().findByExample(t);
          ????????
          if?(list.isEmpty())
          ????????????
          throw?new?Exception("No?Such?Record");
          ????????
          else
          ????????????
          return?(Test)?list.get(0);
          ????}


          ????
          public?void?addTest(String?name)?{
          ????????Test?test?
          =?new?Test();
          ????????test.setName(name);
          ????????
          this.getHibernateTemplate().save(test);
          ????}


          ????
          public?void?updateTest(Test?test){
          ????????
          this.getHibernateTemplate().update(test);
          ????}

          ????
          ????
          public?void?deleteTest(Test?test){
          ????????
          this.getHibernateTemplate().delete(test);
          ????}

          ????
          ????
          public?HibernateTemplate?getHibernateTemplate()?{
          ????????
          return?hibernateTemplate;
          ????}


          ????
          public?void?setHibernateTemplate(HibernateTemplate?ht)?{
          ????????
          this.hibernateTemplate?=?ht;
          ????}

          }

          ?

          相應(yīng)的,還需要修改下spring的配置文件

          <?xml?version="1.0"?encoding="UTF-8"?>
          <!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>
          ?

          ????
          <bean?id="dataSource"?class="org.apache.commons.dbcp.BasicDataSource">
          ????????
          <property?name="driverClassName">
          ????????????
          <value>com.mysql.jdbc.Driver</value>
          ????????
          </property>
          ????????
          <property?name="url">
          ????????????
          <value>jdbc:mysql://localhost:3306/nirvana?useUnicode=true</value>
          ????????
          </property>
          ????????
          <property?name="username">
          ????????????
          <value>dyerac</value>
          ????????
          </property>
          ????????
          <property?name="password">
          ????????????
          <value></value>
          ????????
          </property>
          ????
          </bean>
          ????????
          ????
          <bean?id="sessoinFactory"?class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          ????????
          <property?name="dataSource">
          ????????????
          <ref?bean="dataSource"?/>
          ????????
          </property>
          ????????
          <property?name="hibernateProperties">
          ????????????
          <props>
          ????????????????
          <prop?key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
          ????????????????
          <prop?key="connection.characterEncoding">utf8</prop>
          ????????????????
          <prop?key="hibernate.show_sql">true</prop>
          ????????????????
          <prop?key="hibernate.hbm2ddl.auto">update</prop>
          ????????????
          </props>
          ????????
          </property>
          ????????
          <property?name="mappingDirectoryLocations">
          ???????????
          <list?>
          ????????????
          <value>src/mapping</value>
          ???????????
          </list>
          ????????
          </property>
          ????
          </bean>
          ????
          ????
          <bean?id="hibernateTemplate"?class="org.springframework.orm.hibernate3.HibernateTemplate">
          ??????
          <property?name="sessionFactory">
          ???????
          <ref?bean="sessoinFactory"/>
          ??????
          </property>
          ??????
          <property?name="allowCreate">
          ????????
          <value>true</value>
          ??????
          </property>
          ????
          </bean>
          ????
          ????
          <bean?id="testDAO"?class="mapping.TestDAO">
          ??????
          <property?name="hibernateTemplate">
          ????????
          <ref?bean="hibernateTemplate"/>
          ??????
          </property>
          ????
          </bean>
          </beans>


          最后的最后,開發(fā)一個測試類:

          import?mapping.Test;
          import?mapping.TestDAO;?
          import?org.springframework.context.ApplicationContext;
          import?org.springframework.context.support.FileSystemXmlApplicationContext;

          public?class?Tester?{
          ?
          ????
          public?static?void?main(String?args[])?{
          ????????ApplicationContext?ctx?
          =?new?FileSystemXmlApplicationContext(
          ????????????????
          "src/applicationContext.xml");
          ????????TestDAO?test?
          =?(TestDAO)?ctx.getBean("testDAO");
          ????????
          //test.addTest("dyerac");
          ????????try?{?
          ????????????Test?t?
          =?test.getTest("bsbs");
          ????????????System.err.println(t.getName());
          ????????????
          //t.setName("bsbs");
          ????????????
          //test.updateTest(t);
          ?????????????
          //test.deleteTest(t);
          ????????}
          ?catch?(Exception?e)?{?
          ????????????System.err.println(e);
          ????????}

          ????}

          }

          posted on 2006-08-04 17:42 dyerac in java... 閱讀(6911) 評論(10)  編輯  收藏 所屬分類: spring&hibernate原創(chuàng)文章
          評論:
          • # re: 在myeclipse下整合spring和hibernate  高峰 Posted @ 2006-09-26 16:34
            我用sqlserver連的數(shù)據(jù)庫 出異常  回復(fù)  更多評論   

          • # re: 在myeclipse下整合spring和hibernate  dyerac in java... Posted @ 2006-09-29 01:16
            不會吧,什么問題?  回復(fù)  更多評論   

          • # re: 在myeclipse下整合spring和hibernate  冰川 Posted @ 2006-10-04 21:13
            樓主自己測試過了嗎?  回復(fù)  更多評論   

          • # re: 在myeclipse下整合spring和hibernate  dyerac in java... Posted @ 2006-10-04 21:48
            @冰川
            在mysql下肯定沒有問題,因為這是我做了后才寫的
            其他數(shù)據(jù)庫也應(yīng)該沒有問題  回復(fù)  更多評論   

          • # re: 在myeclipse下整合spring和hibernate  fishbaby Posted @ 2006-10-17 16:33
            樓主的樣例不錯,讓我知道了 spring+hibernate是怎么個流程,我試著做了這個樣例,沒有發(fā)現(xiàn)問題。謝謝樓主!  回復(fù)  更多評論   

          • # re: 在myeclipse下整合spring和hibernate  leifeng Posted @ 2006-12-07 19:49
            ..開發(fā)對應(yīng)的DAO操作pojo..

            是否多余?因為habernate mapping 已經(jīng)幫你做好了吧  回復(fù)  更多評論   

          • # re: 在myeclipse下整合spring和hibernate  dyerac in java... Posted @ 2006-12-08 17:09
            恩,只是為演示而已。  回復(fù)  更多評論   

          • # re: 在myeclipse下整合spring和hibernate  loner Posted @ 2006-12-27 16:15
            為什么我的   回復(fù)  更多評論   

          • # re: 在myeclipse下整合spring和hibernate  loner Posted @ 2006-12-27 16:17
            為什么我的 db profile 下是空的,無法選擇呢````這是為什么阿``  回復(fù)  更多評論   

          • # re: 在myeclipse下整合spring和hibernate  ,貓貓 Posted @ 2007-03-19 21:48
            List list = this.getHibernateTemplate().findByExample(t);
            老報沒有findByEample()這個方法的錯??  回復(fù)  更多評論   


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
           
          Copyright © dyerac in java... Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 秦皇岛市| 宝山区| 鹰潭市| 汤原县| 精河县| 会宁县| 祁门县| 华安县| 清丰县| 聊城市| 隆昌县| 会东县| 普定县| 宽甸| 虹口区| 黎城县| 吉安市| 高碑店市| 孟村| 武穴市| 方正县| 禄劝| 钦州市| 抚州市| 邯郸县| 疏勒县| 洛宁县| 峨山| 江西省| 绥阳县| 历史| 平顶山市| 叶城县| 神池县| 江西省| 册亨县| 通渭县| 彰化县| 偏关县| 门头沟区| 阿拉善右旗|