Spring MVC - SSM整合入门(02)

本文详细介绍了SSM框架整合的具体步骤及配置文件设置,包括如何配置applicationContext.xml、sqlMapConfig.xml、springmvc.xml和web.xml等关键文件,实现基于Tomcat的SSM框架整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置

整合SSM最终的目的是在Tomcat启动的时候带动三个框架实现良性循环。这其中涉及到四个配置文件web.xmlapplicationContext.xmlspringmvc.xmlsqlMapConfig.xml

首先SSM框架的核心是Spring,所以应该是在applicationContext.xml中配置springmvc.xmlsqlMapConfig.xml,但是Spring MVC和Spring是一家的,所以只需要把Mybatis配置进去就可以。

第一步:在applicationContext.xml配置Mybatis和数据库连接池:配置sqlSessionFactoryBean和sqlMapper接口所在的包。

<?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:aop="http://www.springframework.org/schema/aop" 
      	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 
         	http://www.springframework.org/schema/context/spring-context.xsd
         	http://www.springframework.org/schema/tx 
         	http://www.springframework.org/schema/tx/spring-tx.xsd
         	http://www.springframework.org/schema/aop 
         	http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 引入数据库信息properties -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<!-- Mybatis的工厂(不同的项目只需要改configLocation的value) -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- 核心配置文件的位置 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
	</bean>

	<!-- Mapper动态代理开发(不同的项目只需要改basePackage的value)  -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 基本包,com.sm.mapper是sqlMap接口的根包,它下面及其子包下面的sqlMap接口都会被扫描到 -->
		<property name="basePackage" value="com.ssm.mapper"/>
	</bean>
</beans>

第二步:配置sqlMapConfig.xml:由于数据库连接池交给Spring管理并且使用动态Mapper开发,所以这个文件的作用就是配置别名来帮我们简便开发。

<?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>
	<!-- 设置别名 -->
	<typeAliases>
		<!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
		<package name="com.itheima.springmvc.pojo" />
	</typeAliases>
</configuration>

第三步:配置springmvc.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:p="http://www.springframework.org/schema/p"
	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-4.0.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-4.0.xsd">
        
        <!-- 扫描@Controler  @Service   -->
        <context:component-scan base-package="com.ssm.controller"/>
        
        <!-- 配置处理器映射器和处理器适配器 -->
        <mvc:annotation-driven/>
        
        <!-- 视图解释器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<property name="prefix" value="/WEB-INF/jsp/"/>
        	<property name="suffix" value=".jsp"/>
        </bean>
   </beans>

第四步:配置web.xml文件:Tomcat开启时会加载web.xml。Mybatis配置文件已经被配置进applicationContext.xml中,所以在web.xml中需要配置springmvc.xmlapplicationContext.xml

<?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_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ssm-1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置Spring上下文 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 配置Spring监听器,就是加载 applicationContext.xml文件 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 前端控制器 -->
  <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:springmvc.xml</param-value>
 	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

流程分析

当服务器开启时,web.xml会被加载进去,就会把SSM框架的配置文件都加载进去,使用注解开发时在bean类上加上相应注解就可以。对于Mybatis的动态代理开发,虽然都是接口,不能使用注解,但在applicationContext.xml中已经配置了扫描,会自动帮我们创建Spring的bean,使用注解@Autowired就可以自动注入属性。对于其他的bean,使用@Component@Service@Controller注解标识一下就可以。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值