java – Mybatis嵌套集合无法正确使用列前缀

我需要使用mybatis映射为另一个集合中的对象设置集合.

它适用于我没有使用columnPrefix,但我需要它,因为有很多可重复的列.

     <resultMap id="ParentMap" type="org.example.mybatis.Parent">
        <id column="Id" jdbcType="VARCHAR" property="id" />
        <result column="Name" jdbcType="VARCHAR" property="name" />
        <result column="SurName" jdbcType="VARCHAR" property="surName" />

        <collection property="childs"
        javaType="ArrayList" ofType="org.example.mybatis.Child"
        resultMap="ChildMap" columnPrefix="c_"/>       
    </resultMap>

<resultMap id="ChildMap" type="org.example.mybatis.Parent">
    <id column="Id" jdbcType="VARCHAR" property="id" />
    <result column="ParentId" jdbcType="VARCHAR" property="parentId" />
    <result column="Name" jdbcType="VARCHAR" property="name" />
    <result column="SurName" jdbcType="VARCHAR" property="surName" />
    <result column="Age" jdbcType="INTEGER" property="age" />

    <collection property="toys"
        javaType="ArrayList" ofType="org.example.mybatis.Toy"
        resultMap="ToyMap" columnPrefix="t_"/>   
</resultMap>

<resultMap id="ToyMap" type="org.example.mybatis.Toy">
    <id column="Id" jdbcType="VARCHAR" property="id" />
    <result column="ChildId" jdbcType="VARCHAR" property="childId" />
    <result column="Name" jdbcType="VARCHAR" property="name" />
    <result column="Color" jdbcType="VARCHAR" property="color" />
</resultMap>

<sql id="Parent_Column_List">
    p.Id, p.Name, p.SurName,
</sql>  

<sql id="Child_Column_List">
    c.Id as c_Id, c.ParentId as c_ParentId, c.Name as c_Name, c.SurName as c_Surname, c.Age as c_Age,
</sql>

<sql id="Toy_Column_List">
    t.Id as t_Id, t.Name as t_Name, t.Color as t_Color
</sql>  

<select id="getParent" parameterType="java.lang.String" resultMap="ParentMap" >
    select 
    <include refid="Parent_Column_List"/>
    <include refid="Child_Column_List" />
    <include refid="Toy_Column_List" />
    from Parent p

    left outer join Child c on p.Id = c.ParentId
    left outer join Toy t on c.Id = t.ChildId
    where p.id = #{id,jdbcType=VARCHAR}

使用columnPrefix一切正常,但嵌套玩具集合是空的.
数据库上的SQL查询工作正常,所有玩具都加入了.

可能是我错过了什么或者这是mybatis的错误?

解决方法:

我遇到了同样的问题.
返回null的原因是ChildMap有一个列前缀’c_’,而ToyMap的前缀是’t_’,但是ChildMap包含了ToyMap by collection属性.所以,实际上在这种情况下,Toy表中的所有列都必须有前缀’c_t_’.

<sql id="Toy_Column_List">
    t.Id as c_t_Id, t.Name as c_t_Name, t.Color as c_t_Color
</sql>  
上一篇:java – 如何在应用程序级别管理只读数据库连接


下一篇:C# ibatis 测试