第一步:导进mybatis-plus的包
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
自动配置:
MybatisPlusAutoConfiguration 配置类,MybatisPlusProperties 配置项绑定。
mybatis-plus:xxx 就是对mybatis-plus的定制
SqlSessionFactory 自动配置好。底层是容器中默认的数据源
mapperLocations 自动配置好的。有默认值。classpath*:/mapper/**/*.xml;任意包的类路径下的所有mapper文件夹下任意路径下的所有xml都是sql映射文件。 建议以后sql映射文件,放在 mapper下
容器中也自动配置好了 SqlSessionTemplate
@Mapper 标注的接口也会被自动扫描;建议直接 @MapperScan(“com.atguigu.admin.mapper”) 批量扫描就行
优点:
只需要我们的Mapper继承 BaseMapper 就可以拥有crud能力
第二步:配置数据库文件
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: abcde
url: jdbc:mysql://localhost:3306/springbootdata
type: com.alibaba.druid.pool.DruidDataSource
server:
port: 8081
第三步:配置实体类和对应的mapper
person实体类:
PersonMapper:
package com.codel.mybatis_plus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.codel.mybatis_plus.pojo.Person;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
//把方法注入容器里面 如果不想每一个都写,就在启动类里面配置
@Mapper
public interface PersonMapper extends BaseMapper<Person> {
List<Person> getPersonAll();
}
第三步:写service和controller
PersonService
package com.codel.mybatis_plus.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.codel.mybatis_plus.pojo.Person;
import java.util.List;
//继承这个接口,里面都是写好的sql语句,可以直接调用
public interface PersonService extends IService<Person> {
List<Person> getPersonAll();
}
PersonController :
package com.codel.mybatis_plus.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codel.mybatis_plus.pojo.Person;
import com.codel.mybatis_plus.service.impl.PersonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.List;
@Controller
public class PersonController {
@Autowired
PersonServiceImpl personService;
@GetMapping("/getperson")
public String getPerson(Model model) {
List<Person> personAll = personService.getPersonAll();
model.addAttribute("persons", personAll);
return "person/List";
}
// 分页
@GetMapping("/testPage")
public String page(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
//构造分页参数
Page<Person> page = new Page<>(pn, 2);
//调用page进行分页 获得分页信息
Page<Person> personPage = personService.page(page, null);
// userPage.getRecords()
// userPage.getCurrent()
// userPage.getPages()
// 获得所有对象信息
List<Person> records = personPage.getRecords();
model.addAttribute("page", personPage);
model.addAttribute("persons", records);
return "person/List";
}
@GetMapping("/deleteUser/{id}")
public String deleteUser(@PathVariable("id") int id, Model model,
@RequestParam(value = "pn", defaultValue = "1") int pn,
RedirectAttributes redirectAttributes) {
boolean b = personService.removeById(id);
redirectAttributes.addAttribute("pn", pn);
return "redirect:/testPage";
}
}
看上面的controller可以知道,要使用分页,就要使用分页插件,下面进行分页的配置
可以从官网上面找到模板的
Mybatis-plus官网网址
package com.codel.mybatis_plus.myconfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisConfig {
// 最新版 分页插件 没有配置这个分页插件的是不可以进行分页的
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setMaxLimit(500L);
paginationInnerInterceptor.setOverflow(true);
interceptor.addInnerInterceptor(paginationInnerInterceptor);//这是分页拦截器
return interceptor;
}
}
第四步:写html前端页面
html页面
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>输出title>
head>
<body>
<div>
<table border="1" >
<tr>
<th>idth>
<th>nameth>
<th>ageth>
<th>emailth>
<th>操作th>
tr>
<tr th:each="person: ${persons}">
<td th:text="${person.id}">td>
<td th:text="${person.name}">td>
<td th:text="${person.age}">td>
<td th:text="${person.email}">td>
<td>
<a th:href="@{/deleteUser/{id}(id=${person.id},pn=${page.current})}">删除a>
td>
tr>
table>
<div>
当前第[[${page.current}]]页 总计 [[${page.pages}]]页 共[[${page.total}]]条记录
div>
<div>
<ul>
<li><a href="#">← 前一页a>li>
<li th:class="${num == page.current?'active':''}" th:each="num:${#numbers.sequence(1,page.pages)}">
<a th:href="@{/testPage(pn=${num})}">[[${num}]]a>
li>
<li><a href="#">下一页 → a>li>
ul>
div>
div>
body>
html>