spring基于注解与mybatis整合配置代码例子如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd">
<!--扫描所有注解-->
<context:component-scan base-package="cn.*"/>
<!-- 引入数据库配置文件-->
<context:property-placeholder location="database.properties"/>
<!-- 配置数据源-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 配置数据映射器-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 引入数据源-->
<property name="dataSource" ref="dataSource"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>param1=value1</value>
</property>
</bean>
</array>
</property>
<!-- 设置 别名-->
<property name="typeAliasesPackage" value="cn.pojo.*"/>
<!-- mapper映射-->
<property name="mapperLocations" value="cn/dao/*.xml"/>
<!-- 内部配置-->
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="mapUnderscoreToCamelCase" value="true"/>
<property name="autoMappingBehavior" value="PARTIAL"/>
</bean>
</property>
</bean>
<!-- 自动扫描指定包下的Mapper接口,并将它们直接注册为MapperFactoryBean
MapperScannerConfigurer递归扫描基准包下所有接口,若它们在SQL映射文件中定义过,则动态注册为MapperFactoryBean,如此即可批量产生映射器实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="cn.dao"/>
</bean>
<!-- 注册事务管理-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解扫描-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>