Spring Boot案例-员工分页查询

准备工作:

数据库的连接:


#驱动类名称
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#数据库连接的url
spring.datasource.url=jdbc:mysql://localhost:3306/tlias
#连接数据库的用户名
spring.datasource.username=root
#连接数据库的密码
spring.datasource.password=123456

#配置mybatis的日志, 指定输出到控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#开启mybatis的驼峰命名自动映射开关 a_column ------> aCloumn
mybatis.configuration.map-underscore-to-camel-case=true

PageBean: 返回给前端的格式限制

其他的都是那三层架构

Controller: 写的是前端发送过来的请求,然后对相应的请求做出对应的响应

Service: 逻辑的处理

Dao: 把对应的java语句转换为SQL语句,然后对数据库进行操作

Controller(对前端的请求做出响应):

package com.example.tliaswebmanagement.controller;

import com.example.tliaswebmanagement.pojo.PageBean;
import com.example.tliaswebmanagement.pojo.Result;
import com.example.tliaswebmanagement.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询,参数: {}, {}", page, pageSize);

       PageBean pageBean =  empService.page(page, pageSize);

        return Result.success(pageBean);
    }
}

代码分析:

@slf4j 就等同于下面这个代码,是日志的记录

private static Logger log = LoggerFactory.getLogger(DeptController.class);

 @RestController 会将方法的返回值返回给前端,返回的形式转换为JSON的形式

@Autowired 是 Spring Framework 提供的一种依赖注入(DI)方式,它可以自动装配一个 Spring 容器中已经注册的 bean 对象到需要使用这些对象的类中。通过 @Autowired 注解,Spring 会自动查找相应类型的 bean,然后将其注入到被注解的字段、方法或构造函数参数中。

@GetMapping(url) 这个注解是写在一个方法的上面的,用于处理 HTTP GET 请求,当浏览器的请求和这个url一样的时候,会执行这个注解下面的方法

@RequestParam 用于将HTTP请求中的参数绑定到方法的参数上。在Java Spring框架中,它可以用于处理HTTP GET请求的查询参数或者POST请求的表单数据, 它里面的 defaultValue属性是指定参数的默认值,当请求中没有该参数时,会使用默认值。

自动创建一个接口的实现类

 @Autowired
    private EmpService empService;

调用这个对象的相关方法,进行逻辑处理

 Service(逻辑处理):

接口:

package com.example.tliaswebmanagement.service;

import com.example.tliaswebmanagement.pojo.PageBean;
import org.apache.ibatis.annotations.Mapper;


public interface EmpService {

    PageBean page(Integer page, Integer pageSize);
}

实现:

package com.example.tliaswebmanagement.service.impl;

import com.example.tliaswebmanagement.mapper.EmpMapper;
import com.example.tliaswebmanagement.pojo.Emp;
import com.example.tliaswebmanagement.pojo.PageBean;
import com.example.tliaswebmanagement.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServicelmpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;


    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //获取总的记录数
        Long count = empMapper.count();

        //获取分页查询结果列表
        List<Emp> list = empMapper.page((page - 1) * pageSize, pageSize);

        //封装PageBean对象
        PageBean pageBean = new PageBean(count, list);
        return  pageBean;
    }
}

代码分析:

为什么去特意定义一个接口呢?解除耦合,为了后期的维护和管理

@Service 在Spring中,使用@Service注解表示这是一个服务层组件,也就是业务逻辑层的实现类。@Service注解放置在类上,表示这个类是一个Spring的Service,用于标注业务层组件。当我们需要在Controller中调用Service层方法时,可以使用@Autowired注解进行自动注入。

在这里面和dao结合起来,也是为了后期的方便 解除耦合

 @Autowired
    private EmpMapper empMapper;

Dao(操作数据库):

package com.example.tliaswebmanagement.mapper;

import com.example.tliaswebmanagement.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface EmpMapper {
    @Select("select count(*) from emp")
    public Long count();

    @Select("select * from emp limit #{start}, #{pageSize}")
    public List<Emp> page(Integer start, Integer pageSize);

}

@Mapper 在MyBatis框架中,@Mapper注解用于标识一个接口是MyBatis的Mapper接口。这个注解告诉MyBatis根据接口定义创建一个实现类,并将其注册为Mapper。这样你就可以通过该接口调用数据库操作方法

@Select("") 这个写在一个方法的前面,意思就是当你调用这个方法的时候 他会执行的SQL操作

 @Select("select count(*) from emp")
    public Long count();

运行:

在Postman里面进行访问

运行的结果:

  • 28
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FindYou.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值