以下是一个简单的ssm项目:如果中途报错,肯定是tomcat配置或者数据库配置有问题,在程序中注意将包名等配置换成自己的。数据库表需要提前建好,并加入数据,注意表结构要和实体对象对应。

1.开发条件:eclipse + comcat7.0

2.创建一个Dynamic Web Project项目,大概的目录结构。

3.jar包如下:

aopalliance.jar
asm-3.3.1.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
aspectjweaver.jar
cglib-2.2.2.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.19.jar
javassist-3.17.1-GA.jar
jstl-1.2.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.3.jar
mysql-connector-java-5.1.20-bin.jar
ognl-3.0.5.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
standard-1.1.2.jar
struts2-core-2.3.4.jar
struts2-spring-plugin-2.3.4.jar

xwork-core-2.3.4.jar

4.页面访问结果:http://localhost:8080/spring001/list.action

下面开始上代码:

1.UserDao

package cn.mr.li.dao;

import java.util.List;

import cn.mr.li.entity.User;

public interface UserDao {

    List<User> getUser();
}

2.UserDaoImpl

package cn.mr.li.dao.impl;

import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.mr.li.dao.UserDao;
import cn.mr.li.entity.User; public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { @Override
public List<User> getUser() {
return this.getSqlSession().selectList("cn.mr.li.entity.user.mapper.getAll");
}
}

3.userService

package cn.mr.li.service;

import java.util.List;

import cn.mr.li.entity.User;

public interface UserService {

    List<User> getAll();
}

4.userServiceImpl

package cn.mr.li.service.impl;

import java.util.List;

import cn.mr.li.dao.UserDao;
import cn.mr.li.entity.User;
import cn.mr.li.service.UserService; public class UserServiceImpl implements UserService { private UserDao userDao; @Override
public List<User> getAll() {
return userDao.getUser();
} public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}

5.user

package cn.mr.li.entity;

public class User {

    private int id;

    private String name;

    private int age;

    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;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
} public User() {
}
}

6.user-mapper.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="cn.mr.li.entity.user.mapper">
<select id="getAll" resultType="User">
select * from user
</select>
</mapper>

7.UserAction

package cn.mr.li.action;

import java.util.List;

import cn.mr.li.entity.User;
import cn.mr.li.service.UserService; public class UserAction {
private List<User> list;
private UserService userService; public String list(){
list = userService.getAll();
return "success";
} public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}

8.config.spring  user.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="userDao" class="cn.mr.li.dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<bean id="userService" class="cn.mr.li.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="userAction" class="cn.mr.li.action.UserAction" scope="prototype">
<property name="userService" ref="userService"/>
</bean> </beans>

9.config.struts  user.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="user" namespace="/" extends="struts-default">
<action name="list" class="userAction" method="list">
<result>/list.jsp</result>
</action>
</package>
</struts>

10applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!-- 声明式事务配置 开始 -->
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 配置哪些方法使用什么样的事务,配置事务的传播特性 -->
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="insert" propagation="REQUIRED"/>
<tx:method name="update" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="get" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.mr.li.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
<!-- 声明式事务配置 结束 -->
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>
</bean> <import resource="config/spring/user.xml"/>
</beans>

11.mybatis-cfg.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>
<typeAliases>
<package name="cn.mr.li.entity"/>
</typeAliases>
<mappers>
<mapper resource="cn/mr/li/entity/user.mapper.xml"/>
</mappers>
</configuration>

12.struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<include file="config/struts/user.xml"></include>
</struts>

13.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 配置spring:配合全局变量,将由contextConfigLocation去读取spring得配置,不在需要在程序中读取了 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器:此监听器在启动初始化的时候在它的构造器中初始化全局上下文,
因此需要将spring的配置也加载进去,因为spring中有对象实例 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置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>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

14.index.jsp,好像没用到

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
This is my JSP page. <br>
</body>
</html>

15.list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<table width="80%" align="center">
<tr>
<td>编号</td>
<td>姓名</td>
<td>密码</td>
</tr>
<c:forEach items="${list }" var="bean">
<tr>
<td>${bean.id }</td>
<td>${bean.name }</td>
<td>${bean.age }</td>
</tr>
</c:forEach>
</table>
</body>
</html>

