在MyBatis-Plus中,selectOne方法可以用來查詢單條記錄。對于復(fù)雜查詢,可以通過使用條件構(gòu)造器來實(shí)現(xiàn)。以下是一些技巧:
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);
通過以上技巧,可以實(shí)現(xiàn)復(fù)雜查詢并使用selectOne方法獲取單條記錄。在實(shí)際應(yīng)用中,根據(jù)具體需求選擇合適的條件構(gòu)造器和查詢方法,以達(dá)到最佳的查詢效果。