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文件等

相关推荐
有个傻瓜32 分钟前
分页 total 对不上、列表还少几条:MyBatis-Plus 一对多 LEFT JOIN 分页错乱复盘与四种写法实测
spring boot·sql优化·mybatis-plus·分页查询·踩坑复盘
inferno9 小时前
十三、存储过程(PROCEDURE)
数据库·sql·mysql
Decay__11 小时前
PostgreSQL DBA 日常运维手册 —— 会话、锁阻塞、慢查询、膨胀与每日巡检
运维·数据库·sql·postgresql
千寻技术帮12 小时前
微信小程序+Spring Boot 烘焙商城系统(附完整源码+数据库+部署文档)50053
数据库·spring boot·微信小程序·烘焙商城
kiwixing13 小时前
GaussDB|几台机器、哪种协议:高可用部署形态与副本协议选型
数据库·sql·database·gaussdb
SongRenShuo13 小时前
三年 MyBatis 排查史:从我翻日志,到让 SQL 自己浮现
sql·mybatis·idea·日志·调试
CoderYanger15 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
Decay__15 小时前
PostgreSQL 安装部署实战 —— PGDG yum 源安装、initdb 初始化与 postgresql.conf 参数调优
运维·数据库·sql·postgresql
她的男孩16 小时前
状态默认填了 DRAFT,同步后却变成 '''DRAFT'':Forge Admin 清洗 PG 的 COLUMN_DEFAULT
spring boot·低代码·postgresql
考虑考虑1 天前
记一次@Async生产事故
spring boot·后端·spring