利用aop技术实现分页

1 连接数据库

第一步

连接数据库,需加入pom.xml

<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.3.0</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.2.9</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

数据库配置spring.factories

  线程池中核心参数有哪些?

  • 1.初始化连接数
  • 2.最小连接数
  • 3.最大连接数
  • 4.最大空闲等待时间

第二步

dao

Data Access Object 数据访问对象

底层可以换任何一个主键

  所以需要写student类(在moder中) 

package cn.zxy.moder;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Student {
    private Integer id;
    private String name;
    private String age;
}

 然后写select方法(在resources中建立mapper)

StudentMapper.xml

第三步

Studentcontroller 类在controller文件中写

package cn.zxy.controller;

import cn.zxy.dao.StudentDao;
import cn.zxy.moder.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("api/student")
public class StudentController {
    @Autowired
    private StudentDao studentDao;
    
    @GetMapping
    public List<Student> select(){
        return studentDao.select();
    }
    
}

注入StudentDao 

@Autowired
    private StudentDao studentDao;

第四步

 启动类

package cn.zxy;



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

@SpringBootApplication
@MapperScan
public class Application {

	public static void main(String[] args) {

		SpringApplication.run(Application.class, args);



	}

}

第五步

GET做测试http://localhost:8080/api/student/

 2 开始做分页

第一步

导入pom.xml

设置参数,测试 如果成功就可以传不固定值

 

 但是,这种方法需要每次都传入对应的参数,不方便

第二步

在StudentController中 select方法里面写入代码

 是否分页都支持

第三步

测试接口通用,万能接口,以不变应万变

配置日志

分页

 不分页

第四步

分页想要总条数

Page

第五步

在advice中 MyResponseAdvice类中需要加入

判断body是否在Page这个类中

如果是,将body强转为Page

第六步

利用hutool工具

3  利用Product再次验证

第一步

第二步

第三步

连接数据库 表名是202_product

第四步

 

4 提取公共分页 

第一步

 第二步

 ProductDao也是一样,加入@PageX

第三步

加入pom.xml

 

 第四步

第五步

使用的时候只需要加入@PageX即可用了

扩展 更加优化

 

5 记录接口耗时

只有在核心代码中才会使用接口耗时

第一步

 第二步

记录学生耗时

  第三步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值