0


ssm整合各配置文件详解

各个配置文件(总结版)

叫法

  • springmvc框架中,handler和controller只是不同的叫法,本质是一样的,只是名字不一样。在实际编写中,都要加@controller注解,才能被spring知道是一个控制器,才能交给spring的ioc容器管理。
  • spring配置文件有的叫做applicationContext.xml。applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。但是我们创建通常不会这样叫。
  • mapper接口和DAO接口本质是一样的。叫法不同罢了。

spring-persist.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!--1. 配置自动扫描的包(service层)--><context:component-scanbase-package="com.atguigu.ssm.service"/><!--2.加载外部属性配置文件(jdbc.properties)--><context:property-placeholderlocation="classpath:jdbc.properties"/><!-- 2.1配置数据源--><beanid="druidDataSource"class="com.alibaba.druid.pool.DruidDataSource"><propertyname="username"value="${jdbc.user}"/><propertyname="password"value="${jdbc.password}"/><propertyname="url"value="${jdbc.url}"/><propertyname="driverClassName"value="${jdbc.driver}"/></bean><!--========================================mybatis整合==============================================================--><!--3.配置SqlsessionFactoryBean,整合mybatis--><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><!--3.1指定mybatis全局配置文件--><!-- 方法1:保留全局配置文件-----指定Mybatis全局配置文件--><propertyname="configLocation"value="classpath:mybatis-config.xml"/><!--方法2:舍弃mybatis全局配置文件,这样写--><!-- <property name="configuration">--><!-- <bean class="org.apache.ibatis.session.Configuration">--><!--  <property name="mapUnderscoreToCamelCase" value="true"/>--><!--  </bean>--><!-- </property>--><!--方法2:舍弃mybatis全局配置文件,这样写--><!-- <property name="typeAliasesPackage" value="com.atguigu.ssm.entity"/>--><!-- 3.2 指定Mapper配置文件位置--><propertyname="mapperLocations"value="classpath:mappers/*Mapper.xml"/><!--3.3装配数据源(我们在上面配置的数据源)--><propertyname="dataSource"ref="druidDataSource"/></bean><!--方法1--><!--4. 使用mybatis-spring命名空间--><mybatis-spring:scanbase-package="com.atguigu.ssm.mapper"/><!--方法2--><!--4.配置mapper接口(代理对象加入到ioc容器中)类型的bean的扫描器--><!--  <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">--><!--  <property name="basePackage" value="com.atguigu.ssm.mapper"/>--><!--  </bean>--><!--==================================mybatis整合==============================================================--><!--5.配置事务管理器--><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--5.1 装配数据源(要能管事务就要控制住数据源,数据源中连接,回滚等操作)--><propertyname="dataSource"ref="druidDataSource"/></bean><!--6.开启基于注解的声明式事务--><tx:annotation-driventransaction-manager="transactionManager"/></beans>

spring-persist.xml主要作用(主要配置和业务逻辑有关的,数据源,mybatis整合,事务控制)

  1. 配置自动扫描的包(service层)
  2. 加载外部属性配置文件(jdbc.properties) 2.1 配置数据源
  3. 配置SqlsessionFactoryBean(整合mybatis) 3.1指定mybatis全局配置文件 3.2 指定Mapper配置文件位置 3.3装配数据源(因为以往都是在mybatis中要配置的)
  4. 配置扫描mapper接口
  5. 配置事务管理器 5.1 装配数据源
  6. 开启基于注解的声明式事务

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!--1.Mybatis全局配置,驼峰式命名--><!-- Mybatis全局配置 --><settings><!-- 将数据库表字段映射到驼峰式命名的Java实体类属性中 --><!-- 数据库表字段格式:单词_单词 --><!-- Java实体类属性:首字母小写的驼峰式命名 --><settingname="mapUnderscoreToCamelCase"value="true"/></settings><!--2.设置实体类别名(以包为单位),方便mapper.xml中resultType直接写类名--><!--指定实体类别名,设置之后mapper.xml文件中resultType就不用写包名,直接写类名即可--><typeAliases><packagename="com.atguigu.ssm.entity"/></typeAliases></configuration>

mybatis-config.xml文件主要作用

  1. Mybatis全局配置,驼峰式命名
  2. 设置实体类别名(以包为单位),方便mapper.xml中resultType直接写类名

web.xml

web.xml文件为所有web应用程序启动加载的首个文件,所有框架的加载都需要在此配置。

