36 SpringBoot与通用Mapper和分页插件整合

1 引入依赖


<dependency>
    <groupId>com.github.pagehelpergroupId>
    <artifactId>pagehelper-spring-boot-starterartifactId>
    <version>RELEASEversion>
    <exclusions>
        <exclusion>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
        exclusion>
    exclusions>
dependency>


<dependency>
    <groupId>tk.mybatisgroupId>
    <artifactId>mapper-spring-boot-starterartifactId>
    <version>2.0.2version>
dependency>

2 配置

#通用Mapper
mapper.mappers=com.gp6.springboot31.utils.MapperUtil
mapper.identity=MYSQL

#分页插件
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

3 扫描更改

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// 注意包路径
import tk.mybatis.spring.annotation.MapperScan;

// 使用MapperScan批量扫描所有的Mapper接口,此时CartMapper上的mapper就可以注释
@MapperScan(value = "com.gp6.springboot31.mapper")
@SpringBootApplication
public class Springboot31Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot31Application.class, args);
    }
}

4 自定Mapper接口

package com.gp6.springboot31.utils;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

public interface MapperUtil<T> extends Mapper<T>, MySqlMapper<T> {
}

5 Mapper继承MapperUtil

public interface ItemParamTemplateMapper extends MapperUtil<ItemParamTemplate> {
 
}

6 测试

@GetMapping("/itemParamTemplate")
public PageInfo<ItemParamTemplate> selectItemParamTemplateList() {
    PageHelper.startPage(1,5);
    return new PageInfo<>(itemParamTemplateMapper.selectAll());
}

你可能感兴趣的:(SpringBoot)