配置Struts+spring+hibernate

一、在WEB中配置文件spring字符集过滤
< filter >
    
< filter-name > CharsetFilter </ filter-name >
    
< filter-class >
        org.springframework.web.filter.CharacterEncodingFilter
    
</ filter-class >
    
< init-param >
        
< param-name > encoding </ param-name >
        
< param-value > UTF-8 </ param-value >
    
</ init-param >
    
< init-param >
        
< param-name > forceEncoding </ param-name >
        
< param-value > true </ param-value >
    
</ init-param >
</ filter >
< filter-mapping >
    
< filter-name > CharsetFilter </ filter-name >
    
< url-pattern > /* </ url-pattern >
</ filter-mapping >
二、在web.xml加载spring配置
< 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 >
三、在web.xml中配置OpenSessionInViewFilter
< filter >
    
< filter-name > hibernateFilter </ filter-name >
    
< filter-class >
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    
</ filter-class >
    
< init-param >
        
< param-name > singleSession </ param-name >
        
< param-value > false </ param-value >
    
</ init-param >
</ filter >

< filter-mapping >
    
< filter-name > hibernateFilter </ filter-name >
    
< url-pattern > /* </ url-pattern >
</ filter-mapping >
四、在struts-config.xml配置spring
< controller  contentType ="text/html;charset=UTF-8" >
    
< set-property  property ="processorClass"
            value
="org.springframework.web.struts.DelegatingRequestProcessor"   />
</ controller >

< message-resources  parameter ="org.school.web.ApplicationResources"   />

< plug-in
    
className ="org.springframework.web.struts.ContextLoaderPlugIn" >
    
< set-property  property ="contextConfigLocation"
            value
="/WEB-INF/strutsContext.xml"   />
</ plug-in >
五、配置log4j.properties文件
#log4j.rootLogger = DEBUG , A1 , R
#log4j.rootLogger
= INFO , A1 , R
log4j.rootLogger
= ERROR , A1 
#log4j.category.com.laoer
= INFO , A1 , R
log4j.appender.A1
= org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout
= org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern
= %-d{yyyy-MM-dd HH:mm:ss , SSS}  [ %c ] - [ %p ]  %m%n

log4j.appender.R
= org.apache.log4j.RollingFileAppender
log4j.appender.R.File
= study_log.txt
log4j.appender.R.MaxFileSize
= 500KB
log4j.appender.R.MaxBackupIndex
= 1
#log4j.appender.R.layout
= org.apache.log4j.HTMLLayout
log4j.appender.R.layout
= org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern
= %d{yyyy-MM-dd HH:mm:ss , SSS}  [ %t ]   [ %c ]   [ %p ]  - %m%n
六、配置applicationContent.xml文件
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

< beans  default-lazy-init ="false" >

    
< 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/school?useUnicode=true &amp; characterEncoding=utf8 </ value >
        
</ property >
        
< property  name ="username" >
            
< value > root </ value >
        
</ property >
        
< property  name ="password" >
            
< value > vkbje76 </ value >
        
</ property >
        
< property  name ="maxActive" >
            
< value > 10 </ value >
        
</ property >
        
< property  name ="maxIdle" >
            
< value > 2 </ value >
        
</ property >
        
< property  name ="maxWait" >
            
< value > 1200 </ value >
        
</ 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.MySQLDialect
                
</ prop >
                
<!--
                    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                
-->
                
< prop  key ="hibernate.dialect" >
                    org.hibernate.dialect.MySQLDialect
                
</ prop >
                
< prop  key ="hibernate.show_sql" > true </ prop >
                
< prop  key ="hibernate.jdbc.fetch_size" > 25 </ prop >
                
< prop  key ="hibernate.jdbc.batch_size" > 50 </ prop >

                
<!-- <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                    
                
-->
            
</ props >
        
</ property >
        
< property  name ="mappingResources" >
            
< list >
                
< value > org/school/dao/entity/Users.hbm.xml </ value >
                
< value > org/school/dao/entity/Groups.hbm.xml </ value >
                
< value > org/school/dao/entity/GroupUser.hbm.xml </ value >
            
</ list >
        
</ property >
    
</ bean >
    
< bean  id ="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >

    
< bean  id ="baseProxyFactory"  abstract ="true"
        class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
        
< property  name ="transactionManager"  ref ="transactionManager"   />
        
< property  name ="transactionAttributes" >
            
< props >
                
< prop  key ="get*" > PROPAGATION_REQUIRED </ prop >
                
< prop  key ="update*" > PROPAGATION_REQUIRED </ prop >
                
< prop  key ="add*" > PROPAGATION_REQUIRED </ prop >
                
< prop  key ="delete*" > PROPAGATION_REQUIRED </ prop >
            
</ props >
        
</ property >
    
</ bean >
    
<!--
        <bean id="UserServerProxy" parent="baseProxyFactory">
        <property name="target">
        <ref local="UserServer" />
        </property>
        </bean>
    
-->

    
< bean  id ="UserDAO"  class ="org.school.dao.hibernate.UserDAO" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory"   />
        
</ property >
    
</ bean >
    
    
< bean  id ="GroupDAO"  class ="org.school.dao.hibernate.GroupDAO" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory"   />
        
</ property >
    
</ bean >
    
    
< bean  id ="GroupUserDAO"  class ="org.school.dao.hibernate.GroupUserDAO" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory"   />
        
</ property >
    
</ bean >
    
< bean  id ="UserServer"  class ="org.school.server.impl.UserServer" >
        
< property  name ="userDAO" >
            
< ref  bean ="UserDAO"   />
        
</ property >
        
< property  name ="groupDAO" >
            
< ref  bean ="GroupDAO"   />
        
</ property >
        
< property  name ="groupUserDAO" >
            
< ref  bean ="GroupUserDAO"   />
        
</ property >
    
</ bean >
    
    
< bean  id ="UserServerProxy"  parent ="baseProxyFactory" >
        
< property  name ="target" >
            
< ref  local ="UserServer"   />
        
</ property >
    
</ bean >
</ beans >
七、写DAO基础类
IBaseDAO.java
package  org.school.dao;

import  java.io.Serializable;
import  java.util.List;

public   interface  IBaseDAO  {
    
/*
     * 
     
*/

    
public Object findById(String className, Serializable id);

    
public List findByProperty(String tableName,String propertyName, Object value);

    
public void attachDirty(Object instance);

    
public Serializable save(Object transientInstance);

    
public void delete(Object persistentInstance);

}
BaseDAO.java
package  org.school.dao.hibernate;

