SpringBoot框架Day04之整合Mybatis-plus分頁展示
阿新 • • 發佈:2021-02-01
技術標籤:springboot資料庫
Mybatis-plus
- 搭建資料庫user表
DROP TABLE IF EXISTS USER;
CREATE TABLE USER
(
id BIGINT(20) NOT NULL COMMENT '主鍵ID',
NAME VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年齡',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '郵箱',
PRIMARY KEY (id)
);
DELETE FROM USER;
INSERT INTO USER (id, NAME, age, email) VALUES
(1, 'Jone', 18, ' [email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
- 導包引入依賴 mybatis-plus-boot-starter,不需要mybatis依賴了
<dependency>
<groupId>com.baomidou</ groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
- 主程式上添加註解@MapperScan(“com.lwt/admin/mapper”)去預設掃描包下所有檔案
@MapperScan("com.lwt/admin/mapper")
@SpringBootApplication
public class Springboot01ThymeleafAdminApplication {
- 建立實體類和實體類對應的Mapper介面,繼承BaseMapper<實體類>擁有CRUD能力。
@Test
void testUserMapper() {
User user = userMapper.selectById(1L);
log.info("使用者資訊:"+user);
}
注意點
@TableField(exist = false) 表中不存在的屬性需要額外標識
@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {
@TableField(exist = false)
private String username;
@TableField(exist = false)
private String password;
// 以下資料庫
private Long id;
private String name;
private Integer age;
private String email;
}
不能存在UserMapper.xml檔案,否則報錯。
Mybatis-plus分頁資料展示
mybatis分頁外掛,官網https://baomidou.com/guide/page.html
在config包下新建MybatisPlusConfig配置類
@Configuration
public class MyBatisPlusConfig {
//Spring boot方式
@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); // 再次點選回首頁
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
UserMapper
public interface UserMapper extends BaseMapper<User> {
}
UserService
public interface UserService extends IService<User> {
}
UserServiceImpl實現類
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
Controller
@GetMapping("/dynamic_table")
private String dynamic_table(@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
Model model){
// 從資料庫中查出user表中使用者
List<User> list = userService.list();
// model.addAttribute("users",list);
Page<User> userPage = new Page<>(pageNo, 2);
Page<User> page = userService.page(userPage, null);// mybatis-plus分頁
System.out.println("總記錄數"+page.getTotal());
model.addAttribute("page",page);
return "/table/dynamic_table";
}
dynamic_table.html
關鍵點1:
<`tr class=“gradeA” th:each=“user,stats:${page.records}”>
關鍵點2:
<div class="dataTables_info" id="dynamic-table_info">當前第 [[${page.current}]] 頁 總計 [[${page.pages}]] 頁 共 [[${page.total}]] 條記錄
關鍵點3:
<ul>
<li class="prev disabled"><a href="#">← Previous</a></li>
<li th:class="${num == page.current?'active':''}" th:each="num:${#numbers.sequence(1,page.pages)}"><a th:href="@{/dynamic_table(pageNo=${num})}">[[${num}]]</a></li>
<li class="next disabled"><a href="#">Next → </a></li>
</ul>
<!--body wrapper start-->
<div class="wrapper">
<div class="row">
<div class="col-sm-12">
<section class="panel">
<header class="panel-heading">
Dynamic Table
<span class="tools pull-right">
<a href="javascript:;" class="fa fa-chevron-down"></a>
<a href="javascript:;" class="fa fa-times"></a>
</span>
</header>
<div class="panel-body">
<div class="adv-table">
<table class="display table table-bordered table-striped" id="dynamic-table">
<thead>
<tr>
<th>#</th>
<th>表中id</th>
<th>使用者name</th>
<th>年齡age</th>
<th>郵箱email</th>
</tr>
</thead>
<tbody>
<tr class="gradeA" th:each="user,stats:${page.records}">
<td th:text="${stats.count}">123</td>
<td th:text="${user.id}">1234</td>
<td th:text="${user.name}">無所謂</td>
<td th:text="${user.age}">年齡</td>
<!-- <td th:text="${user.password}">Win 95+ / OSX.1+</td>-->
<td>[[${user.email}]]</td>
</tr>
</tbody>
</table>
<div class="row-fluid">
<div class="span6">
<div class="dataTables_info" id="dynamic-table_info">當前第 [[${page.current}]] 頁 總計 [[${page.pages}]] 頁 共 [[${page.total}]] 條記錄
</div>
</div>
<div class="span6">
<div class="dataTables_paginate paging_bootstrap pagination">
<ul>
<li class="prev disabled"><a href="#">← Previous</a></li>
<li th:class="${num == page.current?'active':''}" th:each="num:${#numbers.sequence(1,page.pages)}"><a th:href="@{/dynamic_table(pageNo=${num})}">[[${num}]]</a></li>
<li class="next disabled"><a href="#">Next → </a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<!--body wrapper end-->
整合Redis
定義:
Redis 是一個開源(BSD許可)的,記憶體中的資料結構儲存系統,它可以用作資料庫、快取和訊息中介軟體。 它支援多種型別的資料結構,如 字串(strings), 雜湊(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 與範圍查詢, bitmaps, hyperloglogs 和 地理空間(geospatial) 索引半徑查詢。 Redis 內建了 複製(replication),LUA指令碼(Lua scripting), LRU驅動事件(LRU eviction),事務(transactions) 和不同級別的 磁碟持久化(persistence), 並通過 Redis哨兵(Sentinel)和自動 分割槽(Cluster)提供高可用性(high availability)