五、整合struts2和mybatis和spring
<!-- 编码后面UserDaoImpl extends SqlSessionDaoSupport implements UserDao执行不了,目录结构也有所改变,对项目看-->
<!--执行不了的原因mybatis核心包没导入,导入mybatis较高版本可以执行了-->
<!--做测试list.jsp少了核心标签库的jar包,新导入了taglibs-standard-impl-1.2.5.jartaglibs-standard-spec-1.2.5.jar,对下一个注解整合影响改进一样-->
1、导入jar包,及mysql驱动包5.1.20(后来删除了commons-logging.jar)
2、web.xml配置文件(重要)
对应用上下文配置文件改名为默认名:applicationContext
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置spring --> <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的配置 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.class</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
![](https://img2018.cnblogs.com/blog/1463424/201908/1463424-20190824201255184-1026956769.png)
3、mybatis配置文件mybatis.cfg.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="cn.sxt.vo"/> </typeAliases> <mappers> <package name="cn.sxt.dao.UserMapper"/> </mappers> </configuration>
4、编码 整个目录结构
UserDao接口
UserDao实现
UserService接口
public interface UserService { public List<User> getAll(); }
UserService实现
public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public List<User> getAll() { return userDao.getAll(); } }
UserAction
public class UserAction { private List<User> list; private UserService userService; public String list(){ list = userService.getAll(); return "success"; } public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } }
Spring配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据源 dataSource可以使用第三方的数据源的插件,也可以使用spring给我们提供的数据源的插件 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="123"/> </bean> <!-- spring声明式事务配置,首先导入命名空间tx --> <!-- 配置事务管理器 它引入了一个dataSource,它来管理dataSource--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 --> <tx:attributes><!--required会去检查当前有没有事务开启,如果有用当前事务,没有重新开启一个事务 --> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="insert" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="remove" propagation="REQUIRED"/> <tx:method name="get" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config><!-- 正常情况下,这里配置service execution(* cn.sxt.service.impl.*.*(..)) --> <aop:pointcut expression="execution(* cn.sxt.dao.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config> <!-- 配置好了,现在再去做测试的时候,虽然有插入和删除两条sql,但由于delete的sql有错误,所以插入的也 插入不进去了,就起作用了(一个有错全部回滚) --> <!-- 配置sqlSessionFactory,完全可以取代mybatis.cfg.xml文件,但是这里引用它,它里面还可以找到映射 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:cn/sxt/vo/*.mapper.xml"/> </bean> <!--$$$$$$$$$$$$$$$$$上面内容是取代mybatis.cfg.xml文件的,没完全取代,保留了里面的取别名和找实体类的映射功能 --> <import resource="config/spring/user.xml"/>
User.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="userDao" class="cn.sxt.dao.impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <bean id="userService" class="cn.sxt.service.impl.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userAction" class="cn.sxt.action.UserAction"> <property name="userService" ref="userService"></property> </bean> </beans>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<include file="config/struts/user.xml"></include>
</struts>