前言

SSM就是Spring+SpringMvc+Mybatis,本文搭建一个基本的ssm框架

本文所有源代码包含jar包下载点击:https://download.csdn.net/download/wcc27857285/10667045

正文

Eclipse新建web dynamic project

一.项目结构图:

二.mysql数据库创建好user表:(数据库名为firstdb)

三.引入JAR包:

四.开始正式搭建项目

配置文件

web.xml     

我们在web.xml中加载Spring配置,并且将所有的请求都过滤给Spring MVC来处理,同时设置编码过滤器解决编码问题(最后一项可以不配置)。 其中Spring MVC的请求过滤就是一个简单的Servlet配置。

<?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_2_5.xsd"
id="WebApp_ID" version="2.5"> <!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring容器加载所有的配置文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:configxml/applicationContext.xml</param-value>
</context-param> <servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:configxml/applicationContext-Mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 设置编码过滤器 -->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

 applicationContext.xml   spring 核心配置文件

在这个配置文件中,我们主要配置数据源,Spring的事务管理和Dao接口的扫描,以及对Mybatis的一些列相关配置文件的扫描。

<?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: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/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/firstdb" />
<property name="username" value="root" />
<property name="password" value="" />
</bean> <!-- 配置session工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:configxml/mybatis-config.xml" />
<property name="mapperLocations" value="classpath:com/zht/mapping/*.xml"/>
</bean> <!-- DAO接口所在包名,Spring会自动查找之中的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zht.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> <!--事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--开启事务注解扫描-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 applicationContext-Mvc.xml

这个配置文件中我们主要启用Sping注解驱动,进行静态资源的配置,注解扫描配置和视图解析器配置.

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <mvc:annotation-driven/>
<context:component-scan base-package="com.zht"></context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/"></property>
<property name = "suffix" value = ".jsp"></property>
</bean> <mvc:default-servlet-handler/>
</beans>

mybatis-config.xml

Mybatis的配置文件就是mybatis-config.xml,主要是配置typeAlias,将实体类匹配成XXXMapper.xml中可以直接使用的类型,相当于一个别名,在XXXMapper.xml中就无需再写完整的实体类全路径,直接用alias的值来代替。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!-- 别名 -->
<typeAlias type="com.zht.entity.User" alias="User" />
</typeAliases>
</configuration>

开始测试

User.java实体类

在src下新建com.zht.entity包,然后新建实体类User.java,一般跟表名对应

package com.zht.entity;

public class User {
private Integer id; private String name; private Integer age; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name == null ? null : name.trim();
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

IUserDao.java数据访问层接口

在src下新建com.zht.dao包,然后新建IUserDao.java 接口类,数据访问层接口

package com.zht.dao;

import com.zht.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List; @Repository("userDao")
public interface IUserDao {
List<User> selectAllUser();
}

UserMapper.xml  mybatis映射文件,存放sql语句

src下新建com.zht.mapping包,然后新建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" > <!--namespace就是与此文件对应的Dao接口的全路径-->
<mapper namespace="com.zht.dao.IUserDao" >
<!--如下type的User就是mybatis-config.xml中配置的user-->
<resultMap id="BaseResultMap" type="User" >
<id column="ID" property="id" jdbcType="INTEGER" />
<result column="Name" property="name" jdbcType="VARCHAR" />
<result column="Age" property="age" jdbcType="INTEGER" />
</resultMap>
<!--自己配置的查询表所有数据的sql-->
<select id="selectAllUser" resultType="User">
select * from user;
</select>
</mapper>

IUserService.java   业务逻辑层接口

在src下新建com.zht.service包,然后新建IUserService.java 接口类, 业务逻辑层接口

package com.zht.service;

import com.zht.entity.User;
import java.util.List; public interface IUserService {
public List<User> getUser();
}

UserServiceImpl.java 业务逻辑层实现类

在src下新建com.zht.service.impl包,然后新建UserServiceImpl.java实体类

实现了IUserService接口,主要是处理具体的业务,向下调用dao接口访问数据库,经过业务处理后向上返回给视图

这里写了个很简单的业务判断,若取到的user数据数量大于100或者等于0,则返回空。

package com.zht.service.impl;

import com.zht.dao.IUserDao;
import com.zht.entity.User;
import com.zht.service.IUserService; import org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.util.List; @Service("userService")
public class UserServiceImpl implements IUserService { @Resource(name = "userDao")
private IUserDao userDao; @Override
public List<User> getUser() {
List<User> list = userDao.selectAllUser();
if (list.size() > 100 && list.size() <= 0) {
return null;
} else {
return list;
}
}
}

