SpringBoot——MybatisPlus整合实现数据表的查询分页功能和删除功能

1.pom.xml

        <!--  数据库连接 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--  引入静态模板引擎,才能访问templates下面的静态页面  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!-- web开发 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!-- 配置文件提示 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 日志、getset方法等 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--  mybatisplus整合连接数据库,包含了jdbc包(和mybatis整合一样都包含)-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>

2.application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/graduationproject
    username: root
    password: 113846
    driver-class-name: com.mysql.jdbc.Driver        #5.x版本mysql

3.bean

@AllArgsConstructor
@NoArgsConstructor
@Data
@TableName("user")  //指定数据库表名字
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

@AllArgsConstructor:lombok包实现的有参构造器

@NoArgsConstructor:springboot启动后对bean生成无参构造器

@Data:生成get()、set()、toString()方法

@TableName:指定数据库中的表名

4.mapper

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

@Mapper:标识该类为bean,当springboot启动后自动注入组件到ioc容器,后续在服务实现层能通过@Autowired注解注入该bean,实现数据库表的增删查改等操作。

BaseMapper<User>:mybatisplus的jar包中的接口类,源码中Mapper继承该接口后,无需编写mapper.xml文件,即可获得CURD功能。并<T>为该Mapper支持泛型,在本次BaseMapper测试中泛型为User。

5.service和serviceImpl

@Service
public interface UserService extends IService<User> {
}

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

}

ServiceImpl:源码中ServiceImpl<M extends BaseMapper<T>, T> implements IService<T>,其中IService 实现类( 泛型:M 是 mapper 对象,T 是泛型实体 )。

6.controller

(1).查询分页功能

@Slf4j
@Controller
public class PagingAndCurdTestController {
    @Autowired
    UserService userService;

    //RequestParam:请求参数
    @RequestMapping("/dynamic_table")
    public String getDynamicTable(@RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                  Model model){
        //查询该表所有对象
        List<User> list = userService.list();
//        model.addAttribute("lists",list);
        //分页查询数据,pn第几页,2 每页显示记录数
        Page<User> userPage = new Page<>(pn,2);
        //分页查询结果
        Page<User> page = userService.page(userPage,null);
        model.addAttribute("pages",page);
        return "table/dynamic_table";
    }
}

(2).删除功能

    //PathVariable:路径参数
    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Long id,
                             @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                             RedirectAttributes redirectAttributes){
        log.info("删除的用户id={}",id);
        userService.removeById(id);
        //重定向 ,携带参数
        redirectAttributes.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }

7.resoutces\templates\table\dynamic_table.html

        <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>Rendering engine</th>
            <th>id</th>
            <th>Browser</th>
            <th>Platform(s)</th>
            <th class="hidden-phone">Engine version</th>
            <th class="hidden-phone">CSS grade</th>
        </tr>
        </thead>
        <tbody>


        <tr class="gradeX" th:each="users,stat:${pages.records}">
            <td th:text="${stat.count}">Trident</td>
            <td th:text="${users.id}">id</td>
            <td th:text="${users.name}">Internet
                Explorer 4.0
            </td>
            <td th:text="${users.age}">Win 95+</td>
            <td class="center hidden-phone">[[${users.email}]]</td>
            <td>
                <a th:href="@{/user/delete/{id}(id=${users.id},pn=${pages.current})}" class="btn btn-danger btn-sm" type="button">删除</a>
            </td>
        </tr>

        </tbody>

        </table>
            <div class="row-fluid"><div class="span6">
                <div class="dataTables_info" id="dynamic-table_info">
                    当前第[[${pages.current}]]页,总计[[${pages.pages}]]页,共[[${pages.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==pages.current?'active':''}" th:each="num:${#numbers.sequence(1,pages.pages)}">
                                <a th:href="@{/dynamic_table(pn=${num})}">[[${num}]]</a>
                            </li>
                            <li class="next disabled">
                                <a href="#">Next → </a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        </div>
        </section>

(1).

th:each:循环对象集合

stat:thymeleaf的内置变量stat,可以获取对象集合的index、count、first、last等属性值

pages.records:在分页类Page<T>的源码中records为对象集合

th:text:获取Model中存储的文本值

th:href:路径跳转

(2).

[[${pages.current}]]:标签外写法,获取分页类Page<T>的源码中的current为当前页数的值

[[${pages.pages}]]:标签外写法,获取分页类Page<T>的源码中的pages为数据集合全部页数的值

[[${pages.total}]]:标签外写法,获取分页类Page<T>的源码中的total为全部对象条数的值

th:class="${num==pages.current?'active':''}  :在li标签封装中遍历显示页数值,如果num值等于当前页数current,高亮显示

8.web页面展示

http://localhost:8080/dynamic_table

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot整合MybatisPlus可以很方便地实现分页查询,而MybatisPlus本身也提供了物理分页功能。下面是实现步骤: 1.在pom.xml文件中添加MybatisPlus和分页插件的依赖。 2.在application.yml文件中配置分页插件。 3.在Mapper接口中添加分页查询方法,使用MybatisPlus提供的Page对象进行分页查询。 4.在Service层中调用Mapper接口中的分页查询方法,将查询结果封装到Page对象中返回给Controller层。 5.在Controller层中接收前端传来的分页参数,调用Service层中的分页查询方法,将查询结果返回给前端。 具体实现代码可以参考以下示例: 1.在pom.xml文件中添加MybatisPlus和分页插件的依赖: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> ``` 2.在application.yml文件中配置分页插件: ``` mybatis-plus: configuration: # 开启驼峰命名转换 map-underscore-to-camel-case: true # 配置分页插件 plugins: - com.github.pagehelper.PageInterceptor # 配置分页插件的参数 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql ``` 3.在Mapper接口中添加分页查询方法,使用MybatisPlus提供的Page对象进行分页查询: ``` public interface UserMapper extends BaseMapper<User> { /** * 分页查询用户列表 * @param page 分页参数 * @return 用户列表 */ List<User> selectUserList(Page<User> page); } ``` 4.在Service层中调用Mapper接口中的分页查询方法,将查询结果封装到Page对象中返回给Controller层: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public Page<User> getUserList(Page<User> page) { return userMapper.selectUserList(page); } } ``` 5.在Controller层中接收前端传来的分页参数,调用Service层中的分页查询方法,将查询结果返回给前端: ``` @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") public Result getUserList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { Page<User> page = new Page<>(pageNum, pageSize); Page<User> userList = userService.getUserList(page); return Result.success(userList); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值