各个配置文件:
applicationContext.xml的配置文件:
1. 配置好数据库连接需要的bean
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="*****"></property>
<property name="url" value="jdbc:mysql:///ssh"></property>
<!--这个value的///后面要写自己所连接的数据库,不要忘记改了!!!-->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.current_session_context_class">
thread
</prop>
</props>
</property>
<!-- 映射文件 -->
<property name="mappingLocations" >
<list>
<value>classpath:cn/itcast/domain/Users.hbm.xml</value>
</list>
</property>
</bean>
注意一定要导好jar包,同时要对应好所使用的jar包中的类(这个org.springframework.orm.hibernate3.LocalSessionFactoryBean之前写成hibernate5就一直报错。
这里我没有用hibernate.cfg.xml,如果用了的话就不需要上面这么写了,但是需要在applicationContext.xml的开头加上下面这句
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocatin" value="classpath:hibernate.cfg.xml"></property>
</property>
</bean>
2. 将整个项目需要用到的bean全部写入(这里就写了一部分)
类所在的地址一定要写对!!!!!注意这里bean是一层层注入的,sessionFactory-->usersDao-->usersService-->userAction
<bean id="usersDao" class="cn.itcast.dao.impl.UsersDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="usersService" class="cn.itcast.service.impl.UsersServiceImpl">
<property name="usersDao" ref="usersDao"></property>
</bean>
<bean id="userAction" class="cn.itcast.action.UserAction" scope="prototype">
<property name="usersService" ref="usersService"></property>
</bean>
3. 事务管理(暂时还不太理解)
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="delete*" />
<tx:method name="find*" read-only="true" />
</tx:attributes>
</tx:advice>
<!--将切入点与通知关联 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.service.*.*(..))"/>
</aop:config>
<!--也可以写成这种形式,两者含义相同 <aop:config>
<aop:pointcut expression="execution(* cn.itcast.service.*.*(..))" id="txPointCut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>-->
注意pointcut后面的命名:
struts.xml文件:照常写就好。
<constant name="struts.devMode" value="true"></constant>
可以添加这句设置为开发者模式,便于代码调试。
web.xml文件:
添加下面两句,其余均和struts2相同!
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
配置文件完。。。写配置文件一定要细心!!!!
struts2和spring整合时需要struts2-spring-plugin-2.3.24.jar!!这个jar包使action由Spring创建,而非struts2使用自身工厂创建。
(未完待续...,欢迎指正)