我们搭建一个springboot应用,我们在springboot应用中给它输出这个数据。输出的数据让前端进行加载。
这里我们要导入的依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.southwind</groupId>
<artifactId>layui01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>layui01</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
当我们使用到数据库表的时候第一反应首先是先有实体类
当我们有了实体类以后我们就要测验一下能不能调通。(这个时候我们就需要创建一个接口)
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/zc_db
username: root
password: root
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
我们需要在启动类里面加一个MapperScan
package com.southwind.layui01;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.southwind.layui01.mapper")
public class Layui01Application {
public static void main(String[] args) {
SpringApplication.run(Layui01Application.class, args);
}
}
package com.southwind.layui01.mapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ZcInfoMapperTest {
@Autowired
private ZcInfoMapper zcInfoMapper;
@Test
void Test(){
zcInfoMapper.selectList(null).forEach(System.out::println);
}
}
它会自动把数据库中的下划线转成驼峰
接口测通了我们就可以去写业务了(给前端返json数据)
前后端的对接接口一定是要对应的。
page是分页的功能
分页操作是会用到总数据量的
因为实体类的数据结构数据类型跟我们前端的数据要求不一致(基本相似但是有点出入),所以我们就需要根据实体类封装一套跟前端所吻合的结构。这个东西就叫vo(视图对象)
package com.southwind.layui01.vo;
import lombok.Data;
import java.util.List;
@Data
public class DataVo<T> {
private Integer code;
private String msg;
private Integer count;
private List<T> data;
}
接下来就开始封装我们的业务层了(service就是然后后端返回前端的数据)
package com.southwind.layui01.service;
import com.southwind.layui01.entity.ZcInfo;
import com.southwind.layui01.vo.DataVo;
import javax.xml.crypto.Data;
public interface ZcInfoService {
public DataVo<ZcInfo> findData();
}
通过id来查找到那一行的数据,然后获取name值。
当然我们不选中自己写也是ok的
package com.southwind.layui01.service.impl;
import com.southwind.layui01.entity.ZcInfo;
import com.southwind.layui01.mapper.ZcInfoMapper;
import com.southwind.layui01.service.ZcInfoService;
import com.southwind.layui01.vo.DataVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ZcInfoServiceImpl implements ZcInfoService {
@Autowired
private ZcInfoMapper zcInfoMapper;
@Override
public DataVo<ZcInfo> findData() {
DataVo dataVo=new DataVo();
dataVo.setCode(0);
dataVo.setMsg("");
//查询出表格中数据的数量
dataVo.setCount(zcInfoMapper.selectCount(null));
//把数据库里面的所有数据查出来
List<ZcInfo> zcInfoList=zcInfoMapper.selectList(null);
dataVo.setData(zcInfoList);
return dataVo;
}
}
package com.southwind.layui01.service;
import com.southwind.layui01.entity.ZcInfo;
import com.southwind.layui01.vo.DataVo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class ZcInfoServiceTest {
@Autowired
private ZcInfoService zcInfoService;
@Test
void findData() {
DataVo dataVo =zcInfoService.findData();
int i=0;
}
}
接口已经搞定了,接下来我们就是把这个接口对外开发了(让前端调用后端开发的接口就可以访问到后端的数据了)。
package com.southwind.layui01.controller;
import com.southwind.layui01.service.ZcInfoService;
import com.southwind.layui01.vo.DataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ZcInfoController {
@Autowired
private ZcInfoService zcInfoService;
@RequestMapping("/list")
public DataVo list(){
return zcInfoService.findData();
}
}
接下来就是让前端访问真实的后端数据
这个时候我们还是访问不到,因为涉及到了跨域问题。
如何解决跨域问题:https://blog.csdn.net/liulang68/article/details/108711063
接下来就是让前端的分页功能生效
所有我们后台需要做分页的功能
后端首先要把前端分页的参数接收到(控制层,接口层,实现类层都需要加上参数)
package com.southwind.layui01.controller;
import com.southwind.layui01.service.ZcInfoService;
import com.southwind.layui01.vo.DataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ZcInfoController {
@Autowired
private ZcInfoService zcInfoService;
@RequestMapping("/list")
public DataVo list(Integer page,Integer limit){
return zcInfoService.findData(page,limit);
}
}
package com.southwind.layui01.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.layui01.entity.ZcInfo;
import com.southwind.layui01.mapper.ZcInfoMapper;
import com.southwind.layui01.service.ZcInfoService;
import com.southwind.layui01.vo.DataVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ZcInfoServiceImpl implements ZcInfoService {
@Autowired
private ZcInfoMapper zcInfoMapper;
@Override
public DataVo<ZcInfo> findData(Integer page,Integer limit) {
DataVo dataVo=new DataVo();
dataVo.setCode(0);
dataVo.setMsg("");
IPage<ZcInfo> zcInfoIPage=new Page<>(page,limit);
IPage<ZcInfo> result=zcInfoMapper.selectPage(zcInfoIPage,null);//这里的null还是指的是查询所有的数据
dataVo.setCount(result.getTotal());
//把数据库里面的所有数据查出来
List<ZcInfo> zcInfoList=result.getRecords();
dataVo.setData(zcInfoList);
return dataVo;
}
}
虽然我们已经把分页的逻辑加上去了,但是我们还需要写一个拦截器,如果没有这个拦截器它是无法进行分页处理的。
package com.southwind.layui01.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
package com.southwind.layui01.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.layui01.entity.ZcInfo;
import com.southwind.layui01.mapper.ZcInfoMapper;
import com.southwind.layui01.service.ZcInfoService;
import com.southwind.layui01.vo.DataVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ZcInfoServiceImpl implements ZcInfoService {
@Autowired
private ZcInfoMapper zcInfoMapper;
@Override
public DataVo<ZcInfo> findData(Integer page,Integer limit) {
DataVo dataVo=new DataVo();
dataVo.setCode(0);
dataVo.setMsg("");
IPage<ZcInfo> zcInfoIPage=new Page<>(page,limit);
IPage<ZcInfo> result=zcInfoMapper.selectPage(zcInfoIPage,null);//这里的null还是指的是查询所有的数据
dataVo.setCount(result.getTotal());
//把数据库里面的所有数据查出来
List<ZcInfo> zcInfoList=result.getRecords();
dataVo.setData(zcInfoList);
return dataVo;
}
}