SSM整合配置文件(整合时不要心急一步一步的来)

在项目下创建resource源文件夹放配置文件
spring配置文件:applicationContext.xml
在spring配置文件里需要做:
扫描service包,开启@Service支持管理对象
加载数据连接属性文件
管理连接池
管理SqlSessionFactory
扫描配置Mapper 动态创建mapper包中所有接口的实例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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
">
	<!-- 扫描包路径:上下文组件扫描 ,Spring容器会去扫描cn.it.show.service这个包及其子包中所有的类, 如果发现类上有类似@Controller这样注解,就会创建该类的对象,并进行管理 -->
	<context:component-scan base-package="cn.itshow.service"></context:component-scan>
	<!-- 2.加载属性文件:连接数据库的参数。system-properties-mode="NEVER"表示忽略前缀的配置 不加前缀要写system-properties-mode="NEVER" -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 管理连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.user}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<!-- 管理SqlSessionFactory -->
	<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置mybatis (mapper)映射器路径 -->
		<property name="mapperLocations" value="classpath:cn/itshow/mapper/*Mapper.xml" />
		<!-- 配置别名 -->
		<property name="typeAliasesPackage" value="cn.itshow.domain"></property>
	</bean>
	<!-- 扫描配置Mapper 动态创建mapper包中所有接口的实例 -->
	<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.itshow.mapper"></property>
	</bean>

</beans>

springmvc配置文件:applicationContext-mvc
在springmvc配置文件中需要做的事情是:
扫描cn.itshow.controller包,开启对@Controller注解支持
开启Spring对Mvc的支持 = 能够使用@Requestmapping注解
开启静态资源放行
视图解析器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">	
	<!-- 扫描包路径:上下文组件扫描 ,Spring容器会去扫描cn.itsource这个包及其子包中所有的类, 如果发现类上有类似@Controller这样注解,就会创建该类的对象,并进行管理 -->
	<context:component-scan base-package="cn.itshow.controller"></context:component-scan>
	<!-- 开启Spring对Mvc的支持 = 能够使用@Requestmapping注解 -->
	<mvc:annotation-driven></mvc:annotation-driven>
		<!-- 开启静态资源放行 -->
	<mvc:default-servlet-handler />
	<!-- 视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

在web.xml中需要配置:
前端控制器:需要加载springmvc的配置文件
监听器:需要加载spring的配置文件
过滤器:解决post中文乱码问题

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>ssm0412ssm</display-name>
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 加载springmvc -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


	<!-- 配置监听器 加载初始化spring -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:appcalitionContext.xml</param-value>
	</context-param>

	<!--过滤器:解决post乱码问题 -->
	<filter>
		<filter-name>filter</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>
			<!--强制指定字符编码,即使request或response设置了字符编码,也会强制使用当前设置的,任何情况下强制使用此编码 -->
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


</web-app>
发布了23 篇原创文章 · 获赞 1 · 访问量 168

猜你喜欢

转载自blog.csdn.net/weixin_45528650/article/details/105496241