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

整合思路:

Dao层:

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

2、applicationContext-dao.xml

a)        数据库连接池

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、配置前端控制器。

详细配置如下:

一、SSM框架整合

      1.1、整合思路

        从底层整合起,也就是先整合mybatis与spring,然后在编写springmvc。

      1.2、开发需求

        查询商品列表(从数据库中查询)

      1.3、创建web工程

          

        现在ssm的工程创建就有区别于原先的dao、service、web这样的三层目录了,现在是mapper、service、controller这样的目录,mapper就相当于以前的dao、controller相当于以前的web,改变了名称而已。不要因此看不懂了。

      1.4、添加jar包

导包

  • spring(包括springmvc)
  • mybatis
  • mybatis-spring整合包
  • 数据库驱动
  • 第三方连接池。

          

      1.5、开始整合mapper(mybatis与spring的整合)

        直接上代码。

        1.5.1、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> <!-- 取别名,或者别的其他全局配置信息,就在这里编写 -->(逆向工程不需要起别名)
<typeAliases>
<!-- 别名为类名首字母大小写都可以 -->
<package name="com.wuhao.ms.domain"/>
</typeAliases>
<!-- 原先这里还有连接数据库的一些配置,与spring整合后,都交由spring来管理 --> <!-- 加载mapper映射文件,使用通用的配置 -->
<mappers>
<package name="com.wuhao.ssm.mapper"/>
</mappers>
</configuration>

        1.5.2、applicationContext-dao.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://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.xsd
http://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>

          这里需要注意一点,在指定mybatis的全局配置文件的路径的时候,也就是在value="classpath:SqlMapConfig.xml"时,如果在创建的config的配置文件目录下还有层级目录,则这里需要加上,比如,config下面分为了mybatis和spring,那么这里就需要写value="classpath:mybatis/SqlMapConfig.xml",看根据你自己的需求来编写

                          

        1.5.3、db.properties配置

            

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

        1.5.4、开发mapper,将逆向工程生成的添加进来

            

          注意:Mapper开发时,先要根据需求进行分析,是否匹配逆向工程生成的代码,如果匹配成功,则不需要再开发mapper;如果不匹配,再去扩展一个新的mapper接口和mapper映射文件来处理该需求,通俗点讲,就是逆向工程生成的mapper接口中的定义的功能是否满足我们开发的需求,因为逆向工程生成的都是对于单表进行操作的,而我们有时候需要的是更复杂的查询,所以如果有需要我们在自己创建mapper接口和mapper映射文件,其实就是扩展功能。

      1.6、整合service

        添加applicationContext-service.xml配置文件,用来处理事务,

        applicationContext-service.xml:如果不懂其中的代码的意思,就查看之前讲解spring管理事务的文章。这里直接复制粘帖即可,修改一些包名称等

          

<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean> <!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice> <!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.itcast.ssm.service.*.*(..))" />
</aop:config> </beans>

      1.7、整合controller

        也就是使用springmvc了。非常简单。

        1.7.1、在web.xml中配置前端控制器DispatcherServlet

          

<?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> <servlet-mapping>
<servlet-name>springmvc-web</servlet-name>
<!-- 配置所有以action结尾的请求进入SpringMVC -->
<url-pattern>*.action</url-pattern>
</servlet-mapping> </web-app>

        1.7.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: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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描@Controler @Service -->
<context:component-scan base-package="com.itheima"/> <!-- 处理器映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
<!-- 处理器适配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
<!-- 注解驱动 -->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/> <!-- 配置Conveter转换器 转换工厂 (日期、去掉前后空格)。。 -->
<bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 配置 多个转换器-->
<property name="converters">
<list>
<bean class="com.itheima.springmvc.conversion.DateConveter"/>
</list>
</property>
</bean> <!-- 视图解释器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>

      1.8、整合spring配置文件      

        就是将所有的spring的配置文件都进行加载启动。也就是在web.xml中配置spring的监听器

              

      1.9、总结所有的配置如下图

              

      1.10、部署测试

        1.10.1、查询商品列表(从数据库中查询)

          1、编写service层

            ItemsService 接口              

package com.itheima.springmvc.service;

import java.util.List;

import com.itheima.springmvc.pojo.Items;

public interface ItemService {

    //查询商品列表
public List<Items> selectItemsList(); public Items selectItemsById(Integer id); //修改
public void updateItemsById(Items items); }

            ItemsServiceImpl 实现类 使用注解开发            

package com.itheima.springmvc.service;

import java.util.Date;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.itheima.springmvc.dao.ItemsMapper;
import com.itheima.springmvc.pojo.Items; @Service
public class ItemServiceImpl implements ItemService { @Autowired
private ItemsMapper itemsMapper; //查询商品列表
public List<Items> selectItemsList(){
return itemsMapper.selectByExampleWithBLOBs(null);(检索字段中存在大字段类型,例如text字段类型)
}
public Items selectItemsById(Integer id){
return itemsMapper.selectByPrimaryKey(id);
}
//修改
public void updateItemsById(Items items){
items.setCreatetime(new Date());
itemsMapper.updateByPrimaryKeyWithBLOBs(items);
}
}                    

          2、编写controller层

           ItemsController

