整合ssm三大框架使用注解开发查询用户信息

一、基础知识准备之spring mvc工作原理

二、分析

第一步:发起请求到前端控制器(DispatcherServlet)

第二步:前端控制器请求HandlerMapping查找 Handler

可以根据xml配置、注解进行查找

第三步:处理器映射器HandlerMapping向前端控制器返回Handler

第四步:前端控制器调用处理器适配器去执行Handler

第五步:处理器适配器去执行Handler

第六步:Handler执行完成给适配器返回ModelAndView

第七步:处理器适配器向前端控制器返回ModelAndView

ModelAndView是springmvc框架的一个底层对象,包括 Model和view

第八步:前端控制器请求视图解析器去进行视图解析

根据逻辑视图名解析成真正的视图(jsp)

第九步:视图解析器向前端控制器返回View

第十步:前端控制器进行视图渲染

视图渲染将模型数据(在ModelAndView对象中)填充到request域

第十一步:前端控制器向用户响应结果

组件:

1、前端控制器DispatcherServlet(不需要程序员开发)

作用接收请求,响应结果,相当于转发器,中央处理器。

有了DispatcherServlet减少了其它组件之间的耦合度。

2、处理器映射器HandlerMapping(不需要程序员开发)

作用:根据请求的url查找Handler

3、处理器适配器HandlerAdapter

作用:按照特定规则(HandlerAdapter要求的规则)去执行Handler

4、处理器Handler(需要程序员开发)

注意:编写Handler时按照HandlerAdapter的要求去做,这样适配器才可以去正确执行Handler

5、视图解析器View resolver(不需要程序员开发)

作用:进行视图解析,根据逻辑视图名解析成真正的视图(view)

6、视图View(需要程序员开发jsp)

View是一个接口,实现类支持不同的View类型(jsp、freemarker、pdf...)

三、程序

1、需求

用户列表信息查询

 

2、环境准备

 

数据库环境:mysql-5.5.28-winx64

java环境:

jdk1.8

Intellij IDEA

springmvc版本:spring4.3.3

需要spring4.3.3所有jar(一定包括spring-webmvc-4.3.3.RELEASE.jar)

需要mybatis 的jar包,以及spring 和 mybatis 整合包

需要数据库连接池jar包druid-1.0.18.jar等其他数据格式处理包,如图

access-bridge-64.jar

aopalliance-1.0.jar

commons-logging-1.2.jar

druid-1.0.18.jar

jackson-annotations-2.5.0.jar

jackson-core-2.5.0.jar

jackson-databind-2.5.0.jar

joda-time-2.1.jar

mybatis-3.2.2.jar

mybatis-spring-1.1.1.jar

mysql-connector-java-5.1.20-bin.jar

spring-aop-4.3.3.RELEASE.jar

spring-aspects-4.3.3.RELEASE.jar

spring-beans-4.3.3.RELEASE.jar

spring-context-4.3.3.RELEASE.jar

spring-context-support-4.3.3.RELEASE.jar

spring-core-4.3.3.RELEASE.jar

spring-expression-4.3.3.RELEASE.jar

spring-instrument-4.3.3.RELEASE.jar

spring-instrument-tomcat-4.3.3.RELEASE.jar

spring-jdbc-4.3.3.RELEASE.jar

spring-jms-4.3.3.RELEASE.jar

spring-messaging-4.3.3.RELEASE.jar

spring-orm-4.3.3.RELEASE.jar

spring-oxm-4.3.3.RELEASE.jar

spring-test-4.3.3.RELEASE.jar

spring-tx-4.3.3.RELEASE.jar

spring-web-4.3.3.RELEASE.jar

spring-webmvc-4.3.3.RELEASE.jar

spring-webmvc-portlet-4.3.3.RELEASE.jar

spring-websocket-4.3.3.RELEASE.jar

3、配置前端控制器

