目录
一、Spring Boot项目中的层级结构
一个完整的Spring Boot项目主要包括Controller层、Service层、Mapper层和Entity层组成。
二、各层详解
(一)Controller层(控制层)
Controller层主要负责接收前端请求并返回响应,它是应用程序的入口点。在Spring Boot中,Controller层通常使用@RestController或@Controller注解来标识。Controller层的主要作用是接收客户端请求,将请求参数传递给Service层进行处理,并将处理结果以适当的形式返回给客户端。
UserController类
package com.itxinian.controller;
import com.itxinian.entity.Result;
import com.itxinian.entity.User;
import com.itxinian.service.UserService;
import com.itxinian.utils.JwtUtil;
import com.itxinian.utils.Md5Util;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public Result register(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$") String password) {
User u = userService.findByUserName(username);
if(u == null){
userService.register(username,password);
return Result.success();
}else{
return Result.error("用户名已被使用");
}
}
@PostMapping("/login")
public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$")String password){
User loginUser = userService.findByUserName(username);
if(loginUser == null){
return Result.error("用户名错误");
}
if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){
Map<String,Object> claims = new HashMap<>();
claims.put("id",loginUser.getId());
claims.put("username",loginUser.getUsername());
String token = JwtUtil.genToken(claims);
return Result.success(token);
}
return Result.error("密码错误");
}
}
(二)Service层(业务逻辑层)
Service层通常位于Controller层和数据访问层之间,负责处理应用程序的业务逻辑。Service层负责执行业务规则、数据验证、数据处理等操作。它接收来自Controller层的请求,调用Mapper层的方法进行数据操作,并将结果返回给Controller层。Service层的设计使得业务逻辑与数据访问逻辑分离,提高了代码的可维护性和可重用性。
UserService接口,主要用于定义接口(业务逻辑)
package com.itxinian.service;
import com.itxinian.entity.User;
public interface UserService {
User findByUserName(String username);
void register(String username, String password);
}
UserServiceImpl类,主要用于实现UserService接口
package com.itxinian.service.impl;
import com.itxinian.mapper.UserMapper;
import com.itxinian.entity.User;
import com.itxinian.service.UserService;
import com.itxinian.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Userserviceimpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findByUserName(String username) {
User u = userMapper.findByUserName(username);
return u;
}
@Override
public void register(String username, String password) {
String md5String = Md5Util.getMD5String(password);
userMapper.add(username,md5String);
}
}
(三)Mapper层(数据访问层)
Mapper层也称为Dao层或Repository层,它是数据持久层的组件。Mapper层的主要作用是访问数据库,执行数据的增删改查操作。它通常包含一些基本的SQL语句或使用ORM框架提供的API来执行数据库操作。Mapper层的设计使得数据访问逻辑与业务逻辑分离,提高了代码的可维护性和可重用性。
UserMapper接口
package com.itxinian.mapper;
import com.itxinian.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UserMapper {
@Select("select * from user where username=#{username}")
User findByUserName(String username);
@Insert("insert into user(username,password,create_time,update_time)" +
" values(#{username},#{password},now(),now())")
void add(String username, String password);
}
(四)Entity层(实体层)
Entity层是存放实体的类,类中定义了多个属性,并且与数据库表的字段保持一致。Entity层的定义主要用于定义与数据库对象对应的属性,提供get/set方法、toString方法以及有参无参构造函数等。一个数据库表通常对应了一个Entity类,使得开发人员可以更加方便地操作数据库表中的数据。
User类
package com.itxinian.entity;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class User {
private Integer id;//主键ID
private String username;//用户名
private String password;//密码
private String nickname;//昵称
private String email;//邮箱
private String userPic;//用户头像地址
private LocalDateTime createTime;//创建时间
private LocalDateTime updateTime;//更新时间
}