Spring+Mybatis+Spring MVC

匆匆过客 提交于 2020-03-12 11:15:50

1、ssm

1.1、认识

ssm是spring mvc,spring,mybatis的集合,是标准的mvc模式,将整个系统划分成为表现层,controller层,service层,dao层共四层。

spring mvc负责请求的转发与视图管理

spring负责业务对象管理

mybatis作为数据对象的持久化引擎

1.2、优点

1.2.1、Spring的优势:

            通过Spring的IOC特性,将对象之间的依赖关系交给了Spring控制,方便解耦,简化了开发

            通过Spring的AOP特性,对重复模块进行集中,实现事务,日志,权限的控制

            提供了对其他优秀开源框架的集成支持

1.2.2、Spring MVC的优势:

            SpringMVC是使用了MVC设计思想的轻量级web框架,对web层进行解耦,使我们开发更简洁

            与Spring无缝衔接

            灵活的数据验证,格式化,数据绑定机制

1.2.3、Mybatis的优势:

           数据库的操作(sql)采用xml文件配置,解除了sql和代码的耦合

           提供映射标签,支持对象和和数据库orm字段关系的映射,支持对象关系映射标签,支持对象关系的组建

           提供了xml标签,支持动态的sql

2、整合案例

2.1、之前

在进行ssm整合时,需要建立maven web项目

建立之后,首先将spring与mybatis整合在一起,在将spring mvc整合进去。添加一些文件。

spring与mybatis的整合办法请看spring-mybatis

这里主要是将spring mvc整合进去。

2.2、新增依赖包

这些包是在原有的基础上新增加的包。

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>
<dependency>
    <artifactId>org.springframework</artifactId>
    <groupId>spring-aspects</groupId>
    <version>3.2.7.RELEASE</version>
</dependency>
<dependency>
    <artifactId>org.springframework</artifactId>
    <groupId>spring-context</groupId>
    <version>3.2.7.RELEASE</version>
</dependency>
<dependency>
    <artifactId>org.springframework</artifactId>
    <groupId>spring-context-support</groupId>
    <version>3.2.7.RELEASE</version>
</dependency>
<dependency>
    <artifactId>org.springframework</artifactId>
    <groupId>spring-web</groupId>
    <version>3.2.7.RELEASE</version>
</dependency>
<dependency>
    <artifactId>org.springframework</artifactId>
    <groupId>spring-webmvc</groupId>
    <version>3.2.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>

2.3、controller层

采用未使用注解方式

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import bean.LinBook;
import service.ILinBookService;

public class LoginController implements Controller {

    ILinBookService linBookService;
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-mybatis.xml");
        linBookService = (ILinBookService) applicationContext.getBean("ILinBookService");
        int id = Integer.parseInt(request.getParameter("id"));
        LinBook linBook = linBookService.getLinBook(id);
        ModelAndView mve = new ModelAndView();
        mve.addObject("linbook", linBook.getBookId());
        mve.setViewName("index");
        return mve;
    }
}

2.4、前端页面

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <h1>输入用户:${linbook }</h1>
    </body>
</html>

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <form action="login.do" method="post">
            <table>
                <tr>
                    <td>id:</td>
                    <td><input type="text" name="id" /></td>
                </tr>
                <tr>
                    <td>班级</td>
                    <td><input type="text" name="clazz" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="提交" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>

2.5、spring-mvc.xml

需要建立spring mvc配置文件

<?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"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                                                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                                http://www.springframework.org/schema/context
                                                http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <!-- 将url映射到具体的业务控制器 -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="login.do">login</prop>
            </props>
        </property>
    </bean>
    <bean id="login" class="controller.LoginController"></bean>

    <!-- 配置视图解析器 -->
    <!-- InternalResourceViewResolver:用于支持servlet,jsp的解析 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 当jsp模板需要使用jstl标签库时,需要配置,没有则不需要配置 -->
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <!-- 查找视图页面的前缀和后缀 -->
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

2.6、web.xml

web工程配置

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>

    <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>

    <!-- 防止Spring内存溢出监听器 -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!-- 前端控制器 -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>

2.7、测试

到此,就将spring mvc,spring,mybatis整合好了

测试时在浏览器输入网址localhost:8080/ssm1/login.jsp即可

3、其他

3.1、说明

整合也可以采用注解的方式

3.2、controller

对于controller使用注解@Controller

其中的方法采用注解@RequestMapping(value="***",method=RequestMethod.GET/POST)

@Controller
public class LinBookController {

    private LinBookService linBookService;

    @RequestMapping(value = "/toindex", method = RequestMethod.POST)
    public ModelAndView toindex(HttpServletRequest request) {
    }
}

3.3、修改spring-mvc.xml

加入

<!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
<mvc:annotation-driven />
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="controller" />
<mvc:default-servlet-handler />
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="mappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>

删除

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="login.do">login</prop>
        </props>
    </property>
</bean>
<bean id="login" class="controller.LinBookController">
</bean>

3.4、修改web.xml文件

加入

<!-- Spring监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->

修改

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

不能配成

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

3.5、问题

目前还无法完成对controller中的service实现注解注入,有待解决。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!