Mybatis-plus

  1. Mapper 接口:
    定义一个 Mapper 接口,继承自 MyBatis-Plus 的 BaseMapper 接口,这样你就可以使用 MyBatis-Plus 提供的各种方法了。

    java复制代码
      package bjdx.example.mapper;
       
      import bjdx.example.entity.User;
      import com.baomidou.mybatisplus.core.mapper.BaseMapper;
       
      public interface UserMapper extends BaseMapper<User> {
      // 这里可以定义你自己的方法,如果需要的话
      }
  2. Service 层:
    创建一个 Service 层来封装业务逻辑,并通过 Spring 的 @Autowired 注解注入 Mapper 接口。

    java复制代码
      package bjdx.example.service;
       
      import bjdx.example.entity.User;
      import bjdx.example.mapper.UserMapper;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
       
      @Service
      public class UserService {
       
      @Autowired
      private UserMapper userMapper;
       
      public User getUserById(Long id) {
      return userMapper.selectById(id);
      }
       
      // 其他业务方法...
      }
  3. Controller 层:
    创建一个 Controller 层来处理 HTTP 请求,并调用 Service 层的方法。

    java复制代码
      package bjdx.example.controller;
       
      import bjdx.example.entity.User;
      import bjdx.example.service.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RestController;
       
      @RestController
      public class UserController {
       
      @Autowired
      private UserService userService;
       
      @GetMapping("/users/{id}")
      public User getUserById(@PathVariable Long id) {
      return userService.getUserById(id);
      }
       
      // 其他控制器方法...
      }
posted @ 2024-07-08 11:46  不如赐场梦给我  阅读(5)  评论(0编辑  收藏  举报