你可以使用 Mybatis-Plus 的 wrapper 来实现两个表的内容显示在一个 json 里,具体实现步骤如下:

  1. 定义一个包含两个表的联合查询语句,使用 Mybatis-Plus 的 wrapper 来实现,如下:
QueryWrapper<IPAddress> ipAddressWrapper = new QueryWrapper<>();
ipAddressWrapper.eq('ip_address', '127.0.0.1');

QueryWrapper<IPDetail> ipDetailWrapper = new QueryWrapper<>();
ipDetailWrapper.eq('ip_address', '127.0.0.1');

List<Map<String, Object>> resultList = new ArrayList<>();

List<IPAddress> ipAddressList = ipAddressMapper.selectList(ipAddressWrapper);
for (IPAddress ipAddress : ipAddressList) {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put('ip_address', ipAddress.getIpAddress());

    List<IPDetail> ipDetailList = ipDetailMapper.selectList(ipDetailWrapper);
    resultMap.put('ip_detail_list', ipDetailList);

    resultList.add(resultMap);
}
  1. 将查询结果转换成 json 格式,如下:
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(resultList);

这样就可以将两个表的内容显示在一个 json 里了。

Mybatis-Plus 联合查询两个表并返回 JSON 数据

原文地址: https://www.cveoy.top/t/topic/mKrW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录