SSM——配置文件

SSM整合

完成后的项目结构如下:

SSM——配置文件_第1张图片

SSM——配置文件_第2张图片

首先加入以下配置文件:

  1. db.properites
  2. generatorConfig.xml
  3. mybatis.xml
  4. spring-mvc.xml
  5. spring-boot.xml

db.properites

用来配置数据源信息,方便其他配置文件引用

内容如下:

jdbc.url=jdbc:mysql://localhost:3306/ssmstoredb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.username=ssmdb
jdbc.password=123
connection_pools.min_pool_size=5
connection_pools.acquire_increment=5
connection_pools.max_pool_size=100
connection_pools.initial_pool_size=5
connection_pools.max_idle_time=600
connection_pools.checkout_timeout=60000

需要注意的是,如果使用mybatis8.0的话,驱动需要需要填:

jdbc.driver=com.mysql.cj.jdbc.Driver

如果使用mybatis5.0的话,驱动需要填

jdbc.driver=com.mysql.jdbc.Driver

MyBaitGenerator.xml

参考自动生成Mybatis文档

接下来介绍Spring相关的配置文件

需要介绍的有SpringMVC配置文件、Spring配置文件以及Web配置文件

  • Spring配置文件配置的是父容器,用来处理后台依赖(除了控制层)
  • SpringMvc配置文件配置的是子容器,用来扫描控制层

Spring配置文件

解决数据源、依赖注入、事务处理的问题

  1. 启动注解扫描(除了控制器)

    控制器在springmvc配置文件中进行扫描

  2. 读取db.properties文件(数据源信息)

  3. 使用Spring继承的c390配置数据源

  4. 配置Session工厂(数据源和(Mapper))

  5. 配置事务管理器,启动注解事务

  6. 配置mapper扫描器

	
	<context:component-scan base-package="ssm">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	context:component-scan>

	
	<context:property-placeholder location="classpath:db.properties" ignore-resource-not-found="true" ignore-unresolvable="true"/>
	
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
			<property name="driverClass" value="${jdbc.driver}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="initialPoolSize" value="${connection_pools.initial_pool_size}" />
		<property name="minPoolSize" value="${connection_pools.min_pool_size}" />
		<property name="maxPoolSize" value="${connection_pools.max_pool_size}" />
		<property name="maxIdleTime" value="${connection_pools.max_idle_time}" />
		<property name="acquireIncrement" value="${connection_pools.acquire_increment}" />
		<property name="checkoutTimeout" value="${connection_pools.checkout_timeout}" />
	bean>
	
	
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="configLocation" value="classpath:mybatis.xml">property>
		<property name="dataSource" ref="dataSource" />
	bean>
	
	
	
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	bean>
	
	
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	
	
	
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="ssm.mapper" />
	bean>

数据源文件不写到jdbc文件里

c3k0数据源,与spring集成好了

SpringMVC配置文件

  1. 启动注解扫描,只扫描控制器的部分
  2. 配置文件上传组件,包括文件大小上限
  3. 配置视图解析器,包括前缀后缀
  4. 配置转换器,转换json数据
  5. 配置静态资源

	
	<context:component-scan base-package="ssm" use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	context:component-scan>
	
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		
		<property name="defaultEncoding" value="utf-8">property>
		
		<property name="maxUploadSize" value="1500000000000">property>
	bean>
	
		
	<bean  id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		
		<property name="prefix" value="/WEB-INF/view/">property>
		
		<property name="suffix" value=".jsp">property>
	bean>
	
	
	<mvc:annotation-driven>
		
		<mvc:message-converters>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/html;charset=UTF-8value>
					list>
				property>
			bean>
		mvc:message-converters>
	mvc:annotation-driven>
	

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


beans>

Web配置文件

web.xml文件是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。

  1. 配置监听器
  2. 启动Spring容器
  3. 启动过滤器,配置过滤器处理路径
  4. 启动servlet,配置servlet处理路径

  <display-name>ssmSpringWebdisplay-name>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring-root.xmlparam-value>
  context-param>
  
  
  
   <filter>
		<filter-name>characterEncodingFilterfilter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
		<init-param>
			<param-name>encodingparam-name>
			<param-value>UTF-8param-value>
		init-param>
		<init-param>
			<param-name>forceEncodingparam-name>
			<param-value>trueparam-value>
		init-param>
	filter>
	
	<filter-mapping>
		<filter-name>characterEncodingFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>


	
	<servlet>
		<servlet-name>springmvcservlet-name>
		
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:spring-mvc.xmlparam-value>
		init-param>
		
		
		<load-on-startup>1load-on-startup>
	servlet>

	
	<servlet-mapping>
		<servlet-name>springmvcservlet-name>
		
		<url-pattern>/url-pattern>
	servlet-mapping>
web-app>

详细说明参考"web.xml配置文件"文档

总的来说

  • Spring配置文件是负责组件的管理
  • SpringMvc配置文件是负责Web相关的东西
  • web.xml配置文件是负责加载前两个配置文件

你可能感兴趣的:(SSM——配置文件)