Spring中用Mybatis注解查询映射多个对象

1.映射写法如下

java 复制代码
@SelectProvider(type = UserGroupMapper.class, method = "getOrigins")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "groupId", column = "groupId"),
            @Result(property = "resId", column = "resId"),
            @Result(property = "resGroup.id", column = "groupId"),
            @Result(property = "resGroup.name", column = "groupName"),
            @Result(property = "resGroup.type", column = "groupType"),
            @Result(property = "resGroup.orgCode", column = "groupOrgCode"),
            @Result(property = "res.id", column = "resId"),
            @Result(property = "res.orgCode", column = "resOrgCode"),
            @Result(property = "res.name", column = "resName"),
            @Result(property = "res.ipv4", column = "ipv4"),
            @Result(property = "res.ipv6", column = "ipv6"),
            @Result(property = "res.resEdition", column = "resEdition"),
            @Result(property = "res.status", column = "resStatus"),
            @Result(property = "res.domainName", column = "domainName")
    })
    List<ResGroupBindRes> getOrigins(UserGroupQuery query);

2.SQL如下:

java 复制代码
public String getOrigins() {

        StringBuilder sql = new StringBuilder("SELECT rlg.c_id as id, rlg.c_group_id as groupId, rlg.c_res_id as resId, lg.c_name as groupName, lg.c_type as groupType, lg.c_desc as groupDesc, lg.c_org_code as groupOrgCode, r.c_org_code as resOrgCode, r.c_name as resName, r.c_ipv4 as ipv4 " +
                "  FROM t_res_logic_group as rlg " +
                "LEFT JOIN  t_res as r on rlg.c_res_id = r.c_id LEFT JOIN t_logic_group as lg on rlg.c_group_id = lg.c_id ");

        return sql.toString();
    }

3.映射的对象如下

java 复制代码
@Data
public class ResGroupBindRes {

    private String id;
    /* 资源组 */
    private ResGroup resGroup;

    /* 资源 */
    private Res res;

    /* 资源id集合 */
    private String resId;

    /* 组id */
    private String groupId;
}

4.亲测有效

相关推荐
掉头发的王富贵1 天前
【StarRocks】极限十分钟入门StarRocks
数据库·sql·mysql
唐青枫2 天前
Java Spring WebFlux 实战指南:用 Mono、Flux 和 WebClient 写响应式接口
java·spring
咖啡八杯4 天前
GoF设计模式——策略模式
java·后端·spring·设计模式
Flittly5 天前
【AgentScope Java新手村系列】(11)中断与恢复
java·spring boot·spring
dunky5 天前
Spring 的三级缓存与循环依赖
后端·spring
zzzzzz3106 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
云技纵横8 天前
唯一索引 INSERT 死锁实战:5 秒复现交叉插入的 S 锁循环等待
sql·mysql
码云数智-园园10 天前
C++20 Modules 模块详解
java·开发语言·spring
咖啡八杯10 天前
GoF设计模式——享元模式
java·spring·设计模式·享元模式