经过几天的努力,我的struts2.3+spring3.2+mybatis3.2整合终于通过了测试,并正式运行了,下面把个配置贴出来
第一个是maven的配置文件 pom.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.dahafo.um</groupId><artifactId>um</artifactId><packaging>war</packaging><version>1.1.1-SNAPSHOT</version><name>um Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>3.0-alpha-1</version><scope>system</scope><systemPath>D:\tomcat\apache-tomcat-7.0.39_um\lib\servlet-api.jar</systemPath></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.2.1-b03</version><scope>system</scope><systemPath>D:\tomcat\apache-tomcat-7.0.39_um\lib\jsp-api.jar</systemPath></dependency><!-- log start--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><!-- log end-->
<!-- struts2 --><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-core</artifactId><version>2.3.14</version></dependency><dependency><groupId>org.apache.struts.xwork</groupId><artifactId>xwork-core</artifactId><version>2.3.14</version></dependency><!-- spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>3.2.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>3.2.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.2.2.RELEASE</version></dependency><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.14</version></dependency><!-- spring end --><!-- mysql connector --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.25</version></dependency><!-- mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.2.2</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.2.0</version></dependency><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><dependency><groupId>commons-pool</groupId><artifactId>commons-pool</artifactId><version>1.6</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>3.2.2.RELEASE</version></dependency></dependencies><build><pluginManagement><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.1</version><configuration><path>/um</path><url>http://localhost:8080/manager/text</url><server>tomcat</server></configuration></plugin></plugins></pluginManagement><finalName>um</finalName></build>
</project>
第二个是struts2的配置文件struts.xml 这个文件放在src目录下,文件名称不能为其他
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd">
<struts><constant value="false" name="struts.enable.DynamicMethodInvocation"/> <constant value="true" name="struts.devMode"/> <!-- 将action托管给spring --><constant name="struts.objectFactory" value="spring" /><package name="default" extends="struts-default" namespace="/"><default-action-ref name="index"/><global-results><result name="error">/error.jsp</result></global-results><global-exception-mappings><exception-mapping result="error" exception="java.lang.Exception"/></global-exception-mappings></package><include file="com/dahafo/um/config/strutsConfig/user.xml"/>
</struts>
user.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd">
<struts><package name="user" extends="default" namespace="/user" ><action name="newUser" class="userAction"><result name="success">/success.jsp</result></action></package>
</struts>
第三个是web.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><!-- 初始化spring配置 --><context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring*.xml</param-value> </context-param><!-- struts2 的拦截器 --> <filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- spring 监听器 --><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>
第四个是spring的配置文件 spring-config.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- dbcp数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/um" /><property name="username" value="qyl" /><property name="password" value="qyl" /><!-- 初始化数 --><property name="initialSize" value="2" /><!-- 最大连接数 --><property name="maxActive" value="5" /><!-- 最大空闲连接数 --><property name="maxIdle" value="1" /><!-- 最小空闲连接数 --><property name="minIdle" value="1" /></bean><!-- MyBatis在spring中Bean的配置,都是固定的 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="WEB-INF/mybatis.xml" /><property name="dataSource" ref="dataSource" /></bean><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg index="0" name="sqlSessionFactory" ref="sqlSessionFactory" /></bean><!-- 配置事务管理器,注意这里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事务就没有作用了--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean></beans>
spring-config-action.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="userAction" class="com.dahafo.um.action.UserAction" scope="prototype"><property name="userService" ref="userServiceImpl" /></bean>
</beans>
spring-config-service.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="userServiceImpl" class="com.dahafo.um.service.impl.UserServiceImpl"><property name="userDao" ref="userDaoImpl" /></bean></beans>
spring-Config-dao.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="userDaoImpl" class="com.dahafo.um.dao.impl.UserDaoImpl"><property name="sqlSession" ref="sqlSession" /></bean></beans>
spring的配置文件都放在WEB-INF下面。
第五个是mybatis的配置文件 mybatis.xml 也放在WEB-INF下了,当然可以在其他地方,同时spring-config.xml 里面的mybatis配置的文件路径也要变
<?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>
<typeAlias alias="user" type="com.dahafo.um.entity.User"/>
</typeAliases>
<mappers>
<mapper resource="/com/dahafo/um/config/mybatisConfig/userMapper.xml" />
</mappers>
</configuration>
userMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dahafo.um.entity.User"><resultMap type="com.dahafo.um.entity.User" id="userResult"><result property="name" column="name" /><result property="id" column="id" jdbcType="INTEGER" javaType="java.lang.Integer" /><result property="age" column="age" jdbcType="INTEGER" javaType="java.lang.Integer" /></resultMap><insert id="insertUser" parameterType="user">insert into user(name,age) values(#{name},#{age})</insert></mapper>
package com.dahafo.um.action;import com.dahafo.um.dao.UserDao;
import com.dahafo.um.entity.User;
import com.dahafo.um.service.UserService;
import com.opensymphony.xwork2.ActionSupport;/** * @author Zevi Qian E-mail:gosun@foxmail.com * @version 创建时间:2013-5-15 下午11:10:52 * 用户控制器*/
public class UserAction extends ActionSupport{private UserService userService;private User user=null;/* (non-Javadoc)* @see com.opensymphony.xwork2.ActionSupport#execute()*/@Overridepublic String execute() throws Exception {user=new User();user.setName("玩哈哈");user.setAge(532);System.out.println("------------到这里了");userService.newUser(user);return "success";}public UserService getUserService() {return userService;}public void setUserService(UserService userService) {this.userService = userService;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}
}
userService的实现类 userServiceImpl.java 接口就不写了,太简单,没意思,后面接口就都不写了
package com.dahafo.um.service.impl;
import com.dahafo.um.dao.UserDao;
import com.dahafo.um.entity.User;
import com.dahafo.um.service.UserService;
/**
* @author Zevi Qian E-mail:gosun@foxmail.com
* @version 创建时间:2013-5-15 下午10:54:09
* 类说明
*/
public class UserServiceImpl implements UserService {
private UserDao userDao;
/* (non-Javadoc)
* @see com.dahafo.um.service.UserService#newUser()
*/
@Override
public void newUser(User user) {
userDao.insertUser(user);
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
userDao的实现 UserDaoImpl.java
package com.dahafo.um.dao.impl;import java.util.List;import org.mybatis.spring.SqlSessionTemplate;import com.dahafo.um.dao.UserDao;
import com.dahafo.um.entity.User;/** * @author Zevi Qian E-mail:gosun@foxmail.com * @version 创建时间:2013-5-15 上午11:52:58 * 类说明 */
public class UserDaoImpl implements UserDao {private SqlSessionTemplate sqlSession;/* (non-Javadoc)* @see com.dahafo.um.dao.UserDao#insertUser(com.dahafo.um.entity.User)*/@Overridepublic void insertUser(User user) {sqlSession.insert("insertUser", user);}/* (non-Javadoc)* @see com.dahafo.um.dao.UserDao#findAll()*/@Overridepublic List<User> findAll() {return sqlSession.selectList("selectAllUser");}public SqlSessionTemplate getSqlSession() {return sqlSession;}public void setSqlSession(SqlSessionTemplate sqlSession) {this.sqlSession = sqlSession;}
}
好了,主要的代码都贴出来了,最后贴个成功的图片
看到这个图很兴奋的啊,几天的努力终于见成果了。
这个页面出现,说明三个框架架构并测试成功。
再看看数据库里是不是插入数据了?
很幸运的看到,我们new的那条用户数据在数据库里了。
结构的时候肯定会遇到许多问题,要有耐心,多找原因。
祝大家好运