文章出处:课程资料
web.xml等配置文件的解释:打开博客

为了更好的学习 springmvc和mybatis整合开发的方法,需要将springmvc和mybatis进行整合。

整合目标:控制层采用springmvc、持久层使用mybatis实现。

步骤详解:

创建数据库表

需要的jar包

  1. spring(包括springmvc)
  2. mybatis
  3. mybatis-spring整合包
  4. 数据库驱动
  5. 第三方连接池。

整合思路

Dao层:

1、SqlMapConfig.xml,空文件即可,但是需要文件头。
2、applicationContext-dao.xml

数据库连接池

b) SqlSessionFactory对象,需要spring和mybatis整合包下的。
c) 配置mapper文件扫描器。

Service层:

1、applicationContext-service.xml包扫描器,扫描@service注解的类。
2、applicationContext-trans.xml配置事务。

Controller层:

1、Springmvc.xml
a) 包扫描器,扫描@Controller注解的类。
b) 配置注解驱动
c) 配置视图解析器

Web.xml文件:

1、配置spring
2、配置前端控制器。

创建工程

创建动态web工程springmvc-web(选2.5可以自动生成web.xml)

加入jar包

复制jar包到/WEB-INF/lib中
工程自动加载jar包

加入配置文件

创建资源文件夹config
在其下创建mybatis和spring文件夹,用来存放配置文件,如下图:

sqlMapConfig.xml

使用逆向工程来生成Mapper相关代码,不需要配置别名。
在config/mybatis下创建SqlMapConfig.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></configuration>

applicationContext-dao.xml

配置数据源、配置SqlSessionFactory、mapper扫描器。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 加载配置文件 --><context:property-placeholder location="classpath:db.properties" /><!-- 数据库连接池 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxActive" value="10" /><property name="maxIdle" value="5" /></bean><!-- 配置SqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 数据库连接池 --><property name="dataSource" ref="dataSource" /><!-- 加载mybatis的全局配置文件 --><property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /></bean><!-- 配置Mapper扫描 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 配置Mapper扫描包 --><property name="basePackage" value="cn.itcast.ssm.mapper" /></bean></beans>

db.properties

