【生日快乐】SpringBoot SpringBoot 基础篇(第一篇) 第4章 SpringBoot 综合案例 4.5 查询客户
创始人
2024-04-03 22:03:16

SpringBoot

【千锋教育java教程SpringBoot2全套,springboot快速入门到项目实战视频教程】

SpringBoot 基础篇(第一篇)

第4章 SpringBoot 综合案例

文章目录

      • SpringBoot
      • SpringBoot 基础篇(第一篇)
      • 第4章 SpringBoot 综合案例
        • 4.5 查询客户
          • 4.5.1 查询客户接口设计
          • 4.5.2 查询后台代码实现

4.5 查询客户

4.5.1 查询客户接口设计

在这里插入图片描述

请求地址:http://localhost/customer

  • 请求方式:GET

  • 请求参数

    参数名参数说明备注
    Customer客户视图对象封装客户前台展示信息
  • 响应数据

    参数名参数说明备注
    DataGridView数据对象前台展示数据对象
4.5.2 查询后台代码实现

【1】封装layui数据表格的数据对象DataGridView

package com.dingjiaxiong.vo;/*** ClassName: DataGridView* date: 2022/10/14 21:09* 前台做数据展示* @author DingJiaxiong*/public class DataGridView {/** 封装数据表格的数据对象* */private Integer code = 0;private String msg = "";private Long count;private Object data;public DataGridView() {}public DataGridView(Object data) {super();this.data = data;}public DataGridView(Long count, Object data) {super();this.count = count;this.data = data;}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public Long getCount() {return count;}public void setCount(Long count) {this.count = count;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}
}

说实话,没看明白这代码的意义

【2】封装客户实体对象Customer

package com.dingjiaxiong.domain;import com.fasterxml.jackson.annotation.JsonFormat;import java.util.Date;/*** ClassName: Customer* date: 2022/10/14 21:12* 客户实体** @author DingJiaxiong*/public class Customer {//身份证private String identity;//客户名称private String custname;//性别private Integer sex;//地址private String address;//电话private String phone;//职业private String career;//创建时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date createtime;public String getIdentity() {return identity;}public void setIdentity(String identity) {//this.identity = identity;this.identity = identity == null ? null : identity.trim();}public String getCustname() {return custname;}public void setCustname(String custname) {//this.custname = custname;this.custname = custname == null ? null : custname.trim();}public Integer getSex() {return sex;}public void setSex(Integer sex) {this.sex = sex;}public String getAddress() {return address;}public void setAddress(String address) {//this.address = address;this.address = address == null ? null : address.trim();}public String getPhone() {return phone;}public void setPhone(String phone) {// this.phone = phone;this.phone = phone == null ? null : phone.trim();}public String getCareer() {return career;}public void setCareer(String career) {// this.career = career;this.career = career == null ? null : career.trim();}public Date getCreatetime() {return createtime;}public void setCreatetime(Date createtime) {this.createtime = createtime;}@Overridepublic String toString() {return "Customer{" +"identity='" + identity + '\'' +", custname='" + custname + '\'' +", sex=" + sex +", address='" + address + '\'' +", phone='" + phone + '\'' +", career='" + career + '\'' +", createtime=" + createtime +'}';}}

【3】封装客户视图对象CustomerVo

package com.dingjiaxiong.vo;import com.dingjiaxiong.domain.Customer;/*** ClassName: CustomerVo* date: 2022/10/14 21:14* 客户的视图对象* @author DingJiaxiong*/public class CustomerVo extends Customer {/*** 分页参数*/private Integer page;  //当前页private Integer limit; //每页展示多少条public Integer getPage() {return page;}public void setPage(Integer page) {this.page = page;}public Integer getLimit() {return limit;}public void setLimit(Integer limit) {this.limit = limit;}}

【5】CustomerMapper接⼝

package com.dingjiaxiong.mapper;import com.dingjiaxiong.domain.Customer;import java.util.List;/*** ClassName: CustomerMapper* date: 2022/10/14 21:17** @author DingJiaxiong*/@Mapper
public interface CustomerMapper {/** 查询* */List queryAllCustomer(Customer customer);}

【6】CustomerMapper.xml



identity, custname, sex, address, phone, career, createtime

【7】CustomerService接口

package com.dingjiaxiong.service;import com.dingjiaxiong.vo.CustomerVo;
import com.dingjiaxiong.vo.DataGridView;/*** ClassName: CustomerService* date: 2022/10/14 21:15** @author DingJiaxiong*/public interface CustomerService {/** 查询所有客户* */public DataGridView queryAllCustomer(CustomerVo customerVo);}

【8】CustomerServiceImpl 实现类

这里快速添加一个分页依赖

com.github.pagehelperpagehelper-spring-boot-starter1.3.1

在这里插入图片描述

package com.dingjiaxiong.service.impl;import com.dingjiaxiong.domain.Customer;
import com.dingjiaxiong.mapper.CustomerMapper;
import com.dingjiaxiong.service.CustomerService;
import com.dingjiaxiong.vo.CustomerVo;
import com.dingjiaxiong.vo.DataGridView;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** ClassName: CustomerServiceImpl* date: 2022/10/14 21:25** @author DingJiaxiong*/@Service
public class CustomerServiceImpl implements CustomerService {@Autowiredprivate CustomerMapper customerMapper;@Overridepublic DataGridView queryAllCustomer(CustomerVo customerVo) {Page page = PageHelper.startPage(customerVo.getPage(), customerVo.getLimit());List data = this.customerMapper.queryAllCustomer(customerVo);return new DataGridView(page.getTotal(), data);}
}
 

【9】编写控制器

package com.dingjiaxiong.controller;import com.dingjiaxiong.service.CustomerService;
import com.dingjiaxiong.vo.CustomerVo;
import com.dingjiaxiong.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** ClassName: CustomerController* date: 2022/10/14 21:30** @author DingJiaxiong*/@RestController
@RequestMapping("/customer")
public class CustomerController {@Autowiredprivate CustomerService customerService;/** 查询客户信息* */@GetMappingpublic DataGridView loadAllCustomer(@RequestBody CustomerVo customerVo){return customerService.queryAllCustomer(customerVo);}}

给启动类加上扫描

在这里插入图片描述

启动服务器

在这里插入图片描述

报了一个循环依赖的错误

【解决】

在application.yml 中配置

main:allow-circular-references: true

在这里插入图片描述

OK。

再次启动

在这里插入图片描述

OK,启动成功。

测试一下接口:

在这里插入图片描述

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...