MyBatis 封装返回类型中包含内部类

注意事项:

(1)内部类必须是静态内部类。

(2)xml中内部类连接使用$

下面看一个示例。

JavaBean 代码:

java 复制代码
import lombok.Data;

import java.util.List;

/**
 * @author wangbo
 * @date 2019/9/30 9:26
 */
@Data
public class LeaveMessageQueryCondition {
    private Integer projectId;
    private String projectName;
    private List<CustomQuestion> questions;
    private List<CustomOrganization> organizations;

    @Data
    public static class CustomQuestion {
        private Integer questionId;
        private String questionName;
    }

    @Data
    public static class CustomOrganization {
        private  Integer channelId;
        private Integer organizationId;
        private String organizationName;
    }
}

如果LeaveMessageQueryCondition类里面的内部类CustomQuestionCustomOrganization没有使用static修饰,MyBatis在将查询结果封装到返回类型的时候会报没有匹配的构造器错误:

java 复制代码
org.mybatis.spring.MyBatisSystemException: 
nested exception is org.apache.ibatis.executor.ExecutorException: 
No constructor found in cn.wangbo.model.survey.LeaveMessageQueryCondition$CustomQuestion matching
[java.lang.Long, java.lang.String, java.lang.Long, java.lang.String, java.lang.Long, java.lang.Long, java.lang.String]

原因是非静态内部类需要依赖外部类实例,异常提示可以看出,外部类的构造器参数包括了所有非静态内部类的属性。

resultMap 代码:

xml 复制代码
    <resultMap id="queryConditionResult" type="cn.wangbo.model.survey.LeaveMessageQueryCondition">
        <result property="projectId" column="project_id" />
        <result property="projectName" column="project_name" />
        <collection property="questions" ofType="cn.wangbo.model.survey.LeaveMessageQueryCondition$CustomQuestion">
            <result property="questionId" column="question_id" />
            <result property="questionName" column="question_name" />
        </collection>
        <collection property="organizations" ofType="cn.wangbo.model.survey.LeaveMessageQueryCondition$CustomOrganization">
            <result property="channelId" column="channel_id" />
            <result property="organizationId" column="organization_id" />
            <result property="organizationName" column="organization_name" />
        </collection>
    </resultMap>

这里注意内部类和外部类的连接符号不同于Java中的点.。必须使用$连接符,否则在项目启动的时候会报类找不到异常。

java 复制代码
java.lang.ClassNotFoundException: 
Cannot find class: 
cn.wangbo.model.survey.LeaveMessageQueryCondition.CustomQuestion

查询 SQL 代码:

xml 复制代码
    <select id="getLeaveMessageQueryCondition" resultMap="queryConditionResult">
        SELECT project_id, project_name, question_id, question_name, channel_id, organization_id, organization_name
        FROM ...
        LEFT JOIN ... ON ...
        LEFT JOIN ... ON ...
        WHERE ...
    </select>

查询返回结果:

json 复制代码
{
	"code": 200,
	"message": "OK",
	"data": [{
		"projectId": 77789,
		"projectName": "测试",
		"questions": [{
			"questionId": 222,
			"questionName": "输入框类"
		}],
		"organizations": [{
			"channelId": 111,
			"organizationId": 555,
			"organizationName": "测试系统公司"
		}]
	}]
}
相关推荐
指令集梦境1 小时前
Cursor + Spring Boot实战:从零写一个RESTful API
spring boot·后端·restful
码云之上1 小时前
聊聊如何设计一个高效、稳定的 Node.js 接入层
前端·后端·node.js
IT_陈寒2 小时前
Vite项目build后路由404了?你可能漏了这个小配置
前端·人工智能·后端
宸津-代码粉碎机3 小时前
Spring AI企业级实战|从RAG优化到Agent多工具调度
java·大数据·人工智能·后端·python·spring
吴佳浩3 小时前
AI Infra 的真相:Go 没输,rust也不是取代
后端·rust·go
INFINI Labs3 小时前
Elasticsearch 6/7/8 到 Easysearch 2.x 迁移指南
大数据·elasticsearch·mybatis·向量·snapshot
喵个咪3 小时前
实时游戏网络协议深度对比:KCP vs WebRTC vs WebSocket
后端·websocket·webrtc
普通网友3 小时前
springboot之集成Elasticsearch
spring boot·后端·elasticsearch
QuZero3 小时前
Guava Cache Deep Dive
java·后端·算法·guava
leeyi4 小时前
SSE 实时推流 —— Token 怎么一个个蹦出来
后端·agent