前言

有了前面自动配置数据源、JDBC与MyBatis的基础后,自动配置MyBatis就很简单了。

注:在说明注解时,第一点加粗为注解中文含义,第二点为一般加在哪身上,缩进或代码块为示例,如:

@注解

  • 中文含义
  • 加在哪
  • 其他……
    • 语句示例
    //代码示例

1. 什么是MyBatis-Plus

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

它提供两大接口BaseMapper<T>IService<T>,方便我们操作数据库与业务。

1.1 BaseMapper<T>接口

可以令XXXMapper接口继承基类BaseMapper<T>,BaseMapper里实现了一些方法方便我们操作数据库:

  • XXXMapper继承该接口后,无需编写mapper.xml文件,即可进行crud操作;

1.2 IService<T>接口

同理对于service有顶层接口IService<T>

2. 整合MyBatis-Plus以及CRUD功能

直接导入场景依赖即可。

2.1 导入场景依赖

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>

引入mybatis-plus-boot-starter后,其帮我们引入了mybatis-plus扩展包与核心包(核心包里引入了mybatis核心包、mybatis与spring整合的包)、starter-jdbc。

2.2 CRUD功能

Mybatis里对service层设有顶层接口IService<T>;而对IService<T>有实现类ServiceImpl<操作的基本表,返回类型>

在Bean类里

@TableName("数据库名")

  • 数据库表名
  • 标注在bean包下的类里;
  • mybatis-plus默认识别数据库表名与类名相同的属性进行绑定,当名字不同时,需要在bean包下的对应类上标注@TableName("数据库名");
  • 当名字相同时可以不标注。

在Service层里

public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
}

3. Mabatis-plus实现分页功能

续上面Service层里的配置。

要实现分页功能首先要导入分页插件

  • 在config包下,注意版本;
@Configuration
public class MyBatisConfig { /**
* MybatisPlusInterceptor
* @return
*/
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); //这是分页拦截器
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
paginationInnerInterceptor.setOverflow(true);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInnerInterceptor.setMaxLimit(500L);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor); return mybatisPlusInterceptor;
}
}

在controller类里(分页功能)

@Controller
public class TableController { @Autowired
UserService userService; //删除用户
@GetMapping("/user/delete/{id}")
public String deleteUser(@PathVariable("id") Long id,
@RequestParam(value = "pn",defaultValue = "1")Integer pn,
RedirectAttributes ra){ userService.removeById(id);
//重定向回到当前页码
ra.addAttribute("pn",pn);
//重定向
return "redirect:/dynamic_table";
} //请求参数pn表示跳转到的页数,并设置默认值为1
@GetMapping("/dynamic_table")
public String dynamic_table(@RequestParam(value="pn",defaultValue = "1") Integer pn,Model model){ //构造分页参数
Page<User> page = new Page<>(pn, 2);
//调用page进行分页,返回page类型结果
Page<User> userPage = userService.page(page, null); // userPage.getRecords()
// userPage.getCurrent()
// userPage.getPages() //将page设置进model属性里
model.addAttribute("users",userPage); return "table/dynamic_table";
} @GetMapping("/responsive_table")
public String responsive_table(){
return "table/responsive_table";
} @GetMapping("/editable_table")
public String editable_table(){
return "table/editable_table";
}
}

在HTML里(分页功能)

  • users.current:表示当前页数
  • users.pages:总页数
  • users.total:总记录数
<div class="adv-table">
<table class="display table table-bordered table-striped" id="dynamic-table">
<thead>
<tr>
<th>#</th>
<th>name</th>
<th>age</th>
<th>email</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr class="gradeX" th:each="user: ${users.records}">
<td th:text="${user.id}"></td>
<td>[[${user.name}]]</td>
<td th:text="${user.age}">Win 95+</td>
<td th:text="${user.email}">4</td>
<td>
<a th:href="@{/user/delete/{id}(id=${user.id},pn=${users.current})}" class="btn btn-danger btn-sm" type="button">删除</a>
</td>
</tr>
</tfoot>
</table> <div class="row-fluid">
<div class="span6">
<div class="dataTables_info" id="dynamic-table_info">
当前第[[${users.current}]]页 总计 [[${users.pages}]]页 共[[${users.total}]]条记录
</div>
</div>
<div class="span6">
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class="prev disabled"><a href="#">← 前一页</a></li>
<li th:class="${num == users.current?'active':''}" th:each="num:${#numbers.sequence(1,users.pages)}" >
<a th:href="@{/dynamic_table(pn=${num})}">[[${num}]]</a>
</li>
<li class="next disabled"><a href="#">下一页 → </a></li>
</ul>
</div>
</div>
</div>
</div>

4. *MyBatis-Plus自动配置源码分析

