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>