SSM整合(三):Spring4与Mybatis3与SpringMVC整合
源码下载 SSMDemo
上一节整合了Mybatis3与Spring4,接下来整合SpringMVC!
说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目!
本节全部采用注解式,如要了解其他的配置方式请查看相关详细的文章!
一、新建SpringMVC配置文件applicationContext-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 使用注解配置 -->
<mvc:annotation-driven />
<!--自动扫描包 -->
<context:component-scan base-package="com.ssm.demo2.controller" />
<!-- ViewResolver 视图解释器 -->
<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>
二、在WebApp/WEB-INF新建jsp目录存放jsp页面!
更改WebApp下index.jsp文件(如没有,新建)内容如下

三、在web.xml文件中添加spring的相关配置文件:
因springmvc是spring的一相模块,也通称为Spring配置文件!
内容如下:
<?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" >
<!-- Spring应用上下文,
理解层次化的ApplicationContext ,
applicationContext-*.xml这里用了通配符,
这里只要与spring整合的文件符合这样的格式将自动全部加载
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!--
DispatcherServlet, Spring MVC的核心 -->
<servlet>
<servlet-name>SSMDemo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SSMDemo</servlet-name>
<!-- mvc-dispatcher拦截所有的请求
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--forwarding 跳转的servlet-->
<servlet>
<servlet-name>forwarding</servlet-name>
<servlet-class>com.spring.mvcdemo.servlet.ForwardingServlet</servlet-class>
</servlet>
<!--配置文件编码 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Spring 全局上下文的
监听,当配置必须有 applicationContext.xml文件,否则报错 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
四、配置tomcat
在eclipse 中windows->preferences
->Server中runtime
environment 添加:

选择自己本地的安装版本号

选择tomcat安装目录:

点Finish 完成!
4.2)在server控制面进行添加服务器

前面配置好了,直接就会显示出,刚配好的tomcat,点完成!
双击服务器进行配置

把配置改成以上图所示:
4.2)部署项目
在项目名右键 run on server

五、开启tomcat部署,报错:

这是因为在上一节中 配置sqlSessionFactory时配置文件少加了classpath: !
在applicationContext-dao.xml中把定义sqlSessionFactory中的配置加上classpath:

再部署再报如下错:

提示缺少jar包,在pom.xml中添加:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
</dependency>
然后maven ->update project 一下,再重新部署,ok!

这时显示index.jsp页面的内容只说明web环境搭建好,配置文件是ok滴,并不代表与springmvc整合成功,接下来,写Controller与Service,从数据库中查询数据并显示,来测试整合成功!
六、写Controller
在com.ssm.demo2.controller新建UserController.java内容如下:
@Controller
@RequestMapping("/user")
publicclass UserController {
@Autowired
UserMapper userMapper;
@RequestMapping("/list")
public ModelAndView userlist() throws Exception{
List<User>
users=userMapper.findUserList("王");
ModelAndView
mView=new ModelAndView();
mView.addObject("users",users);
mView.setViewName("list");//返回的jsp页面名称
returnmView;
}
}
七、在jsp目录下新建list.jsp页面,内容:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h2>SSM 部署成功! </h2>
<p>用户名:</p>
<h3>${users}</h3>
</body>
</html>
八、重新部署:
在输入地址:http://localhost:8080/ssm_demo2/user/list
结果如下:

查询与显示的结果是一样ok!
九、写Service
上面在Controller里直接操作 mapper,这是不规范的,现在在中间加上Service层
在com.ssm.demo2.service中新建UserService.java接口层
publicinterface UserService {
public List<User>findUser(String name) throws Exception;
}
9.1)新建实现类的包名com.ssm.demo2.service.impl,
并写实现类UserServiceImpl.java
@Service
publicclass UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper; //spring框架配置了注解,自动注入
@Override
public List<User>
findUser(String name) throws Exception {
// TODO Auto-generated method
stub
List<User>
users=userMapper.findUserList(name);
returnusers;
}
}
再把UserController.java改成如下:
@Controller
@RequestMapping("/user")
publicclass UserController {
/*@Autowired
UserMapper
userMapper;*/
@Autowired
UserServiceImpl userServie;
@RequestMapping("/list")
public ModelAndView userlist() throws Exception{
// List<User>
users=userMapper.findUserList("王");
List<User> users=userServie.findUser("刘");
ModelAndView mView=new ModelAndView();
mView.addObject("users",users);
mView.setViewName("list");//返回的jsp页面名称
returnmView;
}
}
再重新布署:报如下错:

提示在UserController.java出错,往后再看,显示如下:
说无法注入字段,这说明spring注解配置的无效,根据配置的分类applicationContext-dao.xml是配置管理数据库及spring框架配置的,
applicationContext-mvc.xml是配置SpringMVC模块相关的:
打开applicationContext-dao.xml查看注解配置,发现包名写错:

