头文件
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"></beans>
数据源
<!-- 数据源 --><context:property-placeholder location="classpath:db.properties"/><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property></bean>
事务管理器
<!-- 事务管理器 --><bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property></bean><!-- 基于注解的事务管理 --><tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
sqlSessionFactoryBean及插件
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean"> <!-- 数据源 --> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!-- 别名处理 --> <property name="typeAliasesPackage" value="com.atguigu.mp.beans"/> <!-- 注入全局MP策略配置 --> <property name="globalConfig" ref="globalConfiguration"/> <!-- 配置插件 --> <property name="plugins"> <list> <!-- 分页插件--> <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/> <!-- 性能分析插件 --> <bean class="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor"> <!-- sql格式化--> <property name="format" value="true"/> <!-- 最长时间--> <!-- <property name="maxTime" value="5"/>--> </bean> <!-- 乐观锁拦截器--> <bean class="com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor"/> </list> </property></bean><!-- 配置mybatis 扫描mapper接口的路径 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.carven.mp.mapper"/></bean>
全局策略配置
<!-- 定义MybatisPlus的全局策略配置--><bean id="globalConfiguration" class="com.baomidou.mybatisplus.core.config.GlobalConfig"> <!-- 关闭logo打印 --> <property name="banner" value="false"/> <property name="dbConfig"> <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig"> <!-- 全局的主键策略 --> <property name="idType" value="AUTO"/> <!-- 全局的表前缀策略配置 --> <property name="tablePrefix" value="tbl_"/> <!-- 逻辑删除全局值(默认开启 值选填 需要实体类@TableLogic注解开启)--> <property name="logicDeleteValue" value="1"/> <property name="logicNotDeleteValue" value="0"/> </bean> </property> <!-- 注入公共字段填充处理器 --> <property name="metaObjectHandler" ref="metaObjectHandler"/></bean><!-- 自定义公共字段填充处理器 --><bean id="metaObjectHandler" class="cn.carven.mp.handler.MyMetaObjectHandler"/>