工具:jdk1.7+eclipse+tomcat+mysql。

这里用的版本是spring3,框架中用到的实体类和xml映射文件都可以用工具生成的。接下来会将源码贴出,方便初学者快速搭建。

一、新建一个web工程,添加目录包结构如下。

二、添加ssm所用的jar包。

三,修改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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>Encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- spring mvc核心:分发servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring mvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!-- 此处配置成*.do,对应struts的后缀习惯 -->
<url-pattern>*.do</url-pattern>
<!-- 此处配置成*.json,是为了能够截获"../*.json"格式的访问(例如android客户端) -->
<url-pattern>*.json</url-pattern>
</servlet-mapping> </web-app>

四、添加applicationContext.xml和springMVC.xml配置文件。并修改配置文件中对应的资源路径。这里先贴出各个文件的位置。

1.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:annotation-config />
<context:component-scan base-package="com.gh.service" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/young?characterEncoding=UTF-8</value> </property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.gh.po" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/gh/mapper/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.gh.dao,com.gh.mapper"/>
</bean>
</beans>

2.springMVC.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/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.2.xsd">
<context:annotation-config/> <context:component-scan base-package="com.gh.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- <property name="prefix" value="/WEB-INF/jsp/" /> -->
<property name="suffix" value=".jsp" />
</bean>
</beans>

五.编写测试代码。实现简单查询。

UserMapper.java

package com.gh.dao;

import java.util.List;

import com.gh.po.User;

public interface UserMapper {
int deleteByPrimaryKey(Integer userid); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer userid); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record);
List<User> selectAll();
}

UserMapper.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.gh.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.gh.po.User" >
<id column="userid" property="userid" jdbcType="INTEGER" />
<result column="userName" property="username" jdbcType="VARCHAR" />
<result column="pwd" property="pwd" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
userid, userName, pwd
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where userid = #{userid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where userid = #{userid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.gh.po.User" >
insert into user (userid, userName, pwd
)
values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{pwd,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.gh.po.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userid != null" >
userid,
</if>
<if test="username != null" >
userName,
</if>
<if test="pwd != null" >
pwd,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userid != null" >
#{userid,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="pwd != null" >
#{pwd,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.gh.po.User" >
update user
<set >
<if test="username != null" >
userName = #{username,jdbcType=VARCHAR},
</if>
<if test="pwd != null" >
pwd = #{pwd,jdbcType=VARCHAR},
</if>
</set>
where userid = #{userid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.gh.po.User" >
update user
set userName = #{username,jdbcType=VARCHAR},
pwd = #{pwd,jdbcType=VARCHAR}
where userid = #{userid,jdbcType=INTEGER}
</update>
<select id="selectAll" resultType="com.gh.po.User">
select * from user
</select >
</mapper>

User.java

package com.gh.po;

public class User {
private Integer userid; private String username; private String pwd; public Integer getUserid() {
return userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
}
}

UserService.java

package com.gh.service;

import com.gh.dao.UserMapper;
public interface UserService extends UserMapper{ }

UserServiceimpl.java

package com.gh.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.gh.dao.UserMapper;
import com.gh.po.User;
import com.gh.service.UserService;
@Transactional
@Service
public class UserServiceimpl implements UserService{
@Autowired
private UserMapper us;
@Override
public int deleteByPrimaryKey(Integer userid) {
// TODO Auto-generated method stub
return 0;
} @Override
public int insert(User record) {
// TODO Auto-generated method stub
return 0;
} @Override
public int insertSelective(User record) {
// TODO Auto-generated method stub
return 0;
} @Override
public User selectByPrimaryKey(Integer userid) {
// TODO Auto-generated method stub
return null;
} @Override
public int updateByPrimaryKeySelective(User record) {
// TODO Auto-generated method stub
return 0;
} @Override
public int updateByPrimaryKey(User record) {
// TODO Auto-generated method stub
return 0;
} @Override
public List<User> selectAll() {
// TODO Auto-generated method stub
return us.selectAll();
} }

最好编写测试代码:

 package com.gh.controller;

 import java.util.List;

 import javax.annotation.Resource;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gh.po.User;
import com.gh.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class UserController {
@Resource
private UserService us;
@Test
public void selectAllUser(){
List <User> list=us.selectAll();
if(list.size()>0){
for (User user : list) {
System.out.println(user.getUsername());
}
} } }

打印出信息如下:说明框架成功运转。

[org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
  [org.springframework.context.support.GenericApplicationContext] - Refreshing org.springframework.context.support.GenericApplicationContext@1fe398a0: startup date [Tue Apr 24 16:00:53 CST 2018]; root of context hierarchy
  [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2f0cccfb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,userServiceimpl,dataSource,sqlSession,org.mybatis.spring.mapper.MapperScannerConfigurer#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,userMapper]; root of factory hierarchy
  [org.springframework.jdbc.datasource.DriverManagerDataSource] - Loaded JDBC driver: com.mysql.jdbc.Driver
  华哥
陈哥
光哥
[org.springframework.context.support.GenericApplicationContext] - Closing org.springframework.context.support.GenericApplicationContext@1fe398a0: startup date [Tue Apr 24 16:00:53 CST 2018]; root of context hierarchy
  [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2f0cccfb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,userServiceimpl,dataSource,sqlSession,org.mybatis.spring.mapper.MapperScannerConfigurer#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,userMapper]; root of factory hierarchy

eclipse中搭建ssm框架的更多相关文章

  1. 【原】无脑操作:eclipse + maven搭建SSM框架

    网上看到一些Spring + Spring MVC + MyBatis框架的搭建教程,不是很详细或是时间久远了,自己动手整一个简单无脑的! 0.系统环境 1)Windows 10 企业版 2)JDK ...

  2. eclipse + maven搭建SSM框架

    0.系统环境 1)Windows 10 企业版 2)JDK 1.8.0_131 3)Eclipse Java EE IDE for Web Developers  Version: Neon.3 Re ...

  3. Eclipse中使用Maven搭建SSM框架

    Eclipse中不使用Maven搭建SSM框架:https://www.cnblogs.com/xuyiqing/p/9569459.html IDEA中使用Maven搭建SSM框架:https:// ...

  4. 使用Maven搭建SSM框架(Eclipse)

    今天学习一下使用Maven搭建SSM框架,以前都是用别人配置好的框架写代码,今天试试自己配置一下SSM框架. 这里我的参数是Windows7 64位,tomcat9,eclipse-jee-neon- ...

  5. eclipse中SSH三大框架环境搭建<三>

    相关链接: eclipse中SSH三大框架环境搭建<一> eclipse中SSH三大框架环境搭建<二> 引言:通过上两篇文章我们已经可以掌握struts2和spring的环境的 ...

  6. eclipse中SSH三大框架环境搭建<二>

    通过上一篇博客我们可以轻松搭建strtus2的环境,接下来由我来继续介绍spring的环境搭建以及spring注入的简单使用 相关链接:eclipse中SSH三大k框架环境搭建<一> ec ...

  7. eclipse中SSH三大框架环境搭建<一>

    这里先简单介绍一下我用的三大框架版本以及下载地址 相关链接:eclipse中SSH三大框架环境搭建<二> eclipse中SSH三大框架环境搭建<三> struts-2.3.3 ...

  8. 快速搭建ssm框架

    快速搭建SSM框架 因为最近有很多朋友问我自己的项目搭建的不够完善,并且经常出现一些小问题,那么今天我又整理了一下文档教大家如何快速搭建SSM框架我是用 eclipse搭建的,如果想用idear的话我 ...

  9. 使用maven搭建ssm框架的javaweb项目

    目前主流的javaweb项目,常会用到ssm(Spring+Spring MVC+Mybatis)框架来搭建项目的主体框架,本篇介绍搭建SSM框架的maven项目的实施流程.记之共享! 一.SSM框架 ...