<?``xml version="1.0" encoding="UTF-8"?><web-appxmlns="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"><!--1.配置ContextLoaderListener,读取spring-persist.xml文件--><!--1.1 通过context-param 指定Spring框架的配置文件位置--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-persist.xml</param-value></context-param><!--1.2 配置ContextLoaderListener 监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--2.配置springmvc前端控制器dispatcherServlet,对浏览器发送的请求进行统一处理--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 2.1 通过初始化参数init-param指定SpringMVC配置文件的位置和名称 --><init-param><!-- contextConfigLocation为固定值 --><param-name>contextConfigLocation</param-name><!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的src/main/resources --><param-value>classpath:spring-mvc.xml</param-value></init-param><!--2.2 将启动控制DispatcherServlet的初始化时间提前到服务器启动时--><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><!--/不能匹配.jsp请求路径的请求--><url-pattern>/</url-pattern></servlet-mapping><!--3.配置过滤器CharacterEncodingFilter,设置字符编码--><filter><filter-name>characterEncodingFilter</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>forceRequestEncoding</param-name><param-value>true</param-value></init-param><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>characterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--4.配置Rest风格的HiddenHttpMethodFilter,将post请求转换为put和Delete请求--><filter><filter-name>hiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>hiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

web.xml配置文件主要作用

  1. 配置ContextLoaderListener,读取spring-persist.xml文件 1.1 通过context-param 指定Spring框架的配置文件位置 1.2 配置ContextLoaderListener 监听器
  2. 配置springmvc前端控制器dispatcherServlet,对浏览器发送的请求进行统一处理 2.1 通过初始化参数init-param指定SpringMVC配置文件的位置和名称 2.2 将启动控制DispatcherServlet的初始化时间提前到服务器启动时
  3. 配置过滤器CharacterEncodingFilter,设置字符编码
  4. 配置Rest风格的HiddenHttpMethodFilter,将post请求转换为put和Delete请求

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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 "><!--1.扫描组件,开启注解扫描,handler或者controller被扫描--><context:component-scanbase-package="com.atguigu.ssm.handler"/><!--2.配置 Thymeleaf 的视图解析器,视图跳转 --><beanid="thymeleafViewResolver"class="org.thymeleaf.spring5.view.ThymeleafViewResolver"><propertyname="order"value="1"/><propertyname="characterEncoding"value="UTF-8"/><propertyname="templateEngine"><beanclass="org.thymeleaf.spring5.SpringTemplateEngine"><propertyname="templateResolver"><beanclass="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"><propertyname="prefix"value="/WEB-INF/templates/"/><propertyname="suffix"value=".html"/><propertyname="characterEncoding"value="UTF-8"/><propertyname="templateMode"value="HTML5"/></bean></property></bean></property></bean>
    <!--<!--配置默认视图解析器,注意:不用写-->-->
   <!-- <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">--><!-- <property name="prefix"  value="/WEB-INF/templates/"></property>--><!-- <property name="suffix"  value=".jsp"></property>--><!-- </bean>--><!--3.配置视图控制器view-controller--><!-- 匹配请求路径直接前往视图,不经过 handler 方法 --><!--path:设置处理的请求地址,view-name:设置请求地址所对应的视图名称--><mvc:view-controllerpath="/"view-name="index"/><!-- 4.开放静态资源访问 --><mvc:default-servlet-handler/><!--5.注解驱动--><!--只要设置了视图控制器,就必须设置注解驱动,否则注解全部失效--><mvc:annotation-driven/></beans>

springmvc-config配置文件主要作用

  1. 扫描(controller)组件
  2. thymeleaf视图解析器
  3. 视图控制器view-controller
  4. 开放静态资源访问
  5. 注解驱动

总结

spring-persist.xml主要作用(主要配置和业务逻辑有关的,数据源,mybatis整合,事务控制)

  1. 配置自动扫描的包(service层)
  2. 加载外部属性配置文件(jdbc.properties) 2.1 配置数据源
  3. 配置SqlsessionFactoryBean(整合mybatis) 3.1指定mybatis全局配置文件 3.2 指定Mapper配置文件位置 3.3装配数据源(因为以往都是在mybatis中要配置的)
  4. 配置扫描mapper接口
  5. 配置事务管理器 5.1 装配数据源
  6. 开启基于注解的声明式事务

mybatis-config.xml文件主要作用

  1. Mybatis全局配置,驼峰式命名
  2. 设置实体类别名(以包为单位),方便mapper.xml中resultType直接写类名

springmvc-config配置文件主要作用

  1. 扫描(controller)组件
  2. thymeleaf视图解析器
  3. 视图控制器view-controller
  4. 开放静态资源访问
  5. 注解驱动

web.xml配置文件主要作用

  1. 配置ContextLoaderListener,读取spring-persist.xml文件 1.1 通过context-param 指定Spring框架的配置文件位置 1.2 配置ContextLoaderListener 监听器
  2. 配置springmvc前端控制器dispatcherServlet,对浏览器发送的请求进行统一处理 2.1 通过初始化参数init-param指定SpringMVC配置文件的位置和名称 2.2 将启动控制DispatcherServlet的初始化时间提前到服务器启动时
  3. 配置过滤器CharacterEncodingFilter,设置字符编码
  4. 配置Rest风格的HiddenHttpMethodFilter,将post请求转换为put和Delete请求
标签: mybatis spring java

本文转载自: https://blog.csdn.net/qq_44823283/article/details/125472828
版权归原作者 赤脚的我 所有, 如有侵权,请联系我们删除。

“ssm整合各配置文件详解”的评论:

还没有评论