1

我有一个以前为 JPA 构建的域模型,如下所示:

@Entity
@Table(name="TABLE")
public class MyClassImpl implements MyClass, Serializable {

   @Embeddable
   public static class ID {
   @Column( name = "COL_1", nullable = false)
   protected int id1;
   @Column( name = "COL_2", nullable = false)
   protected int id2;
   @Column( name = "COL_3", nullable = false, unique = true)
   protected String id3;

   ...

   }

   @EmbeddedId
   protected ID id;
   @Column(name = "COL_4")
   protected String otherData;
   ...

}

现在我正在迁移到 MyBatis,我对映射类似的东西有疑问。

我知道我可以制作一个像这样的 resultMap:

<resultMap id="myClassResultMap" type="MyClass">
   <association property="id" javaType="MyClass.ID">
      <id property="id1" column="COL_1" />
      <id property="id2" column="COL_2" />
      <id property="id3" column="COL_3" />
   </association>

   <result property="otherData" column="COL_4" />
   ...
</resultMap>

中的<id ... />字段association会用作类的 id 吗?如果我想将其标记idfinal并将其传递给构造函数怎么办?

我也将该 ID 用于其他类:

@Entity
@Table(name = "OTHERTABLE")
public class MyOtherClassImpl implements MyOtherClass, Serializable {

   @Embeddable
   public static class ID {

      @Embedded
      MyClass.ID ref;

      @Column(name = "ID", nullable = false, unique = true)
      protected int id;

      //methods
   }

   @EmbeddedId
   protected ID id;
   @Column(name = "COL_5")
   protected String otherData;
   ...
}

因为这两个表共享 3 个主键。

有可能保持这样的结构吗?

4

2 回答 2

0

我找到了两种解决方法:

1st:进入构造函数(实际上我正在使用这个)

public class MyClassImpl implements MyClassWritable, Serializable {

   public class IdImpl implements MyClass.Id, Comparable<IdImpl> {
       private final int id1;
       private final int id2;
       private final String id3;

       private IdImpl(int id1, int id2, String id3) {
          this.id1 = id1;
          this.id2 = id2;
          this.id3 = id3;
       }

       // getters, compareTo, hashCode, equals and toString.
       // implementing Comparable to use MyClass.Id as indexes into Collections
   }

   private final Id id;
   ...
   private String other;

   public MyClassImpl(int id1, int id2, String id3) {
      this.id = new IdImpl(id1, id2, id3);
   }

   // methods
}

结果地图是:

<resultMap id="myClassResultMap" type="MyClass">
   <constructor>
      <idArg column="ID_COL_1" javaType="java.lang.Integer"/>
      <idArg column="ID_COL_2" javaType="java.lang.Integer"/>
      <idArg column="ID_COL_3" javaType="java.lang.String"/>
   </contructor>
   <result property="other" column="OTHER_COL" />
</resultMap>

第二:非最终字段和设置器进入 Id 类。resultMap 将是:

public class MyClassImpl implements MyClassWritable, Serializable {

   public class IdImpl implements MyClass.IdWritable, Comparable<IdImpl> {
       private int id1;
       private int id2;
       private String id3;

       private IdImpl() {
          super();
       }

       // SETTERS, getters, compareTo, hashCode, equals and toString.
       // implementing Comparable to use MyClass.Id as indexes into Collections
   }

   private final Id id;
   ...
   private String other;

   public MyClassImpl(int id1, int id2, String id3) {
      this.id = new IdImpl();
   }

   // methods
}

<resultMap id="myClassResultMap" type="MyClass">
   <id property="id.id1" column="ID_COL_1" javaType="java.lang.Integer"/>
   <id property="id.id2" column="ID_COL_2" javaType="java.lang.Integer"/>
   <id property="id.id3" column="ID_COL_3" javaType="java.lang.String"/>
   <result property="other" column="OTHER_COL" />
</resultMap>

我采用了第一个在将对象传递给线程时保持类“只读”的同时编写的样板更少的方法。容器类的结构如下:

+ public interface MyClass // declares getters
+- public interface Id {...} // MyClass.Id declares getters for id's
|
+--+ public interface MyClassWritable extends MyClass // declares setters
   |
   +-- public class MyClassImpl implements MyClassWritable
于 2013-06-07T15:16:57.923 回答
0

希望这对其他人有帮助,请尝试:

<resultMap id="myClassResultMap" type="MyClass">
   <result property="id.id1" column="COL_1" />
   <result property="id.id2" column="COL_2" />
   <result property="id.id3" column="COL_3" />
   <result property="otherData" column="COL_4" />
   ...
</resultMap>
于 2017-06-29T14:22:17.673 回答