sprin-mybatis.xml文件

本文档详细介绍了如何使用Spring框架整合MyBatis,包括配置Druid连接池、SqlSessionFactory及Mapper接口扫描等关键步骤,并实现了声明式事务管理。

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

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p"
    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"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="  
       http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
       http://www.springframework.org/schema/mvc   
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd   
       http://www.springframework.org/schema/tx   
       http://www.springframework.org/schema/tx/spring-tx-4.3.xsd   
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util-4.3.xsd
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 


   <util:properties id="cfg" location="classpath:config.properties"/>  
   <!--配置DruidDataSource连接池 -->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close" init-method="init" lazy-init="true">
		<property name="driverClassName" value="#{cfg.driver}" />
		<property name="url" value="#{cfg.url}" />
		<property name="username" value="#{cfg.username}" />
		<property name="password" value="#{cfg.password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="#{cfg.initialSize}" />
		<!-- 连接池最大数量 -->
		<property name="maxActive" value="#{cfg.maxActive}" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="#{cfg.minIdle}" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="#{cfg.maxWait}" />
     </bean>
     
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	   <property name="dataSource" ref="dataSource"></property>
	   <property name="configLocation" value="classpath:mybatis-config.xml"></property>
	   <!-- 自动扫描mapping.xml文件 -->
	   <property name="mapperLocations" >
			<list>
			<value>classpath:mapper/*/*.xml</value>
			</list>
		</property>
	 </bean>
    <!-- Mapper接口所在包,Spring会自动查找其下的Mapper -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.tedu.**.dao "/>
	</bean>
	<!-- 配置spring的声明式事务管理 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
    </bean>
    <!--设置注解驱动的事务管理  -->
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值