Eclipse中 struts2 + spring3 + mybatis3 环境搭建
来源:互联网 发布:java中过滤器的作用 编辑:程序博客网 时间:2024/05/20 23:05
公司要用struts2 + spring3 + mybatis3做项目,尝试着在家鼓捣了一下。
1. 框架下载
struts2: http://struts.apache.org/ 下载 struts-2.3.14-all.zip
spring3: http://www.springsource.org/spring-framework 下载 spring-framework-3.2.2-dist.zip
mybatis3: http://code.google.com/p/mybatis/ 下载 mybatis-3.2.2.zip 和 mybatis-spring-1.2.0-bundle.zip
2. 建示例工程
在Eclipse中新建示例工程,步骤就不详述了,我把工程取名为ssm_example。
3. struts2配置
3.1 src下创建struts.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><constant name="struts.devMode" value="true" /><package name="basic" extends="struts-default"><action name="index" class="cn.ssm.sample.action.IndexAction" method="execute"><result name="success">/WEB-INF/jsp/Index.jsp</result></action></package></struts>
3.2 修改web.xml
<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>
3.3 从解压的struts-2.3.14-all的lib文件夹下,挑选了必须的和比较常用的jar包,放入WEB-INF/lib的文件夹下
commons-fileupload-1.2.2.jarcommons-io-2.0.1.jarcommons-lang-2.4.jarcommons-lang3-3.1.jarcommons-logging-1.1.1.jarcommons-logging-api-1.1.jarfreemarker-2.3.19.jarjavassist-3.11.0.GA.jarognl-3.0.6.jarstruts2-core-2.3.14.jarxwork-core-2.3.14.jar
3.4 建个测试用的IndexAction.java和Index.jsp
package cn.ssm.sample.action;import com.opensymphony.xwork2.ActionSupport;public class IndexAction extends ActionSupport{@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubreturn super.execute();}}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>Hello world!</body></html>
3.5 测试struts2
运行服务,在浏览器输入http://localhost:8080/ssm_example/index,如果配置没有错误,那么会出现Hello world!
4. spring3配置
4.1 config/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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- enable component scanning (beware that this does not enable mapper scanning!) --><context:component-scanbase-package="cn.ssm.sample.action,cn.ssm.sample.service" /><!-- enable autowire --><context:annotation-config /><!-- enable transaction demarcation with annotations --><tx:annotation-driven /></beans>
4.2 修改web.xml
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext.xml </param-value></context-param>
4.3 添加相关jar包
aopalliance-1.0.jarspring-aop-3.2.2.RELEASE.jarspring-beans-3.2.2.RELEASE.jarspring-context-3.2.2.RELEASE.jarspring-core-3.2.2.RELEASE.jarspring-expression-3.2.2.RELEASE.jarspring-jdbc-3.2.2.RELEASE.jarspring-tx-3.2.2.RELEASE.jarspring-web-3.2.2.RELEASE.jarstruts2-spring-plugin-2.3.14.jar
4.4 修改struts.xml文件
添加:<constant name="struts.objectFactory" value="spring"></constant>
并且把class="cn.ssm.sample.action.IndexAction"改成class="indexAction"
4.5 修改Action
在IndexAction的类上,加上@Controller,这样Spring就能自动实例化成indexAction对象了。
4.6 测试spring
运行服务,在浏览器输入http://localhost:8080/ssm_example/index,如果配置没有错误,那么依然会出现Hello world!
5. mybatis3配置
5.1 数据库准备
这里采用mysql,下面的sql文来运行。
CREATE SCHEMA `ssmsample` DEFAULT CHARACTER SET utf8 ;CREATE TABLE `ssmsample`.`user` (`id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(45) NULL, PRIMARY KEY('id') );INSERT INTO `ssmsample`.`user` (`id`,`name`) VALUES (1,'Ethan');
5.2 修改applicationContext.xml
<!-- 数据源配置 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url"value="jdbc:mysql://localhost:3306/ssmsample?useUnicode=true&characterEncoding=utf8"></property><property name="username" value="root"></property><property name="password" value="admin"></property></bean><!-- transaction manager, use JtaTransactionManager for global tx --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean> <!-- define the SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="typeAliasesPackage" value="cn.ssm.sample.dto" /> </bean> <!-- scan for mappers and let them be autowired --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.ssm.sample.dao" /> </bean>
5.3 创建和修改相关类
UserMapper.java
package cn.ssm.sample.dao;import cn.ssm.sample.dto.User;public interface UserMapper {User getUser(int id);}
UserMapper.xml(和UserMapper.java同目录)
<?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="cn.ssm.sample.dao.UserMapper"><select id="getUser" parameterType="int" resultType="User">SELECT *From user where id = #{id}</select></mapper>
User.java
package cn.ssm.sample.dto;public class User {int id;String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
IndexSvr.java
package cn.ssm.sample.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import cn.ssm.sample.dao.UserMapper;import cn.ssm.sample.dto.User;@Servicepublic class IndexSvr {@AutowiredUserMapper userMapper;public User getUser(int id) {return userMapper.getUser(id);}}
IndexAction.java
package cn.ssm.sample.action;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import cn.ssm.sample.dto.User;import cn.ssm.sample.service.IndexSvr;import com.opensymphony.xwork2.ActionSupport;@Controllerpublic class IndexAction extends ActionSupport{private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}@AutowiredIndexSvr indexSvr;@Overridepublic String execute() throws Exception {user = indexSvr.getUser(1);return super.execute();}}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>Hello world! ${user.name }</body></html>
5.4 添加jar包
commons-dbcp-1.4.jarcommons-pool-1.6.jarmybatis-3.2.2.jarmybatis-spring-1.2.0.jarmysql-connector-java-5.1.13-bin.jar
5.5 测试ssm
运行服务,在浏览器输入http://localhost:8080/ssm_example/index,如果配置没有错误,那么依然会出现Hello world!Ethan
- Eclipse中 struts2 + spring3 + mybatis3 环境搭建
- Eclipse中 struts2 + spring3 + mybatis3 环境搭建
- Struts2+Spring3+Mybatis3开发环境搭建
- Struts2+Spring3+Mybatis3开发环境搭建
- Struts2+Spring3+Mybatis3开发环境搭建
- Struts2+Spring3+Mybatis3开发环境搭建
- Struts2+Spring3+Mybatis3开发环境搭建
- Struts2+Spring3+Mybatis3开发环境搭建
- Struts2+Spring3.0+MyBatis3.0平台搭建
- Struts2+Spring3.0+MyBatis3.0平台搭建
- Spring3.2.4 + Mybatis3.1.1环境搭建
- MyBatis3+Spring3+Struts2配置文件
- Struts2+spring3+mybatis3整合
- spring3+struts2+mybatis3 -2
- Spring3+Struts2+Mybatis3 -3
- Spring3.2+Struts2.1.8+Mybatis3.2.7 项目搭建
- spring3+struts2环境搭建中遇到的一些问题
- myBatis3,spring3,struts2.2整合
- 全排列:穷举法和递归法
- iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效
- 透明桥接
- Java设计模式-读《head first 设计模式》
- Effective C++学习笔记之第五章(2)
- Eclipse中 struts2 + spring3 + mybatis3 环境搭建
- 五个免费UML建模工具推荐
- 项目延期的⑦大因素
- 对于.Net中C#指针的研究
- 【数位DP】双峰数Bi-peak Number HDU3565 (目前TLE。。。)
- 常用库函数编程实现用法总结(二)strstr strcat
- 了解记忆这些,才能说熟练使用Eclipse
- DWZ 动态表单提交数据 action接收值
- matlab画图初试