配置数据库相关信息

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 配置service扫描 --><context:component-scan base-package="cn.itcast.ssm.service" /></beans>

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:p="http://www.springframework.org/schema/p"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-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置controller扫描包 --><context:component-scan base-package="cn.itcast.ssm.controller" /><!-- 注解驱动 --><mvc:annotation-driven /><!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" --><!-- 配置视图解析器 --><bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置逻辑视图的前缀 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 配置逻辑视图的后缀 --><property name="suffix" value=".jsp" /></bean></beans>

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_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>springmvc-web</display-name><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><!-- 配置spring --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext*.xml</param-value></context-param><!-- 使用监听器加载Spring配置文件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置SrpingMVC的前端控制器 --><servlet><servlet-name>springmvc-web</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param></servlet><!-- 1. /*  拦截所有   jsp  js png .css  真的全拦截   建议不使用2. *.action *.do 拦截以do action 结尾的请求     肯定能使用   ERP  3. /  拦截所有 (不包括jsp) (包含.js .png.css)  强烈建议使用     前台 面向消费者  www.jd.com/search   /对静态资源放行--><servlet-mapping><servlet-name>springmvc-web</servlet-name><!-- 配置所有以action结尾的请求进入SpringMVC --><url-pattern>*.action</url-pattern></servlet-mapping></web-app>

加入jsp页面

itemList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!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=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr><td>商品名称</td><td>商品价格</td><td>生产日期</td><td>商品描述</td><td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr><td>${item.name }</td><td>${item.price }</td><td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td><td>${item.detail }</td><td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td></tr>
</c:forEach></table>
</form>
</body></html>

itemEdit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!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=UTF-8">
<title>修改商品信息</title></head>
<body> <!-- 上传图片是需要指定属性 enctype="multipart/form-data" --><!-- <form id="itemForm" action="" method="post" enctype="multipart/form-data"> --><form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post"><input type="hidden" name="items.id" value="${item.id }" /> 修改商品信息:<table width="100%" border=1><tr><td>商品名称</td><td><input type="text" name="items.name" value="${item.name }" /></td></tr><tr><td>商品价格</td><td><input type="text" name="items.price" value="${item.price }" /></td></tr><tr><td>商品生产日期</td><td><input type="text" name="items.createtime"value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td></tr><%-- <tr><td>商品图片</td><td><c:if test="${item.pic !=null}"><img src="/pic/${item.pic}" width=100 height=100/><br/></c:if><input type="file"  name="pictureFile"/> </td></tr>--%><tr><td>商品简介</td><td><textarea rows="3" cols="30" name="items.detail">${item.detail }</textarea></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交" /></td></tr></table></form>
</body></html>

springmvc整合mybatis之准备阶段与文件配置相关推荐

  1. SpringMVC整合mybatis(终结版)

    ssm框架是目前web开发中最流行的框架之一,这个框架快捷方便,搭建完成后是很容易进行项目的开发的. 本文将使用idea作为开发工具,并使用maven进行项目管理,进行ssm框架的搭建. 1. 整合思 ...

  2. 【SpringMVC整合MyBatis】整合思路与工程结构

    springmvc和mybatis整合 1.需求 使用springmvc和mybatis完成商品列表查询. 2.整合思路 springmvc+mybaits的系统架构: 如图 第一步:整合dao层 m ...

  3. spring 整合 mybatis 中数据源的几种配置方式

    因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...

  4. mysql动态配置数据源_Spring整合Mybatis实现动态数据源切换教程配置

    一.摘要 这篇文章将介绍Spring整合Mybatis 如何完成SqlSessionFactory的动态切换的.并且会简单的介绍下MyBatis整合Spring中的官方的相关代码. Spring整合M ...

  5. SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置

    转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...

  6. 【速学java】 java后台框架 springmvc整合mybatis框架源码

    三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] A 调用摄像头拍照,自定义裁剪编辑 ...

  7. springmvc整合mybatis框架源码 bootstrap html5 mysql oracle maven

    获取[下载地址]   QQ: 313596790 A 调用摄像头拍照,自定义裁剪编辑头像,头像图片色度调节 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,快速开发利器)+快速表单构建器 ...

  8. springmvc整合mybatis框架源码 bootstrap html5 mysql oracle

    获取[下载地址]   QQ 313596790 三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体 [新录针对本系统的视频教程,手把手教开发一个模块,快速 ...

  9. springmvc整合mybatis框架源码 bootstrap html5 mysql oracle maven SSM

    获取[下载地址]   QQ: 313596790 A 调用摄像头拍照,自定义裁剪编辑头像 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] B 集成代码生成器 [正反双向](单表.主 ...

最新文章

  1. 2022-2028年中国丝印硅胶行业市场深度分析及投资前景趋势报告
  2. HDU 5115 Dire Wolf ——(区间DP)
  3. C#操作Excel文件暨C#实现在Excel中将连续多列相同数据项合并
  4. iOS动画-从UIView到Core Animation
  5. Centos 安装Java JDK8
  6. 缠论中枢python源码_通达信缠论中枢主图公式源码
  7. 半导体物理与器件_2017级光信息专业本科生选修课程:半导体物理与器件
  8. centos6 安装 directAdmin
  9. 2022美国小非农ADP数据发布时间一览表
  10. ccf-csp 201809-2 买菜
  11. 【CF37E】 Trial for Chief
  12. C++知识整理系列(一)指针和动态空间
  13. Java两个网页之间的文件互传_如何让两台 PC 进行文件传输?
  14. 80老翁谈人生(151):老翁老眼昏花,读错了一个数量级
  15. QtMath:通用数学函数
  16. Android动画之Tween Animation
  17. 日历 android 周历,Android Studio 基础 之 获取系统Calendar 日历日程 (涉及指定日期时间判断是星期几的方法使用)的方法整理...
  18. 创建数据库表格的方法
  19. UA OPTI544 量子光学5 光与介质相互作用:Electric Dipole Selection Rule
  20. 凸优化笔记(1) —— 基本概念

热门文章

  1. 第二届构建之法论坛预告(草案)
  2. sql android客户端,XSGManage: 学生成绩管理系统---客户端,基于Android+Django+sqlit3开发...
  3. JAVA进阶教学之(Object类中的hashCode方法)
  4. ubuntu wifi固定ip_自制wifi遥控小车!ESP8266实践指南(二)
  5. ad16自动布线设置规则_设计 | 18种pcb设计特殊布线的画法与技巧!
  6. mybatis 自动填充无效_开发小知识-mybatis-plus自动填充与读写分离
  7. notebook python 内嵌 数据库_python数据分析:在jupyter notebook上使用pythonSQL做数据分析...
  8. python自动发送邮件不需要发件邮箱_python使用QQ邮箱实现自动发送邮件
  9. python的print输出_python中的print()输出
  10. java scipt 对象 函数_java script 基本函数