Spring Boot+mybatis项目切换sql为传参成无参

一、项目文件结构树

bash 复制代码
src/main/java/com/example/
├── controller
│   └── UserController.java
├── service
│   ├── UserService.java
│   └── impl
│       └── UserServiceImpl.java
├── mapper
│   └── UserMapper.java
└── entity
    └── User.java

src/main/resources/
├── mapper
│   └── UserMapper.xml
└── application.properties

二、改动前

  1. UserController.java
java 复制代码
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getUserById(id);
    }
}
  1. UserService.java
java 复制代码
public interface UserService {
    User getUserById(Long id);
}
  1. UserServiceImpl.java
java 复制代码
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Long id) {
        return userMapper.selectUserById(id);
    }
}
  1. UserMapper.java
java 复制代码
@Mapper
public interface UserMapper {
    User selectUserById(@Param("id") Long id);
}
  1. UserMapper.xml
xml 复制代码
<mapper namespace="com.example.mapper.UserMapper">
    <select id="selectUserById" resultType="com.example.entity.User">
        SELECT * FROM user WHERE id = #{id}
    </select>
</mapper>

三、修改后

  1. UserController.java
java 复制代码
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> getAllUsers() {          // 改为返回列表,不再接收路径参数
        return userService.getAllUsers();
    }
}
  1. UserService.java
java 复制代码
public interface UserService {
    List<User> getAllUsers();                  // 方法名和返回值都改变
}
  1. UserServiceImpl.java
java 复制代码
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUsers() {           // 实现无参方法
        return userMapper.selectAllUsers();     // 调用 Mapper 中的无参方法
    }
}
  1. UserMapper.java
java 复制代码
@Mapper
public interface UserMapper {
    List<User> selectAllUsers();                // 方法签名去掉参数,返回列表
}
  1. UserMapper.xml
xml 复制代码
<mapper namespace="com.example.mapper.UserMapper">
    <select id="selectAllUsers" resultType="com.example.entity.User">
        SELECT * FROM user                     <!-- SQL 中去掉了 WHERE 条件 -->
    </select>
</mapper>

改的不对的话容易报错NullPointerException

一定仔细检查XML及对应的mapper文件等

相关推荐
wangqiaowq5 小时前
SQL server C盘空间占满的处理
数据库·sql
文人sec5 小时前
SQL基础入门笔记 | 从零基础到掌握核心查询语句
数据库·笔记·sql
IT研究室6 小时前
最新计算机毕业设计选题推荐-基于spring boot的葡萄酒文化宣传网站-网站-文档指导-Java-springboot
java·spring boot·课程设计
YangYang9YangYan7 小时前
2027供应链计划校招:数学与应用数学专业如何把预测模型讲成业务决策?
人工智能·sql·算法
凤山老林8 小时前
Spring Boot 集成 Sa-Token 实现高可用权限认证:登录鉴权、动态路由权限与接口防重放实战
spring boot·后端·sa-token·bootstrap
171320330计算机毕设编程9 小时前
计算机毕业设计 基于YOLO改进算法的车辆特征识别系统设计与实现--lw
java·大数据·python·sql·算法
Wx-bishekaifayuan9 小时前
springboot桂味美食分享系统40540-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
墨神谕11 小时前
RabbitMQ 从入门到实战:核心原理、消息模型、可靠性机制与 Spring Boot 实战
spring boot·rabbitmq·java-rabbitmq
威哥爱编程12 小时前
【AI全栈12-12】Spring Boot 3.x → 4.x 迁移实操:Jakarta 11、Jackson 3 与 AI 2.0
java·spring boot·后端
威哥爱编程12 小时前
【AI全栈12-11】Spring Boot 把 AI 接口真正上线扛量:限流 / 降级 / 可观测
java·spring boot·后端