在web.xml中配置前端控制器。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--DispatcherServlet主要用作职责调度工作,本身主要用于控制流程,拦截匹配的请求,
    Servlet拦截匹配规则要自己定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是配置spring MVC的第一步。-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.form</url-pattern>
    </servlet-mapping>
</web-app>

4、主要配置文件

配置注解映射器和适配器

以及在spring容器中加载Handler即为controller

以及配置:

数据源

SqlSessionFactory

mapper扫描器

<?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"
       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">
<context:component-scan base-package="com.controller"/><!--如果直接采用SpringMVC,只需要把所有相关配置放到xxx-servlet.xml,xxx由web.xml文件中的servlet的name决定-->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean id="mappingJacksonHttpMessageConverter"
                  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!--<context:component-scan/>标签是告诉Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件。
    而<mvc:annotation-scan/>是告知Spring,我们启用注解驱动。然后Spring会自动为我们注册上面说到的几个Bean到工厂中,来处理我们的请求。-->
    <mvc:default-servlet-handler/>
    <!-- 让Spring框架能够捕获所有URL的请求(先调整web.xml中的DispatcherServlet的配置,使其可以捕获所有的请求:),
    同时又将静态资源的请求转由Web容器处理(此句注释意义),-->
    <bean id="dataSource"
          class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean><!--加druid-1.0.18jar包-->
    <!-- bim基本库Session工厂Bean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations">
            <list>
                <value>classpath:com/mapper/*.xml</value><!--如果数据库映射文件建立时选择file类型,例如名字为StudentMapper.xml,而不是直接建立xml文件,若是则会出现*.xml变红的现象-->
            </list>
        </property>
    </bean>
    <!-- 自动扫描mapping.xml文件  com.mapper.mapping-->
    <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。
     *是个通配符,代表所有的文件,**代表所有目录下 -->

    <!--  通过扫描的模式,扫描目录在com/dao目录下,所有的mapper都继承SqlMapper接口的接口, 这样一个bean就可以了    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
</beans>

 

 

5:业务代码

Controller(Handler)

package com.controller;

import com.dao.StudentMapper;
import com.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by 谭雪娇 on 2017/4/16.
 *
 */
/*
*
* 无需继承特定的类或实现特定的接口,只需使用@Controller 标记一个类是Controller */

/*可以使用@RequestMapping 来映射URL 到控制器类,或者是到Controller 控制器的处理方法上*/
@Controller        //控制器Controller 负责处理由DispatcherServlet 分发的请求
@RequestMapping("/Student")
public class StudentController {
    @Autowired(required=false)
    StudentMapper mapper;
    @RequestMapping(value="/hello",method= RequestMethod.GET)//method 属性主要是用于限制能够访问的方法类型的。
    @ResponseBody
    public Object get(){
        List<Student> lst=mapper.get();
        return lst;
       public Object delete(@PathVariable long id){
        return mapper.deleteById(id);
    }

}

}

 

dao(接口)

package com.dao;

import com.domain.Student;

import java.util.List;

/**
 * Created by 谭雪娇 on 2017/4/16.
 */
public interface StudentMapper {
    List<Student> get();
   
}

 

domain(实体类)

package com.domain;

/**
 * Created by 谭雪娇 on 2017/4/16.
 */
public class Student {
    private String id;
    private String name;

public String getId() {
        return id;
    }

public void setId(String id) {
        this.id = id;
    }

public String getName() {
        return name;
    }

public void setName(String name) {
        this.name = name;
    }
}

 

mapper(映射文件)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.StudentMapper">
    <resultMap id="BaseResultMap" type="com.domain.Student">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="name" property="name" jdbcType="VARCHAR"></result>
    </resultMap>
    <sql id="Base_Column_List">
        id,name
    </sql>
    <select id="get" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from stu
    </select>
</mapper>

 

 

查询结果

 

