小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

在myeclipse下整合spring和hibernate

 duduwolf 2006-08-05

整合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)境,我們當(dāng)然還得應(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./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./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);
        }

    }

}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約