Mybatis-plus
-
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> { // 这里可以定义你自己的方法,如果需要的话 } -
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; public class UserService { private UserMapper userMapper; public User getUserById(Long id) { return userMapper.selectById(id); } // 其他业务方法... } -
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; public class UserController { private UserService userService; public User getUserById( return userService.getUserById(id); } // 其他控制器方法... }