`
zxwwjs
  • 浏览: 9852 次
  • 来自: ...
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

配置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
<controllercontentType="text/html;charset=UTF-8">
<set-propertyproperty="processorClass"
value
="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>

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

<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-propertyproperty="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-ddHH: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-ddHH:mm:ss,SSS}[%t][%c][%p]-%m%n
六、配置applicationContent.xml文件
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">

<beansdefault-lazy-init="false">

<beanid="dataSource"
class
="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<propertyname="url">
<value>jdbc:mysql://localhost/school?useUnicode=true&amp;characterEncoding=utf8</value>
</property>
<propertyname="username">
<value>root</value>
</property>
<propertyname="password">
<value>vkbje76</value>
</property>
<propertyname="maxActive">
<value>10</value>
</property>
<propertyname="maxIdle">
<value>2</value>
</property>
<propertyname="maxWait">
<value>1200</value>
</property>
</bean>
<beanid="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource">
<refbean="dataSource"/>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!--
<propkey="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
-->
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<propkey="hibernate.show_sql">true</prop>
<propkey="hibernate.jdbc.fetch_size">25</prop>
<propkey="hibernate.jdbc.batch_size">50</prop>

<!--<propkey="hibernate.hbm2ddl.auto">create-drop</prop>

-->
</props>
</property>
<propertyname="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>
<beanid="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

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

<beanid="UserDAO"class="org.school.dao.hibernate.UserDAO">
<propertyname="sessionFactory">
<refbean="sessionFactory"/>
</property>
</bean>

<beanid="GroupDAO"class="org.school.dao.hibernate.GroupDAO">
<propertyname="sessionFactory">
<refbean="sessionFactory"/>
</property>
</bean>

<beanid="GroupUserDAO"class="org.school.dao.hibernate.GroupUserDAO">
<propertyname="sessionFactory">
<refbean="sessionFactory"/>
</property>
</bean>
<beanid="UserServer"class="org.school.server.impl.UserServer">
<propertyname="userDAO">
<refbean="UserDAO"/>
</property>
<propertyname="groupDAO">
<refbean="GroupDAO"/>
</property>
<propertyname="groupUserDAO">
<refbean="GroupUserDAO"/>
</property>
</bean>

<beanid="UserServerProxy"parent="baseProxyFactory">
<propertyname="target">
<reflocal="UserServer"/>
</property>
</bean>
</beans>
七、写DAO基础类
IBaseDAO.java
packageorg.school.dao;

importjava.io.Serializable;
importjava.util.List;

publicinterfaceIBaseDAO...{
/**//*
*
*/

publicObjectfindById(StringclassName,Serializableid);

publicListfindByProperty(StringtableName,StringpropertyName,Objectvalue);

publicvoidattachDirty(Objectinstance);

publicSerializablesave(ObjecttransientInstance);

publicvoiddelete(ObjectpersistentInstance);

}
BaseDAO.java
packageorg.school.dao.hibernate;

importjava.io.Serializable;
importjava.util.List;

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

/***//**
*Dataaccessobject(DAO)fordomainmodel
*
*
@authorMyEclipse-HibernateTools
*/


publicclassBaseDAOextendsHibernateDaoSupportimplementsIBaseDAO...{
publicstaticfinalLoglog=LogFactory.getLog(UserDAO.class);

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

publicObjectfindById(StringclassName,Serializableid)...{
log.debug(
"gettinginstancewithid:"+id);
try...{
Objectinstance
=getHibernateTemplate().get(className,id);
returninstance;
}
catch(RuntimeExceptionre)...{
log.error(
"getfailed",re);
throwre;
}

}


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

publicListfindByProperty(StringtableName,StringpropertyName,Objectvalue)...{
log.debug(
"findinginstancewithproperty:"+propertyName
+",value:"+value);
try...{
StringqueryString
="from"+tableName+"asmodelwheremodel."
+propertyName+"=?";
QueryqueryObject
=getSession().createQuery(queryString);
queryObject.setParameter(
0,value);
returnqueryObject.list();
}
catch(RuntimeExceptionre)...{
log.error(
"findbypropertynamefailed",re);
throwre;
}

}


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

publicvoidattachDirty(Objectinstance)...{
log.debug(
"attachingdirtyinstance");
try...{
getHibernateTemplate().saveOrUpdate(instance);
log.debug(
"attachsuccessful");
}
catch(RuntimeExceptionre)...{
log.error(
"attachfailed",re);
throwre;
}

}


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

publicSerializablesave(ObjecttransientInstance)...{
log.debug(
"savinginstance");
try...{
returngetHibernateTemplate().save(transientInstance);
//log.debug("savesuccessful");
}
catch(RuntimeExceptionre)...{
log.error(
"savefailed",re);
throwre;
}

}


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

publicvoiddelete(ObjectpersistentInstance)...{
log.debug(
"deletinginstance");
try...{
getSession().delete(persistentInstance);
log.debug(
"deletesuccessful");
}
catch(RuntimeExceptionre)...{
log.error(
"deletefailed",re);
throwre;
}

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics