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

相关推荐
仙俊红12 分钟前
SpringBoot启动原理
java·spring boot·后端
記億揺晃着的那天2 小时前
告别误操作!Spring Boot 多环境配置隔离与启动守卫实战
java·spring boot·后端·环境隔离
李白的天不白2 小时前
查找容器IP
sql
skywalker_112 小时前
SpringBoot速通(实战教学)
java·spring boot·redis·rpc·ssm·mybatis-plus
码不停蹄的玄黓3 小时前
Spring Boot 实现过滤器(Filter)三种常用方式
java·spring boot·后端
Flittly5 小时前
【AgentScope Java新手村系列】(4)结构化输出
java·spring boot·spring·ai
mqiqe6 小时前
面试题-MyBatis 面试篇
java·面试·mybatis
kuonyuma6 小时前
MyBatis入门·注解操作
java·spring boot·mysql·spring·mybatis
码不停蹄的玄黓8 小时前
MySQL慢SQL瓶颈定位
sql·mysql
我登哥MVP8 小时前
SpringCloud 核心组件解析:服务链路追踪
java·spring boot·后端·spring·spring cloud·java-ee·maven