pom
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
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.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 数据源 -->
<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="dataSourceTransationManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 基于注解的事务管理器 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!-- 配置SqlSessionFactoryBean
mybatis 提供的class: org.mybatis.spring.SqlSessionFactoryBean
mybatis-plus 提供的class: com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean
-->
<!-- -->
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybaits-config.xml"></property>
<!-- 别名出处 -->
<property name="typeAliasesPackage" value="com.atguigu.mp.beans"></property>
<!-- 注入自定义的全局配置 -->
<property name="globalConfig" ref="mpGlobalConfig"></property>
<property name="plugins">
<list>
<!-- 注册分页插件 -->
<bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"></bean>
<!-- 注册执行分析插件 -->
<bean class = "com.baomidou.mybatisplus.plugins.SqlExplainInterceptor">
<!-- 如果发现这是对全局的删除或更新,是否加以阻止 -->
<property name="stopProceed" value="true"></property>
</bean>
<!-- 注册性能分析插件 -->
<bean class = "com.baomidou.mybatisplus.plugins.PerformanceInterceptor">
<!-- 最大时间,单位毫秒 -->
<property name="maxTime" value="80"></property>
</bean>
<!-- 注册乐观锁插件 -->
<bean class = "com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor"></bean>
</list>
</property>
</bean>
<bean id="mpGlobalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
<!-- 表名、字段名、是否使用下划线命名(默认 true: 数据库下划线命名) -->
<property name="dbColumnUnderline" value="true"></property>
<!-- 全局的主键策略 -->
<property name="idType" value="0"></property>
<!-- 全局的表前缀配置 -->
<property name="tablePrefix" value="tb_"></property>
</bean>
<!-- 配置mybatis 扫描mapper接口的路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ezerbel.mp.mapper"></property>
</bean>
</beans>
测试分页效果
public class TestMyBatisPlusPage {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeMapper mapper = ctx.getBean("employeeMapper", EmployeeMapper.class);
/**
* 测试分页插件
*/
@Test
public void testPage()
{
Page<Employee> page = new Page<>(2, 3);
List<Employee> emps = mapper.selectPage(page, null);
emps.forEach(System.out::println);
System.out.println("=========获取分页相关信息==========");
System.out.println("总条目数:"+ page.getTotal());
System.out.println("当前页码: "+page.getCurrent());
System.out.println("总页码: "+page.getPages());
System.out.println("每页显示的条数: "+page.getSize());
System.out.println("是否有上一页:"+page.hasPrevious());
System.out.println("是否有下一页:"+page.hasNext());
page.setRecords(emps);
}
}
乐观锁额外配置
entity添加version属性
数据表添加version字段
测试乐观锁
package com.ezerbel.mp.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ezerbel.mp.bean.Employee;
import com.ezerbel.mp.mapper.EmployeeMapper;
public class testOptimisticLocker {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeMapper mapper = ctx.getBean("employeeMapper",EmployeeMapper.class);
@Test
public void testOptimisticLocker() {
Employee employee = new Employee();
employee.setId(11);
employee.setLastName("Anrry");
employee.setEmail("Marry@firefox.com");
employee.setAge(12);
employee.setVersion(0);//如果数据库中对应的version不为0,则无法更新
mapper.updateById(employee);
}
}
测试性能分析插件
public class testSqlPerform {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeMapper mapper = ctx.getBean("employeeMapper", EmployeeMapper.class);
@Test
public void testSqlPerfomr() {
Employee employee = new Employee();
employee.setLastName("Marry");
employee.setEmail("marry@sina.com");
employee.setGender("0");
employee.setAge(12);
mapper.insert(employee);
}
}
测试执行分析插件
public class testSqlExplain {
ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeMapper mapper = ctx.getBean("employeeMapper", EmployeeMapper.class);
@Test
public void testSqlExplain() {
mapper.delete(null);//删除全表
}
}