以下是一个简单的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>

最新文章

  1. [LeetCode] Subsets 子集合
  2. hadoop2.0初识1.0
  3. 什么是网络爬虫(Spider) 程序
  4. PLSQL 的简单命令之五
  5. SWIFT Button的基本用法
  6. C#实现ByteBuffer类 .
  7. AOJ 2200 Mr. Rito Post Office
  8. ARM公布“物联网”嵌入式mbed OS系统软件平台
  9. uboot环境变量分析
  10. Android WebView 小结
  11. cer, pfx 创建,而且读取公钥/密钥,加解密 (C#程序实现)
  12. JAVA中pdf转图片的方法
  13. 35 个 jQuery 小技巧
  14. maven插件运行过程中自动执行sql文件
  15. C# Parallel并发执行相关问题
  16. day11_雷神_udp、多进程等
  17. 数据对接—kettle使用之四
  18. 【BZOJ1835】基站选址(线段树)
  19. swoole gets
  20. linux shell 一些命令

热门文章

  1. Android BroadcastReceiver解析
  2. 用NDK调用第三方库
  3. 使用Groovy的sql模块操作mysql进行多种查询
  4. Protues常用元器件查找对应表
  5. Ubuntu14.04+caffe+CPU
  6. FarBox的建站过程
  7. php学习之mysqli的面向对象
  8. centos6环境创建局域网http方式的yum源
  9. 队列queue 代码
  10. 无法下载apk等格式的文件的解决方案---ASP .NET Core 2.0 MVC 发布到IIS上以后无法下载apk等格式的文件的解决方案