1 在springboot中整合mybatis-plus
<!-- 引入mybatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 引入mysql驅動包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
<!-- 引入Druid依賴,阿里巴巴所提供的數據源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
2 在application.yml配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
3 在啟動類上面添加@MapperScan注解,掃描mapper包
@MapperScan("com.atguigu.admin.mapper")
4 新建User和UserMapper

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
@TableName("user")
public class User {
//非數據庫中的數據
@TableField(exist = false)
private String userName;
@TableField(exist = false)
private String password;
//數據庫中的數據
private int id;
private String name;
private String age;
private String email;
}
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fxz.admin.bean.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> { //mapper繼承父類獲取操作方法 泛型為數據庫數據對應的類 無需配置xml映射文件
}
5 Service
import com.baomidou.mybatisplus.extension.service.IService;
import com.fxz.admin.bean.User;
public interface UserService extends IService<User> {
}
//繼承父類 泛型為對應mapper和操作對象
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
}
6 分頁查詢攔截器
@Configuration
public class MybatisConfig { //mybatis分頁查詢必須配置分頁攔截器
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
}
7 前端控制器 Controller
@GetMapping(value = "/dynamic_table") //攔截請求
public String dynamic(Model model,@RequestParam(value = "pn",defaultValue = "1")int pn){
//分頁參數
Page<User> page = new Page(pn,2);
//調用page分頁
Page<User> users = userService.page(page);
model.addAttribute("users",users);
return "table/dynamic_table";
}
8 前端頁面展示數據
<tr class="gradeX" th:each="user: ${users.records}"> <!--這里按照取得的記錄數遍歷-->
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
<td th:text="${user.email}"></td>
</tr>
</div>
<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>