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": "测试系统公司"
		}]
	}]
}
相关推荐
李日灐20 小时前
C++进阶必备:红黑树从 0 到 1: 手撕底层,带你搞懂平衡二叉树的平衡逻辑与黑高检验
开发语言·数据结构·c++·后端·面试·红黑树·自平衡二叉搜索树
阿杰真不会敲代码21 小时前
Mybatis-plus入门到精通
java·tomcat·mybatis
qq_2975746721 小时前
【实战】POI 实现 Excel 多级表头导出(含合并单元格完整方案)
java·spring boot·后端·excel
郝学胜-神的一滴21 小时前
超越Spring的Summer(一): PackageScanner 类实现原理详解
java·服务器·开发语言·后端·spring·软件构建
Tony Bai21 小时前
“Go 2,请不要发生!”:如果 Go 变成了“缝合怪”,你还会爱它吗?
开发语言·后端·golang
Victor35621 小时前
Hibernate(91)如何在数据库回归测试中使用Hibernate?
后端
Victor35621 小时前
MongoDB(1)什么是MongoDB?
后端
侠客行03171 天前
Mybatis连接池实现及池化模式
java·mybatis·源码阅读
Victor3561 天前
https://editor.csdn.net/md/?articleId=139321571&spm=1011.2415.3001.9698
后端
Victor3561 天前
Hibernate(89)如何在压力测试中使用Hibernate?
后端