【Mybatis】xml中映射内部类

前言

  • 使用Mybatis框架是经常要在xml文件中映射实体类对象,如果在实体类中定义了内部类,要如何映射呢?


一、内部类映射方法

使用 $ 符号连接外部类和内部类,而不是常用的 .

二、代码

1. 实体类代码展示

@Data
public class User {
    private String userName;
    private String password;
    public List<User.Detail> userDetail;

    @Data
    public static class Detail {
        private String address;
        private String age;
    }
}

2. XML文件映射代码展示

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.demo.common.mapper.userMapper">
    <resultMap id="BaseResultMap" type="com.xxx.demo.common.model.User">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="user_name" property="userName" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
        <collection property="userDetail" ofType="com.xxx.demo.common.model.User$Detail">
            <result column="address" property="address" jdbcType="VARCHAR"/>
            <result column="age" property="age" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>
</mapper>

posted @ 2022-02-16 16:00  中国制造  阅读(560)  评论(0编辑  收藏  举报