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


1. 注意事项

(1)内部类必须是静态内部类。
(2)xml中内部类连接使用$

下面看一个示例。

2. 类型封装

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在将查询结果封装到返回类型的时候会报没有匹配的构造器错误:

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]

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

3. resultMap

    <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.lang.ClassNotFoundException: 
Cannot find class: 
cn.wangbo.model.survey.LeaveMessageQueryCondition.CustomQuestion

4. 查询SQL

    <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>

5. 返回结果

{
	"code": 200,
	"message": "OK",
	"data": [{
		"projectId": 77789,
		"projectName": "测试",
		"questions": [{
			"questionId": 222,
			"questionName": "输入框类"
		}],
		"organizations": [{
			"channelId": 111,
			"organizationId": 555,
			"organizationName": "测试系统公司"
		}]
	}]
}
  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值