需求分析:
具体返回如下JSON格式数据
含有四个属性列:id 和 username 和photo 和followerCount
第一步:
首先按照下面文章创建一个模板项目
SpingBoot——SB整合MB的web项目模板_北岭山脚鼠鼠的博客-CSDN博客
第二步:
使用如下的建表语句在一个数据库中新建一个用户表
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`username` char(20) DEFAULT NULL,
`photo` varchar(200) DEFAULT NULL,
`followerCount` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
并在pojo层中新建一个与之对应的实体类Users
package com.example.backbound.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Users {
private int id;
private String username;
private String photo;
private int followerCount;
}
第三步:
解决跨域访问的问题:
新建一个config包,在里面新建一个CorsConfig类,填充以下内容,然后在前后端联调时就不会出现跨域访问报错了。
package com.example.backbound.config;
import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Configuration
public class CorsConfig implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
String origin = request.getHeader("Origin");
if(origin!=null) {
response.setHeader("Access-Control-Allow-Origin", origin);
}
String headers = request.getHeader("Access-Control-Request-Headers");
if(headers!=null) {
response.setHeader("Access-Control-Allow-Headers", headers);
response.setHeader("Access-Control-Expose-Headers", headers);
}
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
}
第四步:
准备三层架构各个类
controller层:
新建一个userlistcontroller
package com.example.backbound.controller.userlist;
import com.example.backbound.pojo.Users;
import com.example.backbound.service.userlist.impl.userlistservice;
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("/userlist/")
public class userlistcontroller {
@Autowired
private userlistservice userlistservice;
@GetMapping
public List<Users> getuserlist(){
List<Users> usersList=userlistservice.getuserlist();
return usersList;
}
}
service层:
先新建一个userlistservice接口
package com.example.backbound.service.userlist;
import com.example.backbound.pojo.Users;
import java.util.List;
public interface userlist {
public List<Users> getuserlist();
}
在新建一个该接口的实现类
package com.example.backbound.service.userlist.impl;
import com.example.backbound.mapper.userlist.UserlistMapper;
import com.example.backbound.pojo.Users;
import com.example.backbound.service.userlist.userlist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class userlistservice implements userlist {
@Autowired
private UserlistMapper userlistMapper;
@Override
public List<Users> getuserlist() {
return userlistMapper.getuserlist();
}
}
Mapper层:
实现一个userlistMapper接口如下
package com.example.backbound.mapper.userlist;
import com.example.backbound.pojo.Users;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserlistMapper {
@Select("select * from users")
List<Users> getuserlist();
}
最后项目文件结构如下所示