用struts2 + spring3 + mybatis3做项目

公司要用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配置文件

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <constant name="struts.devMode" value="true" />  
  9.   
  10.     <package name="basic" extends="struts-default">  
  11.         <action name="index" class="cn.ssm.sample.action.IndexAction" method="execute">  
  12.             <result name="success">/WEB-INF/jsp/Index.jsp</result>  
  13.         </action>  
  14.     </package>  
  15.   
  16. </struts>  

3.2 修改web.xml

[html]  view plain copy
  1. <filter>  
  2.         <filter-name>struts2</filter-name>  
  3.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  4.     </filter>  
  5.   
  6.     <filter-mapping>  
  7.         <filter-name>struts2</filter-name>  
  8.         <url-pattern>/*</url-pattern>  
  9.     </filter-mapping>  

3.3 从解压的struts-2.3.14-all的lib文件夹下,挑选了必须的和比较常用的jar包,放入WEB-INF/lib的文件夹下

[html]  view plain copy
  1. commons-fileupload-1.2.2.jar  
  2. commons-io-2.0.1.jar  
  3. commons-lang-2.4.jar  
  4. commons-lang3-3.1.jar  
  5. commons-logging-1.1.1.jar  
  6. commons-logging-api-1.1.jar  
  7. freemarker-2.3.19.jar  
  8. javassist-3.11.0.GA.jar  
  9. ognl-3.0.6.jar  
  10. struts2-core-2.3.14.jar  
  11. xwork-core-2.3.14.jar  

3.4 建个测试用的IndexAction.java和Index.jsp

[java]  view plain copy
  1. package cn.ssm.sample.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class IndexAction extends ActionSupport{  
  6.   
  7.     @Override  
  8.     public String execute() throws Exception {  
  9.         // TODO Auto-generated method stub  
  10.         return super.execute();  
  11.     }  
  12.   
  13. }  

[html]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     Hello world!  
  11. </body>  
  12. </html>  

3.5 测试struts2

运行服务,在浏览器输入http://localhost:8080/ssm_example/index,如果配置没有错误,那么会出现Hello world!

4. spring3配置

4.1 config/spring下创建applicationContext.xml配置文件

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xsi:schemaLocation="  
  8.      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
  11.      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  12.      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  13.   
  14.     <!-- enable component scanning (beware that this does not enable mapper   
  15.         scanning!) -->  
  16.     <context:component-scan  
  17.         base-package="cn.ssm.sample.action,cn.ssm.sample.service" />  
  18.   
  19.     <!-- enable autowire -->  
  20.     <context:annotation-config />  
  21.   
  22.     <!-- enable transaction demarcation with annotations -->  
  23.     <tx:annotation-driven />  
  24.       
  25. </beans>  

4.2 修改web.xml

[html]  view plain copy
  1. <listener>  
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3. </listener>  
  4. <context-param>  
  5.     <param-name>contextConfigLocation</param-name>  
  6.     <param-value>/WEB-INF/classes/applicationContext.xml  
  7.        </param-value>  
  8. </context-param>  

4.3 添加相关jar包

[html]  view plain copy
  1. aopalliance-1.0.jar  
  2. spring-aop-3.2.2.RELEASE.jar  
  3. spring-beans-3.2.2.RELEASE.jar  
  4. spring-context-3.2.2.RELEASE.jar  
  5. spring-core-3.2.2.RELEASE.jar  
  6. spring-expression-3.2.2.RELEASE.jar  
  7. spring-jdbc-3.2.2.RELEASE.jar  
  8. spring-tx-3.2.2.RELEASE.jar  
  9. spring-web-3.2.2.RELEASE.jar  
  10. struts2-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文来运行。

[sql]  view plain copy
  1. CREATE SCHEMA `ssmsample` DEFAULT CHARACTER SET utf8 ;  
  2. CREATE  TABLE `ssmsample`.`user` (`id` INT NOT NULL AUTO_INCREMENT, `nameVARCHAR(45) NULLPRIMARY KEY('id') );  
  3. INSERT INTO `ssmsample`.`user` (`id`,`name`) VALUES (1,'Ethan');  

5.2 修改applicationContext.xml

[html]  view plain copy
  1. <!-- 数据源配置 -->  
  2. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  3.     destroy-method="close">  
  4.     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  5.     <property name="url"  
  6.         value="jdbc:mysql://localhost:3306/ssmsample?useUnicode=true&characterEncoding=utf8"></property>  
  7.     <property name="username" value="root"></property>  
  8.     <property name="password" value="admin"></property>  
  9. </bean>  
  10.   
  11. <!-- transaction manager, use JtaTransactionManager for global tx -->  
  12. <bean id="transactionManager"  
  13.     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  14.     <property name="dataSource" ref="dataSource" />  
  15. </bean>  
  16.   
  17.    <!-- define the SqlSessionFactory -->  
  18.    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  19.        <property name="dataSource" ref="dataSource" />  
  20.        <property name="typeAliasesPackage" value="cn.ssm.sample.dto" />  
  21.    </bean>  
  22.   
  23.    <!-- scan for mappers and let them be autowired -->  
  24.    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  25.        <property name="basePackage" value="cn.ssm.sample.dao" />  
  26.    </bean>  

5.3 创建和修改相关类

UserMapper.java

[java]  view plain copy
  1. package cn.ssm.sample.dao;  
  2.   
  3. import cn.ssm.sample.dto.User;  
  4.   
  5. public interface UserMapper {  
  6.     User getUser(int id);  
  7. }  

UserMapper.xml(和UserMapper.java同目录)

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4.   
  5. <mapper namespace="cn.ssm.sample.dao.UserMapper">  
  6.     <select id="getUser" parameterType="int" resultType="User">  
  7.         SELECT *  
  8.         From user where id = #{id}  
  9.     </select>  
  10. </mapper>  

User.java

[java]  view plain copy
  1. package cn.ssm.sample.dto;  
  2.   
  3. public class User {  
  4.     int id;  
  5.     String name;  
  6.   
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.   
  11.     public void setId(int id) {  
  12.         this.id = id;  
  13.     }  
  14.   
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.   
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22. }  

IndexSvr.java

[java]  view plain copy
  1. package cn.ssm.sample.service;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Service;  
  5.   
  6. import cn.ssm.sample.dao.UserMapper;  
  7. import cn.ssm.sample.dto.User;  
  8.   
  9. @Service  
  10. public class IndexSvr {  
  11.       
  12.     @Autowired  
  13.     UserMapper userMapper;  
  14.       
  15.     public User getUser(int id) {  
  16.         return userMapper.getUser(id);  
  17.     }  
  18. }  

IndexAction.java

[java]  view plain copy
  1. package cn.ssm.sample.action;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Controller;  
  5.   
  6. import cn.ssm.sample.dto.User;  
  7. import cn.ssm.sample.service.IndexSvr;  
  8.   
  9. import com.opensymphony.xwork2.ActionSupport;  
  10.   
  11. @Controller  
  12. public class IndexAction extends ActionSupport{  
  13.   
  14.     private User user;  
  15.       
  16.     public User getUser() {  
  17.         return user;  
  18.     }  
  19.   
  20.     public void setUser(User user) {  
  21.         this.user = user;  
  22.     }  
  23.   
  24.     @Autowired  
  25.     IndexSvr indexSvr;  
  26.   
  27.     @Override  
  28.     public String execute() throws Exception {  
  29.         user = indexSvr.getUser(1);  
  30.         return super.execute();  
  31.     }  
  32.   
  33. }  

index.jsp

[html]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     Hello world! ${user.name }  
  11. </body>  
  12. </html>  

5.4 添加jar包

[html]  view plain copy
  1. commons-dbcp-1.4.jar  
  2. commons-pool-1.6.jar  
  3. mybatis-3.2.2.jar  
  4. mybatis-spring-1.2.0.jar  
  5. mysql-connector-java-5.1.13-bin.jar  

5.5 测试ssm

运行服务,在浏览器输入http://localhost:8080/ssm_example/index,如果配置没有错误,那么依然会出现Hello world!Ethan

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值