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

相关推荐
鱟鲥鳚5 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
腾渊信息科技公司7 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
憧憬成为web高手8 小时前
皮卡丘靶场速通--sql 2
数据库·sql·mybatis
隔窗听雨眠10 小时前
Spring Boot在云原生时代的编程范式革新研究
spring boot·后端·云原生
其美杰布-富贵-李13 小时前
Spring Boot 工程开发全流程说明
java·spring boot·后端
yio_yin14 小时前
Spring事务管理(数据一致性)
数据库·sql·spring
dear_bi_MyOnly14 小时前
【SpringBoot配置文件】
java·spring boot·后端·学习·spring·java-ee·学习方法
其美杰布-富贵-李15 小时前
Spring Boot 依赖注入说明文档
java·spring boot·python
Csvn16 小时前
📊 SQL 入门 Day 9:CTE(公用表表达式)— 让 SQL 像写代码一样优雅
后端·sql
流烟默16 小时前
SpringBoot应用链路追踪 traceId 技术方案
spring boot·skywalking·traceid