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": "测试系统公司"
		}]
	}]
}
相关推荐
szm022512 小时前
Spring
java·后端·spring
AlexDeng13 小时前
EF Core 开发实践:Left Join 查询的多种实现方式
后端
马卡巴卡13 小时前
用Spring的ApplicationEventPublisher进行事件发布和监听
后端
y***n61413 小时前
springboot项目架构
spring boot·后端·架构
无名之辈J13 小时前
生产环境慢 SQL 排查与优化
后端
悟能不能悟13 小时前
jasper里面$F和$P的区别
开发语言·后端
短剑重铸之日13 小时前
《7天学会Redis》Day 3 - 持久化机制深度解析
java·redis·后端·缓存
萧曵 丶14 小时前
Spring 全套高频面试题(由浅到深 完整版)
java·后端·spring
武子康14 小时前
大数据-213 Python 手写 K-Means 聚类实战(鸢尾花 Iris 数据集):从距离函数到迭代收敛与坑点
大数据·后端·机器学习
神奇小汤圆14 小时前
MySQL大事务的Recovery优化
后端