spring整合ssmXML版的更多相关文章

  1. spring整合mybatisXML版

    引用的所有jar包,本demo是一个普通的java项目测试的: aopalliance.jarasm-3.3.1.jaraspectjweaver.jarcglib-2.2.2.jarcommons- ...

  2. 手写Mybatis和Spring整合简单版示例窥探Spring的强大扩展能力

    Spring 扩展点 **本人博客网站 **IT小神 www.itxiaoshen.com 官网地址****:https://spring.io/projects/spring-framework T ...

  3. Spring整合MyBatis整合

    1.导入所需要的jar依赖 !--MyBatis和Spring的整合包 由MyBatis提供--> <dependency> <groupId>org.mybatis&l ...

  4. MyBatis和Spring整合案例

    1.所需要导入的jar文件 !--MyBatis和Spring的整合包 由MyBatis提供--> <dependency> <groupId>org.mybatis&l ...

  5. Spring整合Mybatis报 java.lang.ClassNotFoundException:org.springframework.core.metrics.ApplicationStartup,即:spring的版本过高,采用RELEASE稳定版

    1.遇到的问题: 今天在弄spring整合mybatis的时候遇到一个小问题,如图所示: 简单来说:就是我的spring的xml文件没找到,我就奇了怪了,我所有的配置都没问题啊! 我pom.xml配置 ...

  6. Spring学习总结(六)——Spring整合MyBatis完整示例

    为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...

  7. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)一

    MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中. 使用这个类库中的类, Spring 将会加载必要的MyBatis工厂类和 session 类. 这个类库 ...

  8. redis集群配置,spring整合jedis,缓存同步

    前台的商品数据(图片等加载缓慢)查询,先从redis缓存查询数据. redis是一个nosql数据库,内存版数据库,读取速度11w/s.本身具有内存淘汰机制,是单线程服务器(分时操作系统),线程安全. ...

  9. Spring整合Redis&JSON序列化&Spring/Web项目部署相关

    几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...

随机推荐

  1. Struts的JSON机制

    需要加入jar包 Struts的JSON帮助我们自动将对象解析为JSON对象,不用我门借助第三方进行JSON的解析 .具体的使用机制如下: 1.Action类 package StrutsJSON; ...

  2. MYSQL和oracle 大小写问题

    MySQL的大小写问题 不是一个很大的问题,但是如果不了解的话,却会使用户产生迷惑 :如下面 Sql代码insert into t values('A'); insert into t values( ...

  3. Eclipse中项目不会自动编译问题的坑和注意点

    最近接受了几个又小有老的项目,用eclipse反而比idea方便,但是好长时间不用eclipse了,还有有些问题的! 主要是碰到了classnotfound这个难缠的问题:这里记录一下几个坑,避免以后 ...

  4. Dubbo重试次数

    服务超时后重试次数[retries],不包含第一次调用,0代表不重试 *我们应该在幂等方法上设置重试次数[查询.删除.修改],在非幂等方法上禁止设置重试次数. ★幂等:指多次运行方法所产生的最终效果是 ...

  5. 【CTF MISC】隐写术wireshark找出图片-“强网杯”网络安全挑战赛writeup

    这场CTF中有一道题是分析pcap包的.. 13.大黑阔: 从给的pcap包里把图片提取出来,是一张中国地图. 题目提示是黑阔在聊天,从数据里可以找出几段话. 思路:主要考察wireshark的过滤规 ...

  6. Python中的exec、eval使用实例

    Python中的exec.eval使用实例 这篇文章主要介绍了Python中的exec.eval使用实例,本文以简洁的方式总结了Python中的exec.eval作用,并给出实例,需要的朋友可以参考下 ...

  7. MR目录结构

    D:\MRData\MR\MRS\2017-05-25\TD-LTE_MRS_ZTE_OMC1_20170425000000.zip 每个zip中包含若干FDD-LTE_MRS_OMC1_28163_ ...

  8. 【转】通过xml处理sql语句时对小于号与大于号的处理转换

    当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...

  9. Visual Studio 2017 + Python3.6安装scipy库

    Windows10下安装scipy很麻烦,直接在命令行下使用pip install scipy无法安装,但可以借助VS2017的集成环境来安装. (1)首先在Visual Studio Install ...

  10. saltstack自动化运维系列⑥SaltStack实践安装配置HAproxy的Keepalived

    saltstack自动化运维系列⑥SaltStack实践安装配置HAproxy的Keepalived 安装配置Keepalived 1.编写功能模块 #创建keepalived目录# mkdir -p ...