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 ...
随机推荐
- 设计模式(十三)代理模式(Proxy Pattern)
一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访问到这些对象,如果直接访问对象给系统带来不必要的复杂性,这时候可以在客户端和目标对象之间增加一层中间层,让代 ...
- 配置 EPEL yum 源
当我们在linux上, 使用yum 安装包时,报错如下: Loaded plugins: product-id, refresh-packagekit, security, subscription- ...
- JDBC 详解(转载)
原文链接:http://blog.csdn.net/cai_xingyun/article/details/41482835 什么是JDBC? Java语言访问数据库的一种规范,是一套API JDBC ...
- jQuery选择器笔记
1.$(this).hide() - 隐藏当前元素 $("p").hide() - 隐藏所有段落 $(".test").hide() - 隐藏所有 class= ...
- ArcGIS Engine开发之属性查询
属性查询即基于空间数据的属性数据的查询,通过用户提交SQL语言中的where语句定义的查询条件,对属性数据进行搜索,从而得到查询结果的操作. 相关的类与接口 与属性查询功能相关的类主要有QureyFi ...
- MarkdownPad 2 常用快捷键
Ctrl + I : 斜体 Ctrl + B : 粗体 Ctrl + G : 图片 Ctrl + Q : 引用 Ctrl + 1 : 标题 1 Ctrl + 2 : 标题 2 Ctrl + 3 : 标 ...
- (十一)Maven远程仓库的各种配置
1.远程仓库的配置 在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他 ...
- jsTree简单应用Demo
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- 人工智能AI-机器视觉CV-数据挖掘DM-机器学习ML-神经网络-[资料集合贴]
说明:这个贴用于收集笔者能力范围内收集收藏并认为有用的资料,方便各方参考,免去到处找寻之苦,提升信息的交叉引用价值.仅供参考,不作为必然的推荐倾向.如涉及版权等问题请相关人员联系笔者,谢谢. |博客| ...
- MyBatis源码分析-IDEA新建MyBatis源码工程
MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简 ...