什么是spring

什么是spring

springmvc是基于spring的一个框架,实际上就是spring的一个模块,专门做web开发的,是servlet的一个升级
web开发底层是servlet,框架是在servlet基础上加入了一些功能,让你做开发方便
SpringMVC就是一个Spring,Spring是容器,ioc能够管理对象,使用, @Component, @Repository, @Service, @Controller
SpringMVC能够创建对象, 放入到容器中(SpringMVC容器), springmvc容器中放的是控制器对象
我们要做的是使用@Contorller创建控制器对象, 把对象放入到springmvc容器中, 把创建的对象作为控制器使用,这个控制器对象能接收用户的请求, 显示处理结果,就当做是一个servlet使用
使用@Controller注解创建的是一个普通类的对象, 不是Servlet,springmvc赋予了控制器对象一些额外的功能
springmvc中有一个对象是Servlet : DispatherServlet(中央调度器)
DispatherServlet: 负责接收用户的所有请求, 用户把请求给了DispatherServlet, 之后DispatherServlet把请求转发给我们的Controller对象, 最后是Controller对象处理请求
index.jsp-----DispatherServlet(Servlet)----转发,分配给—Controller对象(@Controller注解创建的对象)
创建一个springMVC项目

在这里插入图片描述

项目中的文件

在这里插入图片描述

常用注解

@Controller:用于标识处理器类
@RestController注解相当于@ResponseBody + @Controller合在一起的作用
@RequestMapping:(窄化路径)请求到处理器功能方法的映射规则,可定义到类和方法
常用参数:value、method
可将@RequestMapping标签定义到类名处窄化路径
@RequestHeader:请求头(header)数据到处理器功能处理方法的方法参数上的绑定
@RequestBody:请求的body体的绑定(通过HttpMessageConverter进行类型转换);
@Repository 用于声明一个 Bean。
@Service注解用于类上,标记当前类是一个service类,加上该注解会将当前类自动注入到spring容器中,不需要再在applicationContext.xml文件定义bean了。
applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/aop
       https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       https://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:component-scan base-package="com.lanou"></context:component-scan>

<mvc:annotation-driven></mvc:annotation-driven>

<mvc:view-resolvers>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/static/"></property>
        <property name="suffix" value=".html"></property>
    </bean>
</mvc:view-resolvers>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">
    <property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/db_test?serverTimezone=UTC"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.lanou.mapper"></property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="interceptor" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="query*" propagation="SUPPORTS"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="detele*" propagation="REQUIRED"/>
        <tx:method name="insert*" propagation="REQUIRED"/>
        <tx:method name="*" propagation="SUPPORTS"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:advisor advice-ref="interceptor" pointcut="execution(* com.lanou.service.*.*(..))"></aop:advisor>
</aop:config>

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**/"/>
        <!--            <mvc:exclude-mapping path="/user/**"/>-->
        <bean class="com.lanou.util.LoginInterceptor"></bean>
    </mvc:interceptor>
</mvc:interceptors>
</beans>

resources中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.lanou.mapper.UserMapper">


	//对象映射
	<resultMap id="queryAll" type="com.lanou.bean.User">
	    <id column="id" property="id"></id>
	    <result column="account" property="username"></result>
	    <result column="pwd" property="pwd"></result>
	</resultMap>
	
	<select id="queryAll" resultMap="queryAll">
	    select *
	    from tb_user
	</select>
//对象映射
<resultMap id="queryAll" type="com.lanou.bean.User">
    <id column="id" property="id"></id>
    <result column="account" property="username"></result>
    <result column="pwd" property="pwd"></result>
</resultMap>

<select id="queryAll" resultMap="queryAll">
    select *
    from tb_user
</select>
</mapper>

springmvc工作原理图

在这里插入图片描述

springmvc工作流程
1、 用户向服务端发送一次请求,这个请求会先到前端控制器DispatcherServlet(也叫中央控制器)。
2、DispatcherServlet接收到请求后会调用HandlerMapping处理器映射器。由此得知,该请求该由哪个Controller来处理(并未调用Controller,只是得知)
3、DispatcherServlet调用HandlerAdapter处理器适配器,告诉处理器适配器应该要去执行哪个Controller
4、HandlerAdapter处理器适配器去执行Controller并得到ModelAndView(数据和视图),并层层返回给DispatcherServlet
5、DispatcherServlet将ModelAndView交给ViewReslover视图解析器解析,然后返回真正的视图。
6、DispatcherServlet将模型数据填充到视图中
7、DispatcherServlet将结果响应给用户

组件说明

DispatcherServlet:前端控制器,也称为中央控制器,它是整个请求响应的控制中心,组件的调用由它统一调度。
HandlerMapping:处理器映射器,它根据用户访问的 URL 映射到对应的后端处理器 Handler。也就是说它知道处理用户请求的后端处理器,但是它并不执行后端处理器,而是将处理器告诉给中央处理器。
HandlerAdapter:处理器适配器,它调用后端处理器中的方法,返回逻辑视图 ModelAndView 对象。
ViewResolver:视图解析器,将 ModelAndView 逻辑视图解析为具体的视图(如 JSP)。
的后端处理器,但是它并不执行后端处理器,而是将处理器告诉给中央处理器。
HandlerAdapter:处理器适配器,它调用后端处理器中的方法,返回逻辑视图 ModelAndView 对象。
ViewResolver:视图解析器,将 ModelAndView 逻辑视图解析为具体的视图(如 JSP)。
JSP)。
的后端处理器,但是它并不执行后端处理器,而是将处理器告诉给中央处理器。
HandlerAdapter:处理器适配器,它调用后端处理器中的方法,返回逻辑视图 ModelAndView 对象。
ViewResolver:视图解析器,将 ModelAndView 逻辑视图解析为具体的视图(如 JSP)。
Handler:后端处理器,对用户具体请求进行处理,也就是我们编写的 Controller 类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值