Bootstrap

Spring Boot (maven)分页3.0版本 通用版

前言:

通过实践而发现真理,又通过实践而证实真理和发展真理。从感性认识而能动地发展到理性认识,又从理性认识而能动地指导革命实践,改造主观世界和客观世界。实践、认识、再实践、再认识,这种形式,循环往复以至无穷,而实践和认识之每一循环的内容,都比较地进到了高一级的程度。

正片

我们目前的分页,还是静态的,只能根据方法中的参数进行查询,3.0版本将向动态发展

在Spring Security的动态认证时,他们提供了一个方法可以事实拿去表单用户名,这里我们有办法实时获取参数吗?

答案是有的——通过Spring Web框架,实时获取参数

在这里,我们先照着上一个版本的分页写完

XML版本 3.0

第一步:增加实体类(连接数据库与服务端的桥梁)

第二步:增加Mapper接口

第三步:增加XML文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="具体mapper层路径,精确到文件名">
​
</mapper>

tip:Mapper和XML保证路径相同,文件名相同

第四步:增加Service类

第一步:创建一个接口类 interface 接口名

第二步:创建一个实现类 实现类名 implements 接口名

第五步:增加Controller类(返回经处理过的数据)

第六步:增加Configuration类

@Configuration
@MapperScan(basePackages = "mapper层")
public class ApplicationConfig {
​
}

第七步:测试

2.0中,我们完成了第三步就去验证了,这次我们先补全模板

package org.example.mybatis.servlet;

import org.example.mybatis.Mapper.TestMapper;

import java.util.List;

public interface TestServlet {
    List<TestMapper>  SelectLimit(int into, int Max);    //相对于复制了一遍TestMapper

}

实现接口

package org.example.mybatis.servlet.Imp;

import jakarta.annotation.Resource;
import org.example.mybatis.Mapper.TestMapper;
import org.example.mybatis.servlet.TestServlet;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TestServletImp implements TestServlet {

    @Resource
    private TestMapper testMapper;

    @Override
    public List<TestMapper> SelectLimit(int into, int Max) {
        return  testMapper.SelectLimit(into, Max);

    }
}

具体业务逻辑写这

第五步:controller层

package org.example.mybatis.controller;

import org.example.mybatis.Mapper.TestMapper;
import org.example.mybatis.servlet.TestServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class test {

    @Autowired
    TestServlet testServlet;

    @GetMapping("test")
    public String SelectLimit(){
        List<TestMapper> testMappers = testServlet.SelectLimit(0, 4);
        return "打印成功" + testMappers;
    }
}

第六步:添加配置

package org.example.mybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan(basePackages = "org.example.mybatis.Mapper")
public class MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisApplication.class, args);
    }
}

这个注解当我们只有一个mapper的时候可以不需要添加,多个的时候一定要加,因为他可以自动扫描,省去你挨个添加@Mapper的时间

第七步测试

现在首要的目的是将参数改为从前台获取,这里可以使用MVC的一个机制

@RestController
public class test {

    @Autowired
    TestServlet testServlet;

    @GetMapping("test")
    public String SelectLimit(int into,int Max){
        List<TestMapper> testMappers = testServlet.SelectLimit(into,Max);
        return "打印成功" + testMappers;
    }
}

添加参数,调用参数

动态的分页,也就是在控制层中添加参数

;