SELECT FROM 表名 WHERE parent_id IN SELECT city_id FROM 表名 WHERE parent_id = 1;用lanbdawapper怎么实现
使用Mybatis-Plus的LambdaQueryWrapper可以实现此SQL语句的查询。
示例代码:
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public List<User> getUsersByCityId(Long cityId) {
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.in(User::getParentId, new LambdaQueryWrapper<City>()
.select(City::getCityId)
.eq(City::getParentId, cityId));
return baseMapper.selectList(wrapper);
}
}
其中,User为实体类,UserMapper为Mybatis的Mapper接口,UserService为Service接口。在LambdaQueryWrapper中,我们使用in方法传入一个LambdaQueryWrapper对象,该对象使用select和eq方法查询parent_id等于cityId的城市的city_id,最后返回查询到的用户列表。
原文地址: https://www.cveoy.top/t/topic/bMlM 著作权归作者所有。请勿转载和采集!