SpringMVC_SSM整合-day03

1.创建Maven项目

在这里插入图片描述

2.导入Spring SpringMVC Mybatis 以及数据库驱动包

在这里插入图片描述

3.在resources下面创建三个配置文件 分别是

  • Spring的配置文件 (applicationContext.xml)
  • myabtis的配置文件(SqlMapConfig.xml)
  • SpringMVC的配置文件 (springmvc-servlet.xml)

4.修改配置文件内容

3.1 配置applicationContext.xml

  • 将配置文件加入加载到spring中
  • 数据库连接池配置
  • sqlSeesionFactory核心配置文件写到Spring
  • 配置mapper自动扫描包,生成代理对象
<?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:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<!-- 数据库连接池配置 -->
	<!-- 加载数据配置文件 -->
	<context:property-placeholder
		location="classpath:*.properties" />
	<!--数据库连接池配置 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
	</bean>

	<!-- 配置sqlSeesionFactory,需要引用连接池 -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation"
			value="classpath:SqlMapConfig.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!--配置mapper扫描包生成mapper对象 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="dao"></property>
	</bean>
<!--  业务层对象是Spring来管理的,所以需要自动扫描  -->
	<context:component-scan base-package="com.test.service"></context:component-scan>
</beans>

3.2 配置mybatis SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
   <mappers>
   <!--  可能分页需要在这里面配置,但是mapper.xml不用配置-->
   </mappers>
</configuration>

3.3 配置SpringMVC文件 springmvc-servlet.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"
	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-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 处理器映射器 处理器适配器 视图解析器 -->
	<mvc:annotation-driven />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>


	<!--资源映射 不拦截静态资源 -->
	<mvc:resources location="/" mapping="/**/*.*" />
	
<!-- 拦截器:	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/login"/>表示哪些路径被拦截,写controller
			<bean class="com.test.myinter.MyInter"></bean>
		</mvc:interceptor>
	</mvc:interceptors> -->
	
<!-- web层是SpringMVC来管理的需要单独扫描 -->
	<context:component-scan base-package="com.test.controller"/>
</beans>

3.4 web.xml配置写SpringMVC的dispatcherServlet,以及listener生成applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	id="WebApp_ID" version="4.0">
	<display-name>Study_SSM</display-name>

	<!-- 配置SpringMVC,因为他本质是servlet,所以要配置DispatcherServlet -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


<!-- 在web.xml配置编码处理的过滤器 ,解决中文乱码问题 -->
	<filter>
		<filter-name>encodingFilter</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>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

4.扫描

  • controller在SpringMVC的配置文件中扫描
  • 除了controller,其他层的扫描在Spring中扫描
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值