MyBatis-Plus:不写 SQL 实现取某字段最大值并仅返回该值
使用 MyBatis-Plus 实现取某个字段的最大值并只返回该值,可以通过 `LambdaQueryWrapper` 和 `select` 方法来实现。以下是一个示例代码: 假设你有一个实体类 `User`,其中有一个字段 `age`,你想要获取 `age` 字段的最大值。 1. 实体类: ```java import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; @TableName("user") public class User { private Long id; @TableField("age") private Integer age; // getters and setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } ``` 2. Mapper 接口: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface UserMapper extends BaseMapper<User> { } ``` 3. Service 层: ```java import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserMapper userMapper; public Integer getMaxAge() { LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.select(User::getAge).orderByDesc(User::getAge).last("limit 1"); User user = userMapper.selectOne(queryWrapper); return user != null ? user.getAge() : null; } } ``` 4. Controller 层(可选): ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping("/max-age") public Integer getMaxAge() { return userService.getMaxAge(); } } ``` 在这个示例中,我们使用了 `LambdaQueryWrapper` 来构建查询条件,并通过 `select` 方法指定只选择 `age` 字段。然后使用 `orderByDesc` 方法按 `age` 字段降序排序,并通过 `last("limit 1")` 方法限制结果集为一条记录。调用 `selectOne` 方法获取结果,并返回 `age` 字段的值。 这样就可以实现只返回某个字段的最大值。 ######[AI写代码神器 | 566点数解答 | 2024-10-17 15:01:29]
- Java SQL语法错误:“select count(*)”语句出错,教你排查MariaDB SQL语法问题!( | 350点数解答 | 2024-04-25 08:26:19)397
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)376
- 巧用 MyBatis Plus 自定义拦截器实现数据权限控制(讯飞星火 | 567点数解答 | 2024-05-29 22:59:21)267
- MyBatis-Plus 纯代码实现联表查询:附详细示例代码(字节豆包 | 284点数解答 | 2024-10-17 11:46:14)218
- MyBatis-Plus 轻松实现:获取表中特定字段最大值全流程指南(阿里通义 | 527点数解答 | 2024-10-17 14:52:50)385
- MyBatis-Plus 代码示例:轻松实现取某个字段最大值! (字节豆包 | 237点数解答 | 2024-12-10 10:12:49)169
- MyBatis-Plus 轻松实现取某个字段最大值:附代码示例及注意事项(阿里通义 | 247点数解答 | 2024-12-10 10:13:04)328
- MyBatis-Plus处理Integer类型JSON数组:自定义TypeHandler实现及注意事项(DeepSeek | 660点数解答 | 2025-08-26 11:22:23)103
- MyBatis-Plus:自定义TypeHandler实现JSON数组转List<Integer>及注意事项(DeepSeek | 656点数解答 | 2025-08-26 11:26:36)145
- C++实现:根据三个整数关系输出对应结果(plus/minus/illegal)(DeepSeek | 98点数解答 | 2025-12-15 21:37:25)81
- MySQL 数据库操作:用德鲁伊连接池和 Spring JDBC 实现 MVC 架构下 Student 表操作及事务测试全解析(字节豆包 | 1115点数解答 | 2025-03-19 11:17:31)310
- Spring JDBC+德鲁伊连接池:MVC架构下MySQL学生表查询、事务验证及代码实现全解析(DeepSeek | 1275点数解答 | 2025-03-19 11:21:32)296