The role and connection of the controller layer, service layer, mapper layer, and entity layer.

 

1. Controller layer

The controller layer is used to receive front-end data and return page request information, and 

the Controller layer is not allowed to directly operate the database! It is like a waiter, which table guests need to order, just call the waiter! 
Correspondingly, what kind of business needs to be completed by the outside world, call different services through the Controller. It should be remembered that the Controller is just a middleman or forwarder, and the business logic of the Service should not be exposed in the Controller, but the Service should be forwarded directly business processing results! 
The control layer is responsible for the business process control of specific modules, and needs to call the interface of the service logic design layer to control the business process. 
The controller performs business operations by receiving parameters from the front-end H5 or App, and then returns the processing results to the front-end.
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;
    
    @GetMapping("/select")
    public List<User> index(){
        List<User> all = userMapper.findAll();
        return all;
    }

    @Autowired
    private UserService userService;
    @PostMapping("/insert")
    public boolean save(@RequestBody User user){
        return userService.saveUser(user);
    }

    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id){
        return userMapper.deleteById(id);
    }

Two.servie layer

The service layer receives information from the controller layer for business processing and logical judgment. Service is used to process business logic, and will call the API of the mapper layer; 

the Service layer is the business logic layer, which performs complex business logic processing, assembles and processes the data found in multiple mapper layers, and then returns the results For Controller, therefore, under normal circumstances, a Controller may contain multiple Services, 
and a Service may contain multiple mappers. 
(Example) The controller layer is the restaurant manager, the service is the waiter, and the mapper layer is the kitchen warehouse. 
The business service layer provides interfaces for classes in the controller layer to call. Generally, the method written by oneself is encapsulated, that is, it is declared, and the specific implementation is in serviceImpl.
public class UserService extends ServiceImpl<UserMapper, User> {
        public boolean saveUser(User user) {
            if(user.getId() == null){
            return save(user);//mybatis-plus提供的方法,表示插入数据。
            }else{
            return updateById(user);
        }
}

Three.mapper layer

The mapper layer (data persistence layer, specially used to deal with the database). 
The mapper layer is used to interact with the database. If you want to access the database and operate it, you can only send sql statements to the database through the mapper layer, pass these results to the service layer through the interface, and perform data persistence operations on the database. His method statement is directly For database operations, 
it mainly implements some addition, deletion, modification and query operations. In mybatis, the methods are mainly mapped with each other in xxx.xml.
@Mapper
@Repository
public interface UserMapper extends BaseMapper<User> {//数据库查询接口,专门用来跟数据库交互用的
    @Select("SELECT * from sys_user")
    public List<User> findAll();

    List<User> findAll1();
    @Insert("INSERT into sys_user(username,password,nickname,email,phone,address)VALUES(#{username},#{password},#{nickname}," +
            "#{email},#{phone},#{address});")
    public int insert(User user);

    public int updateUser(User user);

    @Delete("delete from sys_user where id = #{id}")
    public Integer deleteById(@Param("id") Integer id);

Four. entity layer

The entity layer creates entity classes, which correspond one-to-one with the attribute values ​​in the database table. 
The entity layer is used to store our entity classes, which are basically consistent with the attribute values ​​in the database, and implement set and get methods or use annotations.
@Data//Data注解代替了get和set方法
@TableName(value = "sys_user")
public class User {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String username;
    private String password;
    private String nickname;
    private String email;
    private String phone;
    private String address;
}

Guess you like

Origin blog.csdn.net/qq_45139808/article/details/125870976