上一篇配置好了druid,现在继续配mybatis
一、spring-config.xml启用cglib代理
<!-- aspectj aop启用CGliB代理 -->
<aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>
<context:annotation-config/>
<!-- 扫描service -->
<context:component-scan base-package="com.ranqing.service"/>
二、spring-mybatis.xml进行整合
<!-- Spring整合Mybatis -->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描Mapping.xml文件 ,注意classpath:指定的是resource文件目录-->
<property name="mapperLocations" value="classpath:/sqlMapperXml/*.xml"></property>
<property name="configLocation" value="classpath:xml/mybatis-config.xml"></property>
<!--model里面装的是实体类-->
<property name="typeAliasesPackage" value="com.ranqing.model"/>
<property name="globalConfig" ref="globalConfig"/>
<property name="plugins">
<array>
<!-- 分页插件配置 -->
<bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql"/>
<property name="optimizeType" value="aliDruid" />
</bean>
</array>
</property>
</bean>
<!-- MP 全局配置 -->
<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<property name="idType" value="0"/>
<property name="dbColumnUnderline" value="true"/>
</bean>
<!-- MyBatis 动态实现 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 对Dao 接口动态实现,需要知道接口在哪 : ***Mapper extends BaseMapper<Organization>() -->
<property name="basePackage" value="com.ranqing.mapper"/>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- 事务管理 属性 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config expose-proxy="true" proxy-target-class="true">
<aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.ranqing.service..*.*(..))"/>
</aop:config>
三、配置mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="false"/>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="aggressiveLazyLoading" value="true"/>
<setting name="logImpl" value="LOG4J2"/>
</settings>
</configuration>
另外
classpath指定的路径是resource资源目录;还可以进行逆向生成