在MyBatis-Plus中,selectOne方法可以用来查询单条记录。对于复杂查询,可以通过使用条件构造器来实现。以下是一些技巧:
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "张三").like("email", "@gmail.com");
User user = userMapper.selectOne(queryWrapper);
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getName, "张三").like(User::getEmail, "@gmail.com");
User user = userMapper.selectOne(lambdaQueryWrapper);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.and(i -> i.eq("name", "张三").like("email", "@gmail.com"))
.or(i -> i.eq("age", 25).ge("create_time", LocalDateTime.now().minusDays(7)));
User user = userMapper.selectOne(queryWrapper);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "张三").like("email", "@gmail.com");
Map<String, Object> userMap = userMapper.selectOne(queryWrapper);
通过以上技巧,可以实现复杂查询并使用selectOne方法获取单条记录。在实际应用中,根据具体需求选择合适的条件构造器和查询方法,以达到最佳的查询效果。