SpringBoot集成通用Mapper和分页插件PageHelper(一)

  通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及Example 相关的单表操作。通用 Mapper 是为了解决 MyBatis 使用中 90% 的基本操作,使用它可以很方便的进行开发,可以节省开发人员大量的时间。注意这个框架只适合单表操作,实现了单表的基本增删改查操作,对于多表操作,还是需要自己实现xml,项目文档可以参考github上面的文档(GitHub文档 | 官方文档)接下来就开始整合。
1. 环境准备
  示例demo使用的是MySQL数据库,最低版本要求5.7

create database test001 default CHARACTER SET UTF8MB4 collate utf8mb4_general_ci;
use test001;

drop table if exists user;
drop table if exists role;

create table user
(
    id        varchar(255) primary key comment '主键ID',
    user_name varchar(255) not null comment '用户名',
    password  varchar(255) not null comment '密码',
    email     varchar(255) comment '邮箱',
    role_id   varchar(255) comment '角色ID'
) comment '用户表';

create table role
(
    id        varchar(255) primary key comment '主键ID',
    role_name varchar(255) not null comment '角色名'
) comment '角色表';

2. 导入jar包
  为了方便,这里将需要用上的jar都列举在这里,其中,通用mapper已经集成了MyBatis,所以可以不用重复引入;相反如果你引入了通用mapper和MyBatis两个starter,那么MyBatis官方的自动配置不会生效。

<dependencies>
    <!-- 分页插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.4.1</version>
    </dependency>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.22</version>
    </dependency>
    <!-- mybatis 代码生成器 -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.4.1</version>
    </dependency>
    <!-- oracle -->
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>21.5.0.0</version>
    </dependency>
    <!-- mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.21</version>
    </dependency>
    <!-- 通用Mapper https://github.com/abel533/Mapper/wiki -->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>4.2.1</version>
    </dependency>
    <!-- mybatis, 通用mapper已包含mybatis,可以不用重复引入 -->
    <!--        <dependency>-->
    <!--            <groupId>org.mybatis.spring.boot</groupId>-->
    <!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
    <!--            <version>2.2.2</version>-->
    <!--        </dependency>-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <!-- 测试 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3. 配置
  MyBatis的配置就不在多说了,这里主要说说通用mapper的配置,相关配置参数可以查看 tk.mybatis.mapper.entity.Config
在这里插入图片描述

spring:
  # 数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://10.1.8.41:3306/test001
    username: root
    password: 123456

# mybatis 配置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.hxz.entity
  configuration:
    map-underscore-to-camel-case: true # 驼峰命名
    cache-enabled: true # 开启二级缓存
    lazy-loading-enabled: true # 懒加载,分布查询时需要开启
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 日志输出

# 通用mapper 配置
mapper:
  not-empty: true  # 判断字符串类型 != ''

4. mapper 接口声明
  mapper接口需要继承通用mapper的 tk.mybatis.mapper.common.Mapper 类,该类提供了大量的通用接口

public interface RoleMapper extends tk.mybatis.mapper.common.Mapper<Object> {
}

5. mapper 接口扫描
  使用 tk.mybatis.spring.annotation.MapperScan 扫描,注意不是mybatis的MapperScan

// 使用 tk.mybatis.spring.annotation.MapperScan 扫描,注意不是mybatis的MapperScan
@tk.mybatis.spring.annotation.MapperScan(basePackages = "包名")
public class SpringbootMybatisMapperApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisMapperApplication.class, args);
    }
}

  通过mapper同样是支持MyBatis的原生功能,这里就不在详细说明了

  源码地址:https://gitee.com/peachtec/hxz-study

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

華小灼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值