 UserController.Java 控制器

控制器,向上接收URL,向下调用server层处理逻辑并返回数据

package com.zht.controller;

import java.util.List;
import javax.annotation.Resource; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.zht.entity.User;
import com.zht.service.IUserService; @Controller
@RequestMapping(value = "/gouser")
public class UserController {
@Resource(name = "userService")
IUserService userService; @RequestMapping(value = "/list")
public ModelAndView list() {
ModelAndView mv = new ModelAndView();
List<User> userList = userService.getUser();
mv.addObject("userList", userList);
mv.setViewName("result");
return mv;
}
}

index.jsp

程序默认页面,很简单直接跳转到gouser/list

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="/gouser/list"/>
</body>
</html>

result.jsp 返回结果页

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<c:choose>
<c:when test="${not empty userList}">
<c:forEach items="${userList}" var="user" varStatus="vs">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
</c:when>
<c:otherwise>
<tr>
<td colspan="2">无数据!</td>
</tr>
</c:otherwise>
</c:choose>
</table>
</body>
</html>

部署tomcat,启动,访问url:http://localhost:8080/ssm_proj/

如果出现此页面,那就说明成功了!

遇到的坑:

1.Eclipse的“Bulid Automatically”没开,导致修改的代码没有编译,一直看到的是上一次的结果

2.The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

这个报错令人很头疼,不知如何解决,我原先以为只要看Console窗口的最下方看即可,以为在Url访问时会给出错误提示,但是发现并没有,后来慢慢发现在Tomcat启动时,Console窗口中就会打印出一些错误信息,只要有“严重”的字样,基本上项目是跑不起来的,根据console信息定位问题,然后解决即可。

3.JAR包的导入,需要复制到WEB-INF/libs下面,是物理复制。我之前引入到Refernced Libraries中还是会提示,class找不到。。。原因暂时不清楚

4.NoSuchBeanDefinitionException: No bean named 'userService' available

这个原因一般有两个,如果是用xml配置的,确定下bean的name(id)是否正确,配置是否完整,如果是用注解@service这种的,确定下你xml配置里面的扫描包是否扫描到了userService这个java类所在的包。

回到本项目就是mvc配置文件中  <context:component-scan base-package="com.zht"> 这里的包必须包含userService这个java类

而我之前写的是base-package="com.zht.dao",所以报错

5.报错:org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;

主要是jar包版本兼容问题,MyBatis-Spring.jar和MyBatis版本对不上,参考下表:

MyBatis-Spring MyBatis Spring
1.0.0 and 1.0.1 3.0.1 to 3.0.5 3.0.0 or higher
1.0.2 3.0.6 3.0.0 or higher
1.1.0 or higher 3.1.0 or higher 3.0.0 or higher
1.3.0 or higher 3.4.0 or higher 3.0.0 or higher

6.返回空结果,如图:

没有任何报错,就是查询数据库返回的list<User>为空,不知是何原因,最后查到应该是JSP本身的原因

在用到<c:forEach>的时候发现有黄线感叹号,鼠标悬停,提示:Unknown tag (c:forEach) 未知的标签

需要引入标签库,在开头加上这句 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>即可

同时要引入Jar包jstl-1.2.jar

总结:

就是对Eclipse不熟悉,对Console不熟悉,JSP有黄色警告要注意

ssm框架使用了各种注解,AOP,IOC等技术,你只需要配置文件,然后在业务层里写写业务即可,操作数据库的各种连接代码全封装好了。确实很方便,但是真的是配置很繁琐,而且容易出错。。。

源代码包含jar包csdn下载链接:https://download.csdn.net/download/wcc27857285/10667045

SSM三大框架整合教程的更多相关文章

