SpringMVC+IBatis之IBatis配置

本文详细介绍了IBatis框架的核心配置文件SqlMapConfig.xml(即applicationContext.xml),包括数据库连接配置、SqlSessionFactory配置、SqlSessionTemplate配置、事务管理配置等内容,并展示了如何通过Spring框架整合MyBatis。

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

1、IBatis配置概念图

2、SQL map 配置文件(即图中的SqlMapConfig.xml文件)是IBatis配置的核心(主配置文件)。所有东西(从数据库连接到实际所用的SqlMap文件)都要通过此文件中的配置提供给框架的。


                                  工程框架图
其中applicationContext.xml是上面所说的SqlMapConfig.xml配置文件
applicationContext.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:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx"   
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                        http://www.springframework.org/schema/context   
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd  
                        http://www.springframework.org/schema/aop   
                        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
                        http://www.springframework.org/schema/mvc   
                        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
                        http://www.springframework.org/schema/tx   
                        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <!-- 引入JDBC配置文件 -->
	<context:property-placeholder location="classpath:config/db.properties" /> 
	<!-- 配置数据源 -->
	<bean id="dataSource"
	    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	    <property name="driverClassName">
	        <value>${db.drivers}</value>
	    </property>
	    <property name="url">
	        <value>${db.url}</value>
	    </property>
	    <property name="username">
	        <value>${db.user}</value>
	    </property>
	    <property name="password">
	        <value>${db.password}</value>
	    </property>	        
	</bean> 
	<!-- 配置sqlSessionFactory --> 
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="dataSource" ref="dataSource"></property>
	    <property name="configLocation" value="classpath:config/mybatis-config.xml"></property>
	    <property name="mapperLocations" value="classpath:config/mapper/*Mapper.xml" />
	</bean>   
	<!-- 配置sqlSessionTemplate -->  
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
	</bean>
	<!-- 配置事务 --> 
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 文件上传表单的视图解析器 -->  
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
	 
	<!-- 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、
	CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 
	以及equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。 -->
	<context:annotation-config/>
	
	<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="com.xxxxx"/>
	
    <!-- 默认的注解映射的支持 -->  
    <!-- 默认的注解映射的支持 ,它会自动注册DefaultAnnotationHandlerMapping 
    		与AnnotationMethodHandlerAdapter--> 
    <mvc:annotation-driven />   
           
	<!-- 注解请求映射  激活@RequestMapping annotation -->  
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
	<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
	
	<!-- 事物传播特性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- tx:attribute标签所配置的是作为事务的方法的命名类型 -->
		<tx:attributes>
		<!-- propagation="REQUIRED"代表支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 -->
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	<!-- 哪些类哪些方法使用事务 -->
	<aop:config>
		<aop:pointcut id="servicePointcut"
		 expression="execution(* com.xxxxx.service.*.*(..))" />
		 <aop:advisor pointcut-ref="servicePointcut" advice-ref="txAdvice"/>
	</aop:config>
	
	<!-- 引入其他的spring配置文件 --> 
    <import resource="classpath:config/spring/*.xml"/>
</beans>                        

  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值