随机推荐

  1. python1.返回一个字符串中出现次数第二多的单词 2.字符串中可能有英文单词、标点、空格 3.字符串中的英文字符全部是小写

    import re from collections import Counter def second_count_word(s): # # 利用正则按标点和空格切割,有其他标点可以添加到[]内 # ...

  2. 在docker集群下,使用VNC,物理机器重启后VNC失败解决

    首先重装咯: sudo apt-get update sudo apt-get install ubuntu-desktop gnome-panel gnome-settings-daemon met ...

  3. unittest框架(惨不忍睹低配版)

    根据我上个随笔的unittest框架优化得来,虽然对于smtp模块还是有点迷糊,不过还是勉强搭建运行成功了,还是先上代码: #login_test.py import requests class L ...

  4. python测试开发django-2.templates模板与html页

    前言 Django 中的视图的概念是一类具有相同功能和模板的网页的集合.通俗一点来说,就是你平常打开浏览器,看到浏览器窗口展示出来的页面内容,那就是视图.前面一章通过浏览器访问http://127.0 ...

  5. Django Rest Framework 视图和路由

    Django Rest Framework 视图和路由   DRF的视图 APIView 我们django中写CBV的时候继承的是View,rest_framework继承的是APIView,那么他们 ...

  6. 浅谈https和http2

    最近被问到http2和https的相关知识,这是一篇补基础的博客: 首先想说下https 什么是https https的主要用处: - 建立信息安全通道,保证数据传输的安全 - 确保网站的真实性,防止 ...

  7. mysql学习笔记--数据库索引

    一.索引的优点:查询速度快 二.索引的缺点: 1. 增.删.改(数据操作语句)效率低了 2. 索引占用空间 三.索引类型: 1. 普通索引 2. 唯一索引(唯一键) 3. 主键索引:只要主键就自动创建 ...

  8. Android 代码判断是否有网络

    public void okGo() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemServic ...

  9. LeetCode解题录-51~100

    [leetcode]51. N-QueensN皇后    Backtracking Hard [leetcode]52. N-Queens II N皇后 Backtracking Hard [leet ...

  10. 5N - 考试排名

    C++编程考试使用的实时提交系统,具有即时获得成绩排名的特点.它的功能是怎么实现的呢? 我们做好了题目的解答,提交之后,要么“AC”,要么错误,不管怎样错法,总是给你记上一笔,表明你曾经有过一次错误提 ...