Java蜘蛛人 欢迎大家

欢迎大家 来到我的blog , 如果我身边的朋友 有什么不懂可以直接来问我 我会细心的帮助你的. 如果网络上的朋友有什么不懂的 可以加我Java蜘蛛人 QQ48187537
posts - 54, comments - 192, trackbacks - 0, articles - 1

spring 整合hibernate

Posted on 2008-08-14 14:52 Java蜘蛛人 --郑成桥 阅读(203) 评论(0)  编辑  收藏
首先 顺序导入 spring hibernate struts

配置applicationContet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns
="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    
<bean id="datasource"
         
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        
<property name="driverClass"
            value
="com.microsoft.jdbc.sqlserver.SQLServerDriver">
        
</property>
        
<property name="jdbcUrl"
            value
="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=blog;SelectMethod=cursor">
        
</property>
        
<property name="user" value="sa"></property>
        
<property name="password" value="sa"></property>
        
<property name="minPoolSize" value="5"></property>
        
<property name="maxPoolSize" value="20"></property>
        
<property name="acquireIncrement" value="5"></property>
    
</bean>
    
<bean id="sessionFactory"
        
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
<property name="dataSource">
            
<ref bean="datasource" />
        
</property>
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">
            org.hibernate.dialect.SQLServerDialect 
                
</prop>
                
<prop key="hibernate.show_sql">true</prop>
            
</props>
        
</property>

        
<property name="mappingDirectoryLocations">
            
<list>
               
<value>classpath:/com/zcq/dao</value> 
            
</list>
        
</property>
    
</bean> 
       
<!--  hibernateTemplate 配置 -->
         
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
          
<property name="sessionFactory">
            
<ref bean="sessionFactory" />
          
</property> 
         
</bean>
      
<!-- zcqbb datable -->
         
<bean id="daoimp" class="com.zcq.dao.Test" >
           
<property name="hibernateTemplate" ref="hibernateTemplate" /> 
         
</bean> 
 
</beans> 

然后写接口


然后实现接口
package com.zcq.job.dao;

import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zcq.job.I.Zcq_I;

public class Zcq_Imp extends HibernateDaoSupport implements Zcq_I {

    
public void show() {
      System.out.println(
"sssssss");
    }

    
public static Zcq_I getApplication(ApplicationContext ctx)
    
{
        
return (Zcq_I)ctx.getBean("daoimp");
    }

}


web.xml 里加入
  <context-param>
    
<param-name>contextConfigLocation</param-name>
    
<param-value>/WEB-INF/applicationContext.xml</param-value>
  
</context-param>
  
<listener>
    
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  
</listener>
  
<filter>
    
<filter-name>CharacterEncodingFilter</filter-name>
    
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    
<init-param>
      
<param-name>encoding</param-name>
      
<param-value>gbk</param-value>
    
</init-param>
    
<init-param>
      
<param-name>forceEncoding</param-name>
      
<param-value>true</param-value>
    
</init-param>
  
</filter>
  
<filter-mapping>
    
<filter-name>CharacterEncodingFilter</filter-name>
    
<url-pattern>/*</url-pattern>
  </filter-mapping>

只有注册用户登录后才能发表评论。


网站导航: