美文网首页
mybatis plus学习笔记(2)插件

mybatis plus学习笔记(2)插件

作者: 兴浩 | 来源:发表于2019-04-01 10:45 被阅读0次

1.分页插件PaginationInterceptor

@Configuration
@MapperScan("com.baomidou.mybatisplus.samples.pagination.mapper")
public class MybatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

测试代码

1.调用selectPage方法
2.调用mySelectPage自定义方法

    @Test
    public void tests() {
        System.out.println("----- baseMapper 自带分页 ------");
        Page<User> page = new Page<>(1, 5);
        IPage<User> userIPage = mapper.selectPage(page, new QueryWrapper<User>()
                .eq("age", 20).eq("name", "Jack"));
        assertThat(page).isSameAs(userIPage);
        System.out.println("总条数 ------> " + userIPage.getTotal());
        System.out.println("当前页数 ------> " + userIPage.getCurrent());
        System.out.println("当前每页显示数 ------> " + userIPage.getSize());
        print(userIPage.getRecords());
        System.out.println("----- baseMapper 自带分页 ------");

        System.out.println("json 正反序列化 begin");
        String json = JSON.toJSONString(page);
        Page<User> page1 = JSON.parseObject(json, TypeBuilder.newInstance(Page.class).addTypeParam(User.class).build());
        print(page1.getRecords());
        System.out.println("json 正反序列化 end");

        System.out.println("----- 自定义 XML 分页 ------");
        MyPage<User> myPage = new MyPage<User>(1, 5).setSelectInt(20).setSelectStr("Jack");
        ParamSome paramSome = new ParamSome(20, "Jack");
        MyPage<User> userMyPage = mapper.mySelectPage(myPage, paramSome);
        assertThat(myPage).isSameAs(userMyPage);
        System.out.println("总条数 ------> " + userMyPage.getTotal());
        System.out.println("当前页数 ------> " + userMyPage.getCurrent());
        System.out.println("当前每页显示数 ------> " + userMyPage.getSize());
        print(userMyPage.getRecords());
        System.out.println("----- 自定义 XML 分页 ------");
    }
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
    <select id="mySelectPage" resultType="com.baomidou.mybatisplus.samples.pagination.entity.User">
        select *
        from user where (age = #{pg.selectInt} and name = #{pg.selectStr})
                or (age = #{ps.yihao} and name = #{ps.erhao})
    </select>

2.性能分析插件PerformanceInterceptor

@Configuration
public class MybatisPlusConfig {
    
    @Bean
    public PerformanceInterceptor performanceInterceptor(){
        //启用性能分析插件
        return new PerformanceInterceptor();
    }
}

日志输出

 Time:10 ms - ID:com.baomidou.samples.performance.mapper.StudentMapper.selectList
Execute SQL:SELECT id,name,age FROM student

3.乐观锁插件OptimisticLockerInterceptor

需要加Version注解

@Data
public class User {

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;
    @Version
    private Integer version;
}

测试代码:

    @Test
    public void testUpdateByIdSucc() {
        User user = new User();
        user.setAge(18);
        user.setEmail("test@baomidou.com");
        user.setName("optlocker");
        user.setVersion(1);
        userMapper.insert(user);
        Long id = user.getId();

        User userUpdate = new User();
        userUpdate.setId(id);
        userUpdate.setAge(19);
        userUpdate.setVersion(1);
        Assert.assertEquals("Should update success", 1, userMapper.updateById(userUpdate));
        Assert.assertEquals("Should version = version+1", 2, userUpdate.getVersion().intValue());
    }

输出结果

 Time:10 ms - ID:com.baomidou.mybatisplus.samples.optlocker.mapper.UserMapper.insert
Execute SQL:INSERT INTO user ( name, age, email, version ) VALUES ( ?, ?, ?, ? ) {1: 'optlocker', 2: 18, 3: 'test@baomidou.com', 4: 1}

 Time:0 ms - ID:com.baomidou.mybatisplus.samples.optlocker.mapper.UserMapper.updateById
Execute SQL:UPDATE user SET age=?, version=? WHERE id=? AND version=? {1: 19, 2: 2, 3: 6, 4: 1}

4.执行分析插件SqlExplainInterceptor

拦截防止更新和删除表时不带条件的全局操作

4. 非法SQL语句拦截器IllegalSQLInterceptor

参考:
https://www.jianshu.com/p/1ac66058b3de

相关文章

网友评论

      本文标题:mybatis plus学习笔记(2)插件

      本文链接:https://www.haomeiwen.com/subject/fvembqtx.html