package com.itheima.springmvc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView; import com.itheima.springmvc.pojo.Items;
import com.itheima.springmvc.pojo.QueryVo;
import com.itheima.springmvc.service.ItemService; @Controller
public class ItemController { @Autowired
private ItemService itemService;
//入门程序 第一 包类 + 类包 + 方法名
@RequestMapping(value = "/item/itemlist.action")
public ModelAndView itemList(){
//从Mysql中查询
List<Items> list = itemService.selectItemsList(); ModelAndView mav = new ModelAndView();
//数据
mav.addObject("itemList", list);
mav.setViewName("itemList");
return mav;
}
//去修改页面 入参 id
@RequestMapping(value = "/itemEdit.action")
// public ModelAndView toEdit(@RequestParam(value = "id",required = false,defaultValue = "1") Integer idaaq,
public ModelAndView toEdit(Integer id,
HttpServletRequest request,HttpServletResponse response
,HttpSession session,Model model){ //Servlet时代开发
// String id = request.getParameter("id"); //查询一个商品
// Items items = itemService.selectItemsById(Integer.parseInt(id));
Items items = itemService.selectItemsById(id);
ModelAndView mav = new ModelAndView();
//数据
mav.addObject("item", items);
mav.setViewName("editItem");
return mav; }
//提交修改页面 入参 为 Items对象
@RequestMapping(value = "/updateitem.action")
// public ModelAndView updateitem(Items items){
public ModelAndView updateitem(QueryVo vo){ //修改
itemService.updateItemsById(vo.getItems()); ModelAndView mav = new ModelAndView();
mav.setViewName("success");
return mav; } }

         3、添加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="${itemsList }" 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 }/editItems.do?id=${item.id}">修改</a></td> </tr>
</c:forEach> </table>
</form>
</body> </html>

          4、测试http://localhost:8080/springmvc-mybatis/item/itemlist.action               

SSM整合1(springMVC+mybatis)的更多相关文章

  1. 简单易学的SSM(Spring+SpringMVC+MyBatis)整合

    SSM(Spring+SpringMVC+MyBatis)的整合: 具体执行过程:1.用户在页面向后台发送一个请求 2.请求由DispatcherServlet 前端控制器拦截交给SpringMVC管 ...

  2. SSM,即Spring+SpringMVC+MyBatis三个开源框架的整合框架集。

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...

  3. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一)

    梳理下使用spring+springMVC+mybatis 整合后的一个简单实例:输入用户的 ID,之后显示用户的信息(此次由于篇幅问题,会分几次进行说明,此次是工程的创建,逆向生成文件以及这个简单查 ...

  4. Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)

    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...

  5. SSM(Spring+SpringMVC+MyBatis)高并发优化思路

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容).常作为数据源较简单的web项目的框架 ...

  6. 使用maven整合spring+springmvc+mybatis

    使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...

  7. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(三)(错误整理篇)

    使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二) 以上两篇已经把流 ...

  8. 使用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(二)(代码篇)

    这篇是上一篇的延续: 用ssm(spring+springMVC+mybatis)创建一个简单的查询实例(一) 源代码在github上可以下载,地址:https://github.com/guoxia ...

  9. SSM(spring,springMVC,Mybatis)框架的整合

    这几天想做一个小项目,所以搭建了一个SSM框架. 1.基本概念 1.1.Spring   Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Joh ...

  10. ssm之spring+springmvc+mybatis整合初探

    1.基本目录如下  2.首先是向lib中加入相应的jar包  3.然后在web.xml中加入配置,使spring和springmvc配置文件起作用. <?xml version="1. ...

随机推荐

  1. scrapy框架(二)

    scrapy框架(二) 一.scrapy 选择器 概述: Scrapy提供基于lxml库的解析机制,它们被称为选择器. 因为,它们“选择”由XPath或CSS表达式指定的HTML文档的某部分. Sca ...

  2. Linux下的密码破解

    密码散列: 密码散列的$6 表示是:SHA512 这里我们使用hashcat 工具进行破解 ╰─ hashcat -m 1800 hash.txt /usr/share/wordlists/rocky ...

  3. GIT实用操作指令(更新中)

    提取多次提交的文件 git archive --format=zip HEAD `git diff --name-only 较早的提交ID 较晚的提交ID` > diff.zip

  4. VS激活(Visual Studio)

    以下是Visual Studio各个版本的激活密匙: Visual Studio Enterprise 企业版: BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio ...

  5. everspin自旋转矩MRAM技术

    MRAM的主体结构由三层结构的MTJ构成:自由层(free layer),固定层和氧化层.自由层与固定层的材料分别是CoFeB和MgO.MRAM 是一种非易失性的磁性随机存储器.它拥有静态随机存储器( ...

  6. Centos7.x部署SeaFile私有网盘

    1.安装依赖环境 yum -y install wge gcc-c++ .......... 2.关闭Firewalld防火墙和SElinux systemctl stop firewalld sys ...

  7. 面试连环炮系列(五):你们的项目为什么要用RabbitMQ

    你们的项目为什么要用RabbitMQ? 消息队列的作用是系统解耦.同步改异步.请求消峰,举个下订单的例子: 前端获取用户订单信息,请求后端的订单创建接口.这个接口并不直接请求订单服务,而是首先生成唯一 ...

  8. 面试连环炮系列(二):你们的项目Redis做了集群部署吗

    你们的项目Redis做了集群部署吗? 我们有大量数据需要缓存,而单实例的容量毕竟是有限的,于是做了Redis集群部署. 采取的方案是什么,Codis还是Redis Cluster,为什么要选择这个方案 ...

  9. 最短路径之Dijsktra算法(python)

    定义: 起始位置:A 终止位置:F 持久集合:permanent = set() 暂时集合:temporary = set() 首先将起始位置A加入永久集合,并将A的距离设为0, 此时遍历A的邻接节点 ...

  10. 【AGC028D】Chord

    Problem Description 给定一个圆,圆上均等地放着 \(2n\) 个点,已有 \(k\) 对点之间连好了边,从中选择剩下 \(n-k\) 对点随意连边. 求所有连边方案中,联通块的个数 ...