1、struts
(1)web.xml中要配置的信息如下:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/spring/applicationContext.xml,WEB-INF/classes/spring/userManagement/applicationContext*.xml
</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(2)struts.xml文件
可使用引用<include file="" />使文件结构清楚。
二、Spring
包括context-hibernate.xml和applicationContext*.xml,前者配置hibernate中的部分信息和dao对象及其代理,后者配置action对象,可使用<import resource="" />使文件结构清晰。
三、hibernate
(1)*.hbm.xml 映射文件
可以在该映射文件中设置各个对象之间的映射关系,通过<set>(一对多)和<many-to-one>(多对一),代码如下:
a、set(一对多)
<set cascade="all" inverse="true" lazy="false" name="Tables">
<key column="dbId" />
<one-to-many class="Tableinfo" />
</set>
b、many-to-one(多对一)
<many-to-one name="User" column="user" class="User" not-null="true"
lazy="false">
</many-to-one>
(2)context-hibernate.xml中配置datasource等信息,如下:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/kjbrc?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF8</value></property>
<property name="username"><value>root</value></property>
<property name="password"><value>root</value></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingLocations">
<list>
<value>classpath*:hibernate/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect </prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="cache.use_second_level_cache">true</prop>
</props>
</property>
</bean>
<!--Open Session-->
<bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!--Hibernate Transaction Manager-->
<bean id="HibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
(1)web.xml中要配置的信息如下:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/spring/applicationContext.xml,WEB-INF/classes/spring/userManagement/applicationContext*.xml
</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(2)struts.xml文件
可使用引用<include file="" />使文件结构清楚。
二、Spring
包括context-hibernate.xml和applicationContext*.xml,前者配置hibernate中的部分信息和dao对象及其代理,后者配置action对象,可使用<import resource="" />使文件结构清晰。
三、hibernate
(1)*.hbm.xml 映射文件
可以在该映射文件中设置各个对象之间的映射关系,通过<set>(一对多)和<many-to-one>(多对一),代码如下:
a、set(一对多)
<set cascade="all" inverse="true" lazy="false" name="Tables">
<key column="dbId" />
<one-to-many class="Tableinfo" />
</set>
b、many-to-one(多对一)
<many-to-one name="User" column="user" class="User" not-null="true"
lazy="false">
</many-to-one>
(2)context-hibernate.xml中配置datasource等信息,如下:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/kjbrc?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF8</value></property>
<property name="username"><value>root</value></property>
<property name="password"><value>root</value></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingLocations">
<list>
<value>classpath*:hibernate/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect </prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="cache.use_second_level_cache">true</prop>
</props>
</property>
</bean>
<!--Open Session-->
<bean id="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!--Hibernate Transaction Manager-->
<bean id="HibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>