com.example.plus2.Bean.User
package com.example.plus2.Bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
com.example.plus2.config.MyBatisConfig
package com.example.plus2.config;
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 {
/**
* MybatisPlusInterceptor
* @return
*/
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join
//这是分页拦截器
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(500L);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
com.example.plus2.Mapper.UserMapper
package com.example.plus2.Mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.plus2.Bean.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
com.example.plus2.service.impl.UserServiceImpl
package com.example.plus2.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.plus2.Bean.User;
import com.example.plus2.Mapper.UserMapper;
import com.example.plus2.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
com.example.plus2.service.UserService
package com.example.plus2.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.plus2.Bean.User ;
public interface UserService extends IService<User> {
}
com.example.plus2.tt
package com.example.plus2;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.plus2.Bean.User;
import com.example.plus2.service.UserService;
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.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.util.List;
@Controller
public class tt {
@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:/ta";
}
@GetMapping("/ta")
public String dynamic_table(@RequestParam(value="pn",defaultValue = "1") Integer pn, Model model){
// List<User> users = Arrays.asList(new User("zhangsan", "123456"),
// new User("lisi", "123444"),
// new User("haha", "aaaaa"),
// new User("hehe ", "aaddd"));
//model.addAttribute("users",users);
List<User> list = userService.list();
//model.addAttribute("users",list);
Page<User> userPage = new Page<>(pn, 2);
//调用page进行分页
Page<User> page = userService.page(userPage, null);
// userPage.getRecords()
// userPage.getCurrent()
// userPage.getPages()
long current = page.getCurrent();
long pages = page.getPages();
long total = page.getTotal();
List<User> records = page.getRecords();
model.addAttribute("users",userPage);
model.addAttribute("page",page);
return "ta";
}
}
templates/ta.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
123
<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: ${page.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>
</thead>
</table>
<div class="span6">
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<!-- <li class="prev disabled"><a href="#">← 前一页</a></li>-->
<h1 th:class="${num == users.current?'active':''}" th:each="num:${#numbers.sequence(1,users.pages)}" >
<a th:href="@{/ta(pn=${num})}">[[${num}]]</a>
</h1>
<!-- <li class="next disabled"><a href="#">下一页 → </a></li>-->
</ul>
</div>
</div>
</body>
</html>