Struts2框架07 Struts2 + Spring + Mybatis 整合
Posted 寻渝记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Struts2框架07 Struts2 + Spring + Mybatis 整合相关的知识,希望对你有一定的参考价值。
1 导包
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>cn.xiangxu</groupId> 4 <artifactId>ssh03</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <dependencies> 8 <dependency> 9 <groupId>org.apache.struts</groupId> 10 <artifactId>struts2-core</artifactId> 11 <version>2.3.8</version> 12 </dependency> 13 <dependency> 14 <groupId>org.apache.struts</groupId> 15 <artifactId>struts2-spring-plugin</artifactId> 16 <version>2.3.8</version> 17 </dependency> 18 <dependency> 19 <groupId>org.apache.struts</groupId> 20 <artifactId>struts2-json-plugin</artifactId> 21 <version>2.3.8</version> 22 </dependency> 23 <dependency> 24 <groupId>junit</groupId> 25 <artifactId>junit</artifactId> 26 <version>4.12</version> 27 </dependency> 28 <dependency> 29 <groupId>mysql</groupId> 30 <artifactId>mysql-connector-java</artifactId> 31 <version>5.1.37</version> 32 </dependency> 33 <dependency> 34 <groupId>commons-dbcp</groupId> 35 <artifactId>commons-dbcp</artifactId> 36 <version>1.4</version> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-jdbc</artifactId> 41 <version>3.2.8.RELEASE</version> 42 </dependency> 43 <dependency> 44 <groupId>org.mybatis</groupId> 45 <artifactId>mybatis</artifactId> 46 <version>3.2.8</version> 47 </dependency> 48 <dependency> 49 <groupId>org.mybatis</groupId> 50 <artifactId>mybatis-spring</artifactId> 51 <version>1.2.3</version> 52 </dependency> 53 <dependency> 54 <groupId>jstl</groupId> 55 <artifactId>jstl</artifactId> 56 <version>1.2</version> 57 </dependency> 58 <dependency> 59 <groupId>javax.annotation</groupId> 60 <artifactId>javax.annotation-api</artifactId> 61 <version>1.2</version> 62 </dependency> 63 </dependencies> 64 </project>
2 配置文件
2.1 配置web.xml
配置spring的监听器
配置spring配置文件位置
配置主控制器和过滤条件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3 <display-name>ssh03</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 配置spring监听 14 目的:容器启动时自动加载一些东西到缓存中 --> 15 <listener> 16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 17 </listener> 18 19 <!-- 配置Spring配置文件的位置 --> 20 <context-param> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>classpath:spring_*.xml</param-value> 23 </context-param> 24 25 <!-- 配置主控制器和过滤条件 --> 26 <filter> 27 <filter-name>mvc</filter-name> 28 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 29 </filter> 30 <filter-mapping> 31 <filter-name>mvc</filter-name> 32 <url-pattern>/*</url-pattern> 33 </filter-mapping> 34 35 </web-app>
2.2 配置spring_context.xml
配置组件扫描
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 12 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 16 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 17 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 18 19 <!-- 配置组件扫描 --> 20 <context:component-scan base-package="cn.xiangxu" /> 21 22 </beans>
2.3 配置spring_mybatis.xml
配置数据库连接池
配置SqlSessionFactoryBean
配置mapper扫描
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 12 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 16 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 17 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 18 19 <!-- 配置数据库连接池 --> 20 <bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource"> 21 <property name="username" value="root"></property> 22 <property name="password" value="182838"></property> 23 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 24 <property name="url" value="jdbc:mysql:///test?useUnicode=true&characterEncoding=utf8"></property> 25 </bean> 26 27 <!-- 配置SqlSessionFactory --> 28 <bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean"> 29 <property name="dataSource" ref="dbcp"></property> 30 <property name="mapperLocations" value="classpath:mapper/*.xml"></property> 31 </bean> 32 33 <!-- 配置mapper扫描 --> 34 <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 35 <property name="basePackage" value="cn.xiangxu.dao"></property> 36 </bean> 37 38 </beans>
2.4 配置struts.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!DOCTYPE struts PUBLIC 4 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 5 "http://struts.apache.org/dtds/struts-2.3.dtd"> 6 7 <struts> 8 9 <!-- 测试struts整合spring时用 --> 10 <package name="test" namespace="/test" extends="json-default"> 11 <action name="demo"> 12 <result> 13 /WEB-INF/jsp/msg.jsp 14 </result> 15 </action> 16 </package> 17 18 <!-- 测试ValueStack时用 --> 19 <package name="vs" namespace="/vs" extends="json-default"> 20 <action name="valueStack" class="valueStackAction" method="valueStaceMethod"> 21 <result name="success"> 22 /WEB-INF/jsp/valueStack.jsp 23 </result> 24 </action> 25 </package> 26 27 <!-- 测试struts2+spring+springmybatis时用 --> 28 <package name="test02" namespace="/test02" extends="json-default"> 29 <action name="toLogin"> 30 <result> 31 /WEB-INF/jsp/login.jsp 32 </result> 33 </action> 34 35 <action name="login" class="userLoginAction" method="login"> 36 <result name="success" > 37 /WEB-INF/jsp/index.jsp 38 <!-- 39 <param name="root">result02</param> 40 --> 41 </result> 42 <result name="error" type="redirectAction"> 43 toLogin.action 44 </result> 45 </action> 46 </package> 47 48 </struts> 49 50
3 编写代码
3.1 持久层代码和springmvc+sping+mybatis的持久层代码结构类似
3.2 业务层代码和springmvc+sping+mybatis的业务层代码结构类似
3.3 表现层代码和struts+spring那篇博客的代码结构类似
4 项目结构图
待更新...2017年7月17日21:46:36 好饿呀
5 本篇博客源代码
待更新...2017年7月17日21:46:40
以上是关于Struts2框架07 Struts2 + Spring + Mybatis 整合的主要内容,如果未能解决你的问题,请参考以下文章