import  java.io.Serializable;
import  java.util.List;

import  org.apache.commons.logging.Log;
import  org.apache.commons.logging.LogFactory;
import  org.hibernate.Query;
import  org.school.dao.IBaseDAO;
import  org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Data access object (DAO) for domain model
 * 
 * 
@author MyEclipse - Hibernate Tools
 
*/


public   class  BaseDAO  extends  HibernateDaoSupport  implements  IBaseDAO  {
    
public static final Log log = LogFactory.getLog(UserDAO.class);

    
/* (non-Javadoc)
     * @see org.school.dao.hibernate.IBaseDAO#findById(java.lang.String, java.lang.Integer)
     
*/

    
public Object findById(String className, Serializable id) {
        log.debug(
"getting instance with id: " + id);
        
try {
            Object instance 
= getHibernateTemplate().get(className, id);
            
return instance;
        }
 catch (RuntimeException re) {
            log.error(
"get failed", re);
            
throw re;
        }

    }


    
/* (non-Javadoc)
     * @see org.school.dao.hibernate.IBaseDAO#findByProperty(java.lang.String, java.lang.Object)
     
*/

    
public List findByProperty(String tableName,String propertyName, Object value) {
        log.debug(
"finding instance with property: " + propertyName
                
+ ", value: " + value);
        
try {
            String queryString 
= "from "+tableName+" as model where model."
                    
+ propertyName + "= ?";
            Query queryObject 
=getSession().createQuery(queryString);
            queryObject.setParameter(
0, value);
            
return queryObject.list();
        }
 catch (RuntimeException re) {
            log.error(
"find by property name failed", re);
            
throw re;
        }

    }

    
    
/* (non-Javadoc)
     * @see org.school.dao.hibernate.IBaseDAO#attachDirty(java.lang.Object)
     
*/

    
public void attachDirty(Object instance) {
        log.debug(
"attaching dirty instance");
        
try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug(
"attach successful");
        }
 catch (RuntimeException re) {
            log.error(
"attach failed", re);
            
throw re;
        }

    }

    
    
/* (non-Javadoc)
     * @see org.school.dao.hibernate.IBaseDAO#save(java.lang.Object)
     
*/

    
public Serializable save(Object transientInstance) {
        log.debug(
"saving instance");
        
try {
            
return getHibernateTemplate().save(transientInstance);
            
//log.debug("save successful");
        }
 catch (RuntimeException re) {
            log.error(
"save failed", re);
            
throw re;
        }

    }

    
    
/* (non-Javadoc)
     * @see org.school.dao.hibernate.IBaseDAO#delete(java.lang.Object)
     
*/

    
public void delete(Object persistentInstance) {
        log.debug(
"deleting instance");
        
try {
            getSession().delete(persistentInstance);
            log.debug(
"delete successful");
        }
 catch (RuntimeException re) {
            log.error(
"delete failed", re);
            
throw re;
        }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值