blog/java相关/第一次使用springMvc的记录.md

6.7 KiB
Raw Blame History

title date tags
第一次使用springMvc的记录 2021-08-12 17:25:07 java

+++

第一次使用springmvc的记录

1. webxml配置文件

<?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_4_0.xsd"
         version="4.0">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

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

    <servlet>
        <servlet-name>springmvc</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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

1.1. listener的作用

配置listener的作用是在启动时初始化ioc容器

1.2. context-param

param-name为关键字contextConfigLocation当初始化ioc时会从这个给关键字位置寻找spring的配置文件位置param-value就为spring配置文件的位置

1.3. 初始化servlet

其中init-param中指定springMvc配置文件的位置

<load-on-startup>1</load-on-startup>

的意思为指定初始化的时间,如果不指定,当在第一个用户访问时才会进行初始化,会导致第一个用户访问时间变得特别长

1.4. 指定拦截器

seevlet-mapping为指定一个过滤器url-pattern的参数为拦截的urlservlet为拦截后交个哪一个servlet进行处理

2. 前端控制器的配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
   http://www.springframework.org/schema/mvc
   https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:resources mapping="/page/**" location="/page/"/>

    <context:component-scan base-package="com.gxa.controller"/>

    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

2.1. 指定静态文件资源路径

 <mvc:resources mapping="/page/**" location="/page/"/>

mapping 为映射后的路径location为相对于web目录的路径

例如**"/page/"**表示将page文件夹放在web根目录下面

2.2. 设置控制器扫描路径

<context:component-scan base-package="com.gxa.controller"/>

base-package为mvc控制器类的路径

2.3. 配置mvc的试图模板

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>

prefix参数为试图模板的前缀路径

suffix参数为视图模板的后缀

3. mybatis与spring的整合

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

    <context:property-placeholder location="classpath:spring/gxa.properties" />
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}" />
        <property name="url" value="${jdbc.url}" />
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="maxActive" value="10" />
        <property name="minIdle" value="5" />
    </bean>

    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:main/java/com/gxa/mapper/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.gxa.mapper"/>
    </bean>
</beans>

3.1. 加载property配置文件

<context:property-placeholder location="classpath:spring/gxa.properties" />

location为需要加载的文件的位置

3.2. 配置mybatis数据源

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">        <property name="username" value="${jdbc.username}"/>        <property name="password" value="${jdbc.password}" />        <property name="url" value="${jdbc.url}" />        <property name="driverClassName" value="${jdbc.driverClassName}" />        <property name="maxActive" value="10" />        <property name="minIdle" value="5" />    </bean>
  • username 数据库的用户名

  • password 数据库的密码

  • url 数据库连接的网址

  • driverClassName 数据库连接的驱动名

  • maxActive 数据库连接池的最大数量

  • minIdle 数据库连接池的最小空闲数量

3.3. 配置mybatis的注解扫描类

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.gxa.mapper"/>    </bean>

basePackage为mybatis类的权限类名

3.4. 配置mybatis的SqlSessionFactoryBean

<bean class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="mapperLocations" value="classpath:main/java/com/gxa/mapper/*.xml" /></bean>
  • dataSource为mybatis的数据源引用

  • mapperLocations为mybatis的xml配置文件位置mybatis的配置文件与其接口类在同一路径,且接口名等于配置文件的namespece参数就可以不需要配置该属性spring为对其进行自动扫描