SSM整合步骤
1.引入依赖
spring相关:mybatis mybatis-spring mysql druid
struts2相关:struts2-spring-plugin servlet-api
其它:jstl log4j fastjson
2.spring + mybatis
1.建表
2.实体类
3.DAO接口
4.Mapper配置文件
5.Service接口
6.Service实现类
7.编写spring.xml
a.创建数据源对象 DruidDateSource 注入driverClassName url username password
<!--创建数据源对象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/cy_yxc?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
b.创建SqlSessionFactory 注入dataSource mapperLocation typeAliasPackage别名
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--依赖数据源-->
<property name="dataSource" ref="dataSource"/>
<!--注入mapper文件通用方式-->
<property name="mapperLocations" value="classpath:com/examole/demo/dao/*Mapper.xml"/>
<!--注入别名相关配置 typeAliasesPackage:用来给指定包中所有类起别名 默认的别名:类名|类名首字母小写-->
<property name="typeAliasesPackage" value="com.example.demo.entity"/>
</bean>
c.创建DAO对象 mapperScannerConfig 注入sqlSessionFactory basePackage
<!--创建Dao对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--扫描Dao接口所在的包-->
<property name="basePackage" value="com.example.demo.dao"/>
</bean>
d.创建事务管理器对象 DataSourceTransactionManager 注入DataSource
<!--创建事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源对象-->
<property name="dataSource" ref="dataSource"/>
</bean>
e.创建事务环绕通知并对事务进行细粒度控制 <tx:advice id="" transaction-manager=“事务管理器”>
<!--根据事务管理器创建事务管理环绕通知 tx:advice-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--事务细粒度配置-->
<tx:attributes>
<tx:method name="save*"/>
<tx:method name="add*"/>
<tx:method name="insert*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="find*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
f.配置事务切面<aop:config >
<!--配置事务切面-->
<aop:config>
<aop:pointcut id="pc" expression="within(com.example.demo.service.*ServiceImpl)"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
g.管理Service组件对象并注入响应DAO组件
<!--管理service组件 注入Dao以及事务管理器-->
<bean id="userService" class="com.example.demo.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
3.spring + struts2
1.配置web.xml
a.配置自动工厂监听器 ContextLoaderListener
<!--配置启动工厂的监听器listener-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
b.配置spring配置文件位置 context-param key:contextConfigLocation value:classpath:spring配置文件位置
<!--配置spring配置文件位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
c.配置struts2核心filter StrutsPrepareAndExecuteFilter url-pattern:/*
<!--filter-->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.引入struts.xml
3.开发Action组件 注入Service组件
4.编写spring配置文件 管理Action组件
<bean id="userAction" class="com.test.action.UserAction" scope="prototype">
<property name="xxService" ref="xxxService">
</bean>
5.配置struts.xml
<package name="user" extends="struts-default" namespace="/user">
<action name="hello" class="userAction" method="hello">
<result name="success">/index.jsp</result>
</action>
</package>
4.将项目部署到服务器,进行访问
访问路径:http://localhost:端口号/项目名/namespace/action标签的name属性