MyBatis-Plus 左连接查询:实现 MySQL LEFT JOIN
MyBatis-Plus 支持 MySQL 中的 LEFT JOIN 功能。你可以使用 MyBatis-Plus 提供的 Wrapper 类来实现左连接查询。
下面是一个示例:
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public Page<User> getUserListWithLeftJoin() {
Page<User> page = new Page<>(1, 10);
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.select('user.*, role.role_name')
.leftJoin('role', 'user.role_id = role.id')
.orderByDesc('user.id')
.groupBy('user.id');
return userMapper.selectPage(page, wrapper);
}
}
在上面的示例中,我们使用QueryWrapper类来构建查询条件,使用leftJoin方法来实现左连接查询。select方法用于指定查询的字段,orderByDesc方法用于指定排序方式,groupBy方法用于指定分组方式。
希望对你有帮助!
AI 优化版
原文地址:https://www.cveoy.top/t/topic/qgja 著作权归作者所有。请勿转载和采集!