配置SSH的一种实践方法(Spring,Struts2,Hibernate) 续
SPRING:
这里主要用到是IOC的东西,所以主要说和struts,hibernate的集成。重点看一下applicationContext.xml怎么配。
为了以后少写点配置,前面就得多配点:)
根目录beans 里面加个属性:
default-autowire="byName"
再加上自动扫描的。
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.demo"/>OK,SPRING会自动扫到相应的类加到IOC容器里去。
<!--[if !supportLists]-->1. 1. <!--[endif]-->和struts配置主要还是在struts.xml
<constant name="struts.objectFactory" value="spring"/> <constant name="struts.objectFactory.spring.autoWrite" value="name"></constant>
然后ACTION 里加@Controller完了。
2. 和Hibernate相关的集成。
贴代码了,一看就清楚
配好transaction:
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>配DATASOURCE:
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="org.h2.Driver"/>
<!--<property name="driverClassName" value="com.p6spy.engine.spy.P6SpyDriver"/>-->
<property name="jdbcUrl" value="jdbc:h2:~/demo"/>
<property name="user" value="sa"/>
<property name="password" value=""/>
<property name="maxPoolSize" value="50"></property>
<property name="minPoolSize" value="10"></property>
<property name="initialPoolSize" value="15"></property>
<property name="maxIdleTime" value="120"></property>
</bean>
sessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.characterEncoding">UTF-8</prop>
<prop key="hibernate.useUnicode">true</prop>
<prop key=" hibernate.jdbc.fetch_size">100</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="connection.autoReconnectForPools">true</prop>
<prop key="hibernate.ejb.event.post-insert">org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener</prop>
<prop key="hibernate.ejb.event.post-update">org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener</prop>
<prop key="hibernate.ejb.event.post-delete">org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener</prop>
<prop key="hibernate.ejb.event.pre-collection-update">org.hibernate.envers.event.AuditEventListener</prop>
<prop key="hibernate.ejb.event.pre-collection-remove">org.hibernate.envers.event.AuditEventListener</prop>
<prop key="hibernate.ejb.event.post-collection-recreate">org.hibernate.envers.event.AuditEventListener</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.demo.domain.BaseUser</value>
<value>com.demo.domain.Product</value>
<value>com.demo.domain.ProductDetail</value>
</list>
</property>
</bean> SPRING还是比较爽的,就像空气,不存在去无处不在
HIBERNATE:
Hibernate 基本上都配好了,就说一下怎么写个Dao吧。
SR里面最重要的DAO,就是modelDao.
所有的Dao 都是从这里继承而去,里面有基本的CRUD方法,可以给具体的DAO使用,省下一些代码。
因为项目小,我就没有加service 层,有需要的可以自己加。代码就不贴了,可以自己去看。贴出来反而不好看。