在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方法獲取單條記錄。在實際應用中,根據具體需求選擇合適的條件構造器和查詢方法,以達到最佳的查詢效果。