下面详细讲解“struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo”的完整攻略。
一、环境配置
- 下载并安装Java、Tomcat和MySQL;
- 搭建好Java和Tomcat的环境,配置好MySQL数据库。
二、搭建Struts2框架
- 创建Maven项目,引入Struts2的依赖,具体如下:
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
- 创建struts.xml文件并配置Action映射,具体如下:
<?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 name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
<package name="mypackage" namespace="/" extends="struts-default">
<action name="hello" class="com.demo.action.HelloAction" method="execute">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
- 创建Action处理类并编写对应逻辑,具体如下:
package com.demo.action;
public class HelloAction {
public String execute() {
return "success";
}
}
- 创建JSP视图文件,具体如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello, Struts2!</h1>
</body>
</html>
- 部署到Tomcat服务器上并启动,访问 http://localhost:8080/hello.action 即可看到"Hello, Struts2!"的欢迎页面。
三、搭建Spring框架
- 引入Spring的依赖,具体如下:
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
</dependencies>
- 创建Spring配置文件applicationContext.xml并配置Bean,具体如下:
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Action -->
<bean id="helloAction" class="com.demo.action.HelloAction"/>
</beans>
- 在struts.xml中引入Spring相关配置,具体如下:
<?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 name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
<package name="mypackage" namespace="/" extends="struts-default">
<action name="hello" class="com.demo.action.HelloAction" method="execute">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
<result name="success">/hello.jsp</result>
</action>
</package>
<!-- 引入Spring相关配置 -->
<bean class="org.springframework.web.struts.StrutsSpringObjectFactory"/>
<constant name="struts.objectFactory" value="spring"/>
<include file="applicationContext.xml"/>
</struts>
- 部署到Tomcat服务器上并启动,访问 http://localhost:8080/hello.action 即可看到"Hello, Struts2!"的欢迎页面。
四、搭建MyBatis框架
- 引入MyBatis的依赖,具体如下:
<dependencies>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
- 编写MyBatis的配置文件mybatis-config.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>
<properties resource="jdbc.properties"/>
<typeAliases>
<package name="com.demo.entity"/>
</typeAliases>
<mappers>
<mapper resource="com/demo/dao/HelloMapper.xml"/>
</mappers>
</configuration>
-
配置数据源,随便一家dataSource的配置即可,比如c3p0。这里就不述了。
-
创建DAO接口HelloMapper.java,具体如下:
package com.demo.dao;
import com.demo.entity.Hello;
public interface HelloMapper {
public Hello selectById(int id);
}
- 创建DAO实现类HelloMapper.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.demo.dao.HelloMapper">
<resultMap id="helloResultMap" type="com.demo.entity.Hello">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="message" column="message"/>
</resultMap>
<select id="selectById" resultMap="helloResultMap" parameterType="int">
select * from hello where id = #{id}
</select>
</mapper>
- 创建实体类Hello.java,具体如下:
package com.demo.entity;
public class Hello {
private int id;
private String name;
private String message;
// 省略getter和setter
}
- 在Spring的配置文件applicationContext.xml中配置MyBatis相关组件,具体如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 省略数据源配置 -->
</bean>
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.demo.dao"/>
</bean>
<!-- HelloMapper对应的DAO -->
<bean id="helloMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.demo.dao.HelloMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 配置Action -->
<bean id="helloAction" class="com.demo.action.HelloAction">
<property name="helloMapper" ref="helloMapper"/>
</bean>
</beans>
- 在HelloAction中注入DAO,具体如下:
package com.demo.action;
import com.demo.dao.HelloMapper;
import com.demo.entity.Hello;
public class HelloAction {
private HelloMapper helloMapper;
public String execute() {
Hello hello = helloMapper.selectById(1);
return "success";
}
public void setHelloMapper(HelloMapper helloMapper) {
this.helloMapper = helloMapper;
}
}
- 在struts.xml中注入Spring相关组件,具体如下:
<?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 name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
<package name="mypackage" namespace="/" extends="struts-default">
<action name="hello" class="com.demo.action.HelloAction" method="execute">
<interceptor-ref name="defaultStack">
<param name="exception.logEnabled">true</param>
<param name="exception.logLevel">ERROR</param>
</interceptor-ref>
<result name="success">/demo/hello.jsp</result>
</action>
</package>
<!-- 引入Spring相关配置 -->
<bean class="org.springframework.web.struts.StrutsSpringObjectFactory"/>
<constant name="struts.objectFactory" value="spring"/>
<include file="classpath*:/spring/applicationContext.xml"/>
</struts>
- 部署到Tomcat服务器上并启动,访问 http://localhost:8080/hello.action 即可看到"Hello, Struts2!"的欢迎页面。
五、示例场景
- 实现一个用户管理功能,包含用户列表展示、用户详情查看、用户新增和编辑等功能。
- 基于文件上传实现一个图片上传功能。
本示例仅涉及Struts2、Spring和MyBatis框架的整合集成,具体的业务逻辑实现需要读者根据实际需求进行开发。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成简单demo - Python技术站