posts - 0,  comments - 17,  trackbacks - 0

          來源:互聯網

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

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

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




          3. 導入hibernate, 選擇myeclipse的add hibernatecapabilities,注意把copy .....打勾




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



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




          然后要配置sessionFactory對應的數據源,注,數據源對應的bean id也需要設置,可以簡單設置為dataSource
          就不貼圖咯




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



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

          3.應用:
            配置好了環境,我們當然還得應用咯.下面給出我的代碼
          首先創建pojo和對應的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 >


          然后開發對應的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;
              }

          }

          相應的,還需要修改下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>


          最后的最后,開發一個測試類:

          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 2008-01-25 09:37 xyz 閱讀(673) 評論(0)  編輯  收藏 所屬分類: 網絡文摘

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          留言簿

          隨筆檔案(1)

          文章分類(44)

          文章檔案(46)

          收藏夾(1)

          Adobe

          AOP

          API

          appServer

          BI

          c

          • c-free
          • codeblocks
          • codelite
          • CodeLite IDE 是一個強大的開源,跨平臺的 C/C++整合開發環境. 支持包括 Windows、Linux 和 Mac 系統下運行
          • codelite官網
          • dev-c++
          • Dev-C++是一個C&C++開發工具,它是一款自由軟件,遵守GPL協議。
          • GCC
          • GCC 原名為 GNU C 語言編譯器(GNU C Compiler),因為它原本只能處理 C語言。GCC 很快地擴展,變得可處理 C++。之后也變得可處理 Fortran、Pascal、Objective-C、Java, 以及 Ada 與其他語言。

          Cache

          CMS

          DB

          eclipse

          FreeMarker

          hibernate

          html5

          ibatis

          java

          jquery

          js

          json

          Linux

          Log

          mail server

          mobile

          mysql

          oauth

          openID

          other

          PHP

          portal

          report

          Scheduler

          schema

          Security

          SOA

          spring

          struts

          UI原型設計

          w3c

          Wap

          webservice

          xml

          供應鏈管理

          博客鏈接

          好網站

          工作流

          開源網

          招聘

          插件下載

          操作系統

          構建可伸縮的系統

          構建工具

          測試

          • IETest
          • IE官網
          • OpenSTA
          • Siege
          • Siege是一個壓力測試和評測工具,設計用于WEB開發這評估應用在壓力下的承受能力

          游戲

          源碼托管

          經營

          資源

          金融/財務

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 福贡县| 开封县| 比如县| 内乡县| 和硕县| 那曲县| 江门市| 内丘县| 竹山县| 金阳县| 保定市| 河曲县| 永州市| 赫章县| 尚义县| 海城市| 抚远县| 阿拉善盟| 泽库县| 古浪县| 苍溪县| 新密市| 揭西县| 临海市| 黔南| 盐池县| 凤山县| 睢宁县| 交城县| 杂多县| 辉县市| 麻阳| 桑植县| 当涂县| 永登县| 伊金霍洛旗| 香港 | 社旗县| 崇左市| 长海县| 武平县|