spring+mybatis 配置文件

本文介绍了一个使用 Spring 框架与 MyBatis 完成数据库整合的例子,包括配置自动扫描组件、数据源连接池、事务管理及 MyBatis 的自动扫描等功能。

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

<?xml version="1.0" encoding="UTF-8" ?>   
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.2.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> 
  	 
  	 <!-- 配置自动扫包,包括子包,不与springmvc扫描相同的包,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean-->
  	 <context:component-scan base-package="com.ylg.dao;com.ylg.bean;com.ylg.service"/>
  	  <!-- 引入配置文件 此处为db.properties-->  
  	  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  	  		<property name="location" value="classpath:db.properties">
  	  		</property>
  	  </bean>
  	  
  	  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  	  		<!-- 指定连接数据库的驱动 -->
  	  		<property name="driverClass" value="${driver}"/>
  	  		<property name="jdbcUrl" value="${url}"/>
  	  		<property name="user" value="${username}"/>
  	  		<property name="password" value="${passwd}"/>
  	  		<!-- 指定连接数据库连接池的最大连接数 -->
  	  		<property name="maxPoolSize" value="${maxPoolSize}"/>
  	  		<!-- 指定连接数据库连接池的最小连接数 -->
  	  		<property name="minPoolSize" value="${minPoolSize}"/>
  	  		<!-- 指定连接数据库连接池的初始化连接数 -->
  	  		<property name="initialPoolSize" value="${initialPoolSize}"/>
  	  		<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
  	  		<property name="maxIdleTime" value="${maxIdleTime}"/>
  	  		<!-- 连接关闭时默认将所有未提交的操作回滚。默认:false -->
  	  		<property name="autoCommitOnClose" value="${autoCommitOnClose}"/>
  	  		<!-- 当连接用完时,等待获取新的连接,超时后将抛出SQLException,如果设置为0,则无限等待,单位毫秒,默认:0  -->
  	  		<property name="checkoutTimeout" value="${checkoutTimeout}"/>
  	  		<property name="acquireIncrement" value="${acquireIncrement}"/>
  	  		<property name="acquireRetryAttempts" value="${acquireRetryAttempts}"/>
  	  </bean>
  	  
  	  <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
  	  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  	  		<property name="dataSource" ref="dataSource"/>
  	  		<!-- 自动扫描mapping.xml文件 -->  
  	  		<property name="mapperLocations" value="classpath*:/config/mybatis/*Mapper.xml"/>
  	  </bean>
  	  
  	  <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->  
  	  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  	  		<property name="annotationClass" value="org.springframework.stereotype.Repository"/>
  	  		<property name="basePackage" value="com.ylg.dao"/>
  	  </bean>
  	  
  	  <!-- 由spring管理mybatis的事物 -->  
  	  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  	  		<property name="dataSource" ref="dataSource"/>
  	  </bean>
  	  
  	  <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
  	
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值