SSM整合-spring配置xml约束

本文详细介绍如何在Spring框架中整合MyBatis,包括数据库连接池配置、SqlSessionFactory设置、Dao接口扫描及SpringMVC与事务管理配置。

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

spring-dao.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"
	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">

<!-- 配置整合mybatis过程 -->
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<!-- 配置连接池属性 -->
	<property name="driverClass" value="${jdbc.driver}"></property>
	<property name="jdbcUrl" value="${jdbc.url}"></property>
	<property name="user" value="${jdbc.username}"></property>
	<property name="password" value="${jdbc.password}"></property>

	<!-- c3p0连接池的私有属性 -->
	<property name="maxPoolSize" value="30"></property>
	<property name="minPoolSize" value="10"></property>
	
	<!-- 关闭连接后不自动commit -->
	<property name="autoCommitOnClose" value="false"></property>
	<!-- 获取连接超时时间 -->
	<property name="checkoutTimeout" value="10000"></property>
	<!-- 当获取连接失败重试次数 -->
	<property name="acquireRetryAttempts" value="2"></property>
	
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 注入数据库连接池 -->
	<property name="dataSource" ref="dataSource"></property>
	<!-- 配置MyBatis全局配置文件:mybatis-config.xml -->
	<property name="configLocation" value="classpath:mybatis-config.xml"></property>
	<!-- 扫描entity包,使用别名 -->
	<property name="typeAliasesPackage" value="com.zkx.o2o.entity"></property>
	<!-- 扫描sql配置文件:mapper需要的xml文件 -->
	<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 注入sqlSessionFactory -->
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	<!-- 给出需要扫描Dao接口包 -->
	<property name="basePackage" value="com.zkx.o2o.dao"></property>
</bean>

</beans>

spring-service.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: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">
    
    <!-- 扫描service包下所有注解的类型 -->
    <context:component-scan base-package="com.zkx.o2o.service"></context:component-scan>
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     	<!-- 注入数据库连接池 -->
     	<property name="dataSource" ref="dataSource"></property>
     </bean>
     
     <!-- 配置基于注解的声明式事务 -->
     <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

spring-web.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.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解模式 -->
    <mvc:annotation-driven/>
    
    <!-- 2.静态资源默认servlet配置
    	(1)加入对静态资源的处理:js,gif,png
    	(2)允许使用"/"做整体映射
     -->
    <mvc:resources location="/resources/" mapping="/resources/**"/>
    <mvc:default-servlet-handler/>
    
    <!-- 3.定义视图解析器 -->
    <bean id="viewResolver"
    	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/html"></property>
    	<property name="suffix" value=".html"></property>
    </bean>
    
    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="com.zkx.o2o.web"></context:component-scan>
</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值