ssm框架整合

本文详细介绍了Spring、SpringMVC及MyBatis(SSM)框架整合的具体步骤,包括spring.xml与springmvc.xml配置文件的设置,如组件扫描、数据源配置、事务管理等关键环节。

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

今天第一次整合ssm框架,记录一下全过程备录

 

spring.xml配置:

 

<context:component-scan base-package="com.bbs">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation"
			expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>

	<context:property-placeholder location="WEB-INF/properties/db.properties" />

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl"
			value="jdbc:mysql://localhost:3306/bbs?useUnicode=true&characterEncoding=utf-8" />
		<property name="user" value="root" />
		<property name="password" value="123456" />
	</bean>

	<!-- 2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource / typeAliasesPackage -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath:com/bbs/mapper/*.xml"/>
		<property name="typeAliasesPackage" value="com.bbs.pojo" />
	</bean>

	<!-- 3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer sqlSessionFactory 
		/ basePackage -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.bbs.dao"/>
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>

	<!-- 4. 事务管理 : DataSourceTransactionManager -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 5. 使用声明式事务 -->
	<tx:annotation-driven transaction-manager="txManager" />

SqlSessionFactoryBean:

 

dataSource:配置的数据源

mapperLocations:当我们不想将mappser.xml文件与Dao接口放在同一个包下面我们就需要这个配置,配置我们mapper.xml文件所在的位置,这里需要注意一点的是这里路径 得加上classpath:,我测试了一下,当把这些配置文件都放在src下面也是需要这个classpath:的

typeAliasesPackage:配置我们的别名

MapperScannerConfigurer:

basePackage:这是配置我们dao接口所在的位置

sqlSessionFactory:配置我们的SqlSessionFactory 也就是我们上面所配置的

 

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: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-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!--  
		需要进行 Spring 整合 SpringMVC 吗 ?
		还是否需要再加入 Spring 的 IOC 容器 ?
		是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 ContextLoaderListener ?
		
		1. 需要: 通常情况下, 类似于数据源, 事务, 整合其他框架都是放在 Spring 的配置文件中(而不是放在 SpringMVC 的配置文件中).
		实际上放入 Spring 配置文件对应的 IOC 容器中的还有 Service 和 Dao. 
		2. 不需要: 都放在 SpringMVC 的配置文件中. 也可以分多个 Spring 的配置文件, 然后使用 import 节点导入其他的配置文件
	-->
	
	<!--  
		问题: 若 Spring 的 IOC 容器和 SpringMVC 的 IOC 容器扫描的包有重合的部分, 就会导致有的 bean 会被创建 2 次.
		解决:
		1. 使 Spring 的 IOC 容器扫描的包和 SpringMVC 的 IOC 容器扫描的包没有重合的部分. 
		2. 使用 exclude-filter 和 include-filter 子节点来规定只能扫描的注解
	-->
	
	<!--  
		SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean. 
		返回来呢 ? 反之则不行. Spring IOC 容器中的 bean 却不能来引用 SpringMVC IOC 容器中的 bean!
	-->
	
	<context:component-scan base-package="com.bbs" use-default-filters="false">
		<context:include-filter type="annotation" 
			expression="org.springframework.stereotype.Controller"/>
		<context:include-filter type="annotation" 
			expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>

	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven></mvc:annotation-driven>
	
</beans>

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值