  1. SpringMVC详解(四)------SSM三大框架整合之登录功能实现

    为了后面讲解的需要,我们取数据都会从数据库中获取,所以这里先讲讲三大框架(Spring.SpringMVC.MyBatis)的整合.前面讲解 MyBatis 时,写了一篇 MyBatis 和 Spri ...

  2. ssm三大框架整合基本配置

    ssm三大框架整合基本配置 maven目录结构 数据库脚本mysql create database maven; use maven ; -- --------------------------- ...

  3. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  4. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)(转)

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  5. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  6. SSM三大框架整合详细教程

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  7. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis

    原博主链接:( http://blog.csdn.net/zhshulin ) 使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么 ...

  8. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)(转载)

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  9. SSM三大框架整合

    三大框架整合的思路 1.Dao层: Mybatis的配置文件:SqlMapConfig.xml 不需要配置任何内容,需要有文件头.文件必须存在. applicationContext-dao.xml: ...

随机推荐

  1. 小程序跳转传参参数值为url时参数丢失

    通过先encodeURIComponent,取到值以后再decodeURIComponent,拼接参数正常传递 A页面 switch: function (e) { var aa = 'UNNZVUf ...

  2. springboot中activeMQ消息队列的引入与使用(发送短信)

    1.引入pom依赖 <!--activemq--><dependency> <groupId>org.springframework.boot</groupI ...

  3. 浏览器禁用Cookie后的Session处理

    1. 实现购物车, 可以基于Cookie, 也可以基于Session, 若服务器性能较差, 可以考虑基于Cookie实现购物车 2. 解决方案: URL重写 把用户可能点的每一个超链接后面,都跟上用户 ...

  4. configparser 配置文件模块

    #_author:star#date:2019/11/7# configparser 配置文件模块import configparserconfig=configparser.ConfigParser ...

  5. where方法的用法是ThinkPHP查询语言的精髓

    where方法的用法是ThinkPHP查询语言的精髓,也是ThinkPHP ORM的重要组成部分和亮点所在,可以完成包括普通查询.表达式查询.快捷查询.区间查询.组合查询在内的查询操作.where方法 ...

  6. 如何使用C++获取 进程的 绝对路径

    DWORD GetProcessId(IN PCHAR szExeName) { DWORD dwRet = 0; DWORD dwCount = 0; HANDLE hSnapshot = Crea ...

  7. 使用movable-view制作可拖拽的微信小程序弹出层效果。

    仿了潮汐睡眠小程序的代码.[如果有侵权联系删除 最近做的项目有个弹出层效果,类似音乐播放器那种.按照普通的做了一般感觉交互不是很优雅,设计妹子把潮汐睡眠的弹层给我看了看,感觉做的挺好,于是乘着有空仿照 ...

  8. 深入浅出Mybatis系列(一)---Mybatis入门[转]

    最近两年 springmvc + mybatis 的在这种搭配还是蛮火的,楼主我呢,也从来没真正去接触过mybatis, 趁近日得闲, 就去学习一下mybatis吧. 本次拟根据自己的学习进度,做一次 ...

  9. 《DSP using MATLAB》Problem 7.36

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  10. mybtais分批insert

    这里自己写了个对集合按一批的数量进行分批操作的分页bean,见PagenationUtil如下: package com.util; import java.util.List ; /** * @au ...