整合ssm三大框架使用注解开发查询用户信息的更多相关文章

  1. 学习SpringBoot整合SSM三大框架源码之SpringBoot

    Spring Boot源码剖析 一.Spring Boot 项目的启动入口流程分析 Spring Boot项目的启动入口main线程上有一个@SpringBootApplication( @Confi ...

  2. 整合SSH三大框架用注解时报An AnnotationConfiguration instance is required to use

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's ...

  3. MyBatis框架之注解开发

    MyBatis注解开发 @Insert注解注解属性value:写入SQL语句 @Options注解实现添加新数据的主键封装注解属性useGeneratedKeys:使用生成的主键,配置为truekey ...

  4. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  5. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)(转)

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  6. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  7. SSM三大框架整合详细教程

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  8. SpringMVC详解(四)------SSM三大框架整合之登录功能实现

    为了后面讲解的需要,我们取数据都会从数据库中获取,所以这里先讲讲三大框架(Spring.SpringMVC.MyBatis)的整合.前面讲解 MyBatis 时,写了一篇 MyBatis 和 Spri ...

  9. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis

    原博主链接:( http://blog.csdn.net/zhshulin ) 使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么 ...

随机推荐

  1. 神器 工具 推荐 SRDebugger

    unity asset store 关联下载 ,添加这个书签  javascript:var url = window.location.href;var id = url.substr(url.la ...

  2. 20191105 《Spring5高级编程》笔记-第9章

    第9章 事务管理 一些名词: 2PC(2 Phase Commit) XA协议 JTS(Java Transaction Service) JCA(Java EE Connector Architec ...

  3. CentOS7 redhat7 linux系统1分钟安装Zabbix web 监控 服务器

    一.准备工作OS:centos7.4Zabbix version:3.4.6(2018/1/15日上线的新版本)Database:MariaDB关闭防火墙:systemctl stop firewal ...

  4. Spring MVC-学习笔记(1)认识spring mvc

    1.基于XML Schema.Controller接口的spring mvc简单例子 1>创建一个动态Web项目,选择同时创建web.xml文件 2>在WEB-INF/lib中粘贴spri ...

  5. HDU 1069 Monkey and Banana dp 题解

    HDU 1069 Monkey and Banana 纵有疾风起 题目大意 一堆科学家研究猩猩的智商,给他M种长方体,每种N个.然后,将一个香蕉挂在屋顶,让猩猩通过 叠长方体来够到香蕉. 现在给你M种 ...

  6. 一篇包含了react所有基本点的文章

    去年,我写了一本关于学习React.js的小书,原来是大约100页. 今年我要挑战自己,把它归纳为一篇文章. 本文不会涵盖什么是React,或者为什么要学习它. 相反,这是面向已经熟悉JavaScri ...

  7. [环境搭建]-Web Api搭建到IIS服务器后PUT请求返回HTTP Error 405.0 - Method Not Allowed 解决方法 转摘:http://blog.csdn.net/qiujuer/article/details/23827531

    尝试使用微软的Web Api,他的确是一个很有意思的东西. 让我体会到了许多的方便,但是我发现部署到IIS服务器上去了后PUT和Delete请求将返回405. 原因是IIS的默认处理程序默认情况下只允 ...

  8. NGUI的怎么在一个Gameobject(游戏物体)中调用另一个Gameobject(游戏物体)的脚本(C#)

    一,在C#代码中,我们都知道可以给游戏物体添加一个脚本,如下图 二,在当前我们是可以调用到该游戏物体脚本定义的变量,但是我们要在其他脚本调用怎么办?如下代码, KnapSackItem kn = it ...

  9. 04Dropout

    不加Dropout,训练数据的准确率高,基本上可以接近100%,但是,对于测试集来说,效果并不好: 加上Dropout,训练数据的准确率可能变低,但是,对于测试集来说,效果更好了,所以说Dropout ...

  10. openGL常用对象的创建及使用

    一.GPU英文全称Graphic Processing Unit,中文翻译为“图形处理器”.GPU(显卡核心芯片)是显示卡的“大脑”,它决定了该显卡的档次和大部分性能 二.使用背景 随着OpenGL状 ...