源码分析,跟前面文章类似,这里不做过多解释。

  • 引入mybatis-plus-boot-starter后,首先找到META-INF包下的spring.factories工厂,通过读取EnableAutoConfiguration获取启动时加载的类 :MybatisPlusAutoConfiguration配置类;进而找到配置项 MybatisPlusProperties
  • 通过自动配置项得知:通过mybatis-plus:xxx对mybatis-plus进行定制;
  • 通过自动配置类得知:给我们自动配置好了哪些组件:
    • SqlSessionFactory自动配置好:使用配置过的数据源dataSource,配置MyBatis的全局配置文件configLocation;
    • mapperLocations自动配置好:有默认值 classpath*:/mapper/**/*.xml,即:任意包的类路径下的所有mapper文件夹下任意路径下的所有xml都是sql映射文件。 建议以后sql映射文件,放在mapper路径下。
    • SqlSessionTemplate自动配置好
    • MapperScannerRegistrarNotFoundConfiguration自动配置好: @Mapper 标注的接口也会被自动扫描;可以直接@MapperScan("com.atguigu.admin.mapper") 批量扫描就行

最后

新人制作,如有错误,欢迎指出,感激不尽!
欢迎关注公众号,会分享一些更日常的东西!
如需转载,请标注出处!

SpringBoot | 3.3 整合MyBatis-Plus的更多相关文章

  1. SpringBoot 2.X整合Mybatis

    1.创建工程环境 勾选Web.Mybatis.MySQL,如下 依赖如下 <dependency> <groupId>org.springframework.boot</ ...

  2. 【springboot spring mybatis】看我怎么将springboot与spring整合mybatis与druid数据源

    目录 概述 1.mybatis 2.druid 壹:spring整合 2.jdbc.properties 3.mybatis-config.xml 二:java代码 1.mapper 2.servic ...

  3. SpringBoot当中如何整合mybatis和注入

    [学习笔记] 6.整合mybatis和注入: 马克-to-win@马克java社区: 根据第3部分的helloworld例子,用那个项目做底子.pom.xml只需要加入mybatis和mysql的部分 ...

  4. springboot笔记07——整合MyBatis

    前言 Springboot 整合 MyBatis 有两种方式,分别是:"全注解版" 和 "注解.xml混合版". 创建项目 创建Springboot项目,选择依 ...

  5. springboot学习2 整合mybatis

    springboot整合mybatis 一.添加mybatis和数据库连接的依赖 <!--整合mybatis--> <dependency> <groupId>or ...

  6. SpringBoot学习之整合Mybatis

    本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...

  7. SpringBoot | 3.2 整合MyBatis

    目录 前言 1. 导入MyBatis场景 1.1 初始化导向 1.2 手动导入 2. *MyBatis自动配置原理 3. 全局配置文件 @Mapper @MapperScan 3.1 配置模式 3.2 ...

  8. 利用IDEA搭建SpringBoot项目,整合mybatis

    一.配置文件.启动项目 生成之后这几个文件可以删掉的 配置application spring.datasource.url=jdbc:mysql://localhost:3306/test?serv ...

  9. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  10. SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源

    一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...

随机推荐

  1. 37、linux下安装python3.6和django

    37.1.安装python: 1.python介绍: python是一种面向对象的,解释型的计算机语言,它的特点是语法简介,优雅,简单易学.1989年诞生,Guido(龟叔)开发. 编译型语言:代码在 ...

  2. 37、mysql数据库(dcl)

    在数据库中参考:"12.创建mysql用户及赋予用户权限"文件.

  3. Vue(14)slot插槽的使用

    为什么使用slot slot(插槽) 在生活中很多地方都有插槽,电脑usb的插槽,插板当中的电源插槽 插槽的目的是为了让我们原来的设备具备更多的扩展性 比如电脑的USB我们可以插入U盘,手机,鼠标,键 ...

  4. linux修改 ls 命令的时间显示格式

    一直不习惯 ll 命令将时间日期格式显示为 周名和月名,想要纯粹的 数字格式,找了好久,终于想到一个办法--alias. [root@localhost ~]# alias #显示当前已存在的alia ...

  5. 解决spring boot中文乱码问题

    在开发或学习当中,我们不可避免的会碰到中文乱码的问题(好想哭,但还是要保持微笑!) 今天,在学习spring boot中碰到了中文乱码问题. 首先,看了一下workspace是不是设置utf-8默认字 ...

  6. pixel的Edxposed刷机过程

    1.先解开bl锁 这里的步骤,因为我机子本来就是解过的了,所以简单记录一下过程好了 第一步:确保你的环境变量是否设置好了,判断的标准就是打开终端(我是mac),usb连接上,然后输入 adb devi ...

  7. 2012年第三届蓝桥杯C/C++程序设计本科B组省赛 密码发生器

    密码发生器 题目描述: ```bash 在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全:如果设置不好记的密码,又担心自己也会忘记:如果写在纸上, ...

  8. IDA 动态调试

    感谢南邮,让我把ida动态调试,给搞定了,困扰了很久,之前下的ubuntu的源,好像有问题,ifconfig这个命令一直装不上,突然想起来了我的服务器很久没用了,重装了下系统,换成ubuntu,这里记 ...

  9. HCNA Routing&Switching之静态路由

    前文我们聊到了路由的相关概念和路由基础方面的话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/14947897.html:今天我们聊聊静态路由相关话题: 回顾 ...

  10. Nacos配置中心功能

    目录 Nacos的配置管理模型 命名空间(Namespace) 配置分组(Group) 配置集(Data ID) 配置项 一个最佳实践 命名空间管理 配置管理 参考 Nacos的配置管理模型 对于Na ...