SpringBoot框架Day04之整合Mybatis-plus分页展示

Mybatis-plus

  1. 搭建数据库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]');
  1. 导包引入依赖 mybatis-plus-boot-starter,不需要mybatis依赖了
		<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
  1. 主程序上添加注解@MapperScan(“com.lwt/admin/mapper”)去默认扫描包下所有文件
@MapperScan("com.lwt/admin/mapper")
@SpringBootApplication
public class Springboot01ThymeleafAdminApplication {
    
    
  1. 创建实体类和实体类对应的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)

猜你喜欢

转载自blog.csdn.net/m0_47119598/article/details/113460964