这是Spring注解,这里包应该是根包名,而不是配置mvc注解的包名,
改成 com.ssm.demo2,再重新部署:显示

显示结果为空:可以自行把日志打印的SQl语句 运行,验证结果,的确为空!
部署ok!
但时细心的话,会发现在启动Tomcat有报错一条错误:
因太快,没有截图上来,错误
如下:

提示在 spring配置文件的 xsi:schemaLocation= 中必须是偶数uri
去掉最后spring-dwr-3.0.xsd这条uri,即可;
注:在配置这些uri时,后面最好不要加上版本号信息,让系统自动适配版本号!减少因版本号不配而报错!
此时,Mybatis3与Spring4与SpringMVC整合成功!
源码下载 SSMDemo
SSM整合(三):Spring4与Mybatis3与SpringMVC整合的更多相关文章
- springMVC整合Junit4进行单元测试
springMVC整合Junit4进行单元测试 标签: springMVC整合Junit4junit单元测试教程springMVC入门教程 spring(10) 版权声明:本文为博主原创文章,未 ...
- SSM框架整合环境构建——基于Spring4和Mybatis3
目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...
- SSM框架整合的详细过程(含每一步的分析及代码)。实质上是SpringMVC与mybatis的整合,应为spring与SpringMVC不需要整合。
为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合. 整合目标:控制层采用springmvc.持久层使用mybatis实现. 1.1 需 ...
- Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x
Mybatis整合通用Dao,Mybatis整合通用Mapper,MyBatis3.x整合通用 Mapper3.5.x ============================== 蕃薯耀 2018年 ...
- JAVAEE——SpringMVC第一天:介绍、入门程序、架构讲解、SpringMVC整合MyBatis、参数绑定、SpringMVC和Struts2的区别
1. 学习计划 第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) Sp ...
- spring mybatis springmvc整合
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- springmvc整合mybatis 配置文件
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- ActiveMQ与SpringMVC整合实现发送PTP和订阅发布消息功能
实现一个基于SpringMVC+JMS+ActiveMQ+Tomcat+JDK1.8+IDEA工具 ,Spring4.1.0和ActiveMQ5.15整合的实例,实现PTP和订阅/发布两种消息模型 一 ...
- 【springmvc thymeleaf】springmvc整合thymeleaf
概述 Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的全功能替代品. 这些集成将使您能够: @Controller像使用JSP一样,将Spring ...
随机推荐
- 在DevExpress程序中使用TeeList控件以及节点查询的处理
在很多情况下,我们需要通过树列表进行数据的展示,如一些有层次关系的数据,通过有层级的展示,能够使用户更加直观查看和管理相关的数据.在一般Winform开发的情况下,可以使用微软的TreeView控件, ...
- C# WinForm国际化的简单实现
软件行业发展到今天,国际化问题一直都占据非常重要的位置,而且应该越来越被重视.对于开发人员而言,在编写程序之前,国际化问题是首先要考虑的一个问题,也许有时候这个问题已经在设计者的考虑范围之内,但终归要 ...
- C#开发微信门户及应用(37)--微信公众号标签管理功能
微信公众号,仿照企业号的思路,增加了标签管理的功能,对关注的粉丝可以设置标签管理,实现更加方便的分组管理功能.开发者可以使用用户标签管理的相关接口,实现对公众号的标签进行创建.查询.修改.删除等操作, ...
- android
配置Activity 的启动模式: 在 AndroidManifest.xml 中配置: <activity android:name=".MainActivity" and ...
- 【Java每日一题】20161226
package Dec2016; public class Ques1226 { static{ num = 1; } public static int num = 2; public static ...
- filter 过滤器(监听)
Filter 过滤器 1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, ...
- 【视频处理】YUV与RGB格式转换
YUV格式具有亮度信息和色彩信息分离的特点,但大多数图像处理操作都是基于RGB格式. 因此当要对图像进行后期处理显示时,需要把YUV格式转换成RGB格式. RGB与YUV的变换公式如下: YUV(25 ...
- js—模糊查询
首先要明白什么是模糊查询(废话又来了),就是根据关键字把列表中符合关键字的一项或某项罗列出来,也就是要检查列表的每一项中是否含有关键字,因此抽象一下就是一个字符串中是否含有某个字符或者字符串. 以下例 ...
- Appfuse:权限控制
Appfuse的权限控制依赖于Struts的Menu机制,common下的menu.jsp是对菜单顺序的定义,详细的菜单项和菜单链接及权限再menu-config.xml中控制,如下: <Men ...
- 看看C# 6.0中那些语法糖都干了些什么(终结篇)
终于写到终结篇了,整个人像在梦游一样,说完这一篇我得继续写我的js系列啦. 一:带索引的对象初始化器 还是按照江湖老规矩,先扒开看看到底是个什么玩意. 1 static void Main(strin ...