有了前面自动配置数据源、JDBC与MyBatis的基础后,自动配置MyBatis就很简单了。
注:在说明注解时,第一点加粗为注解中文含义,第二点为一般加在哪身上,缩进或代码块为示例,如:
@注解
语句示例
//代码示例
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
它提供两大接口BaseMapper<T>
和IService<T>
,方便我们操作数据库与业务。
可以令XXXMapper接口继承基类BaseMapper<T>,BaseMapper里实现了一些方法方便我们操作数据库:
同理对于service有顶层接口IService<T>
直接导入场景依赖即可。
<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。
Mybatis里对service层设有顶层接口IService<T>;而对IService<T>有实现类ServiceImpl<操作的基本表,返回类型>
在Bean类里
在Service层里:
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
}
续上面Service层里的配置。
要实现分页功能首先要导入分页插件
@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里(分页功能)
<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>
源码分析,跟前面文章类似,这里不做过多解释。
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
原文:https://www.cnblogs.com/dlhjw/p/15141712.html