@ResultMap with column prefix

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

@ResultMap with column prefix

Patrick Duflot
Hi all,

Is there a way to set a column prefix with the @ResultMap annotation?

private static final String PATIENT_INFO_COLS = "PI.emrId AS pi_emrId,
PI.version AS pi_version, PI.lastModified AS pi_lastModified,
PI.lastModifier AS pi_lastModifier, PI.name AS pi_name, PI.firstName
AS pi_firstName, PI.birthDate AS pi_birthDate";

@ResultMap("org.PatientInfoMapper", ???columnPrefix="pi_"???)
   @Select("SELECT " + PATIENT_INFO_COLS + " FROM patientInfo PI WHERE
PI.emrId=#{emrId}")
   PatientInfo getPatientInfo(@Param("emrId") String emrId);

The reason is that the static string PATIENT_INFO_COLS is also used
when selecting and joining with other tables with conflicting column
names.

   <resultMap id="VersionedEntityMapper"
      type="org.AbstractVersionedEntity">
      <result property="version" column="version" />
      <result property="lastModified" column="lastModified" />
      <result property="lastModifier" column="lastModifier" />
   </resultMap>

   <resultMap id="PatientInfoMapper" type="org.PatientInfo"
extends="VersionedEntityMapper">
      <result property="name" column="name" />
      <result property="firstName" column="firstName" />
      <result property="birthDate" column="birthDate" />
   </resultMap>

   <resultMap id="PatientLocationMapper" type="org.PatientLocation"
extends="VersionedEntityMapper">
      <result property="wardId" column="wardId"/>
      <result property="room" column="room"/>
   </resultMap>

   <resultMap id="PatientMapper" type="org..Patient">
      <id property="id" column="patientId" />
      <result property="emrId" column="emrId" />
      <result property="lastData" column="lastData" />
      <association property="info" javaType="org..PatientInfo"
resultMap="PatientInfoMapper"/>
      <association property="location" javaType="org..PatientLocation"
resultMap="PatientLocationMapper"/>
   </resultMap>

Best,

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: @ResultMap with column prefix

Iwao AVE!

Not exactly.
In MyBatis, it can be achieved by defining <sql /> with variables.

<sql id="patientInfoColumns">
  ${alias}emrId AS ${prefix}emrId,
  ${alias}version AS ${prefix}version,
  ...
</sql>

For a query that does not require alias/prefix (i.e. patientInfo is the root table), you just include the fragment as-is.

<select id="getPatientInfo" resultMap="PatientInfoMapper">
  select
  <include refid="patientInfoColumns" />
  from patientInfo
  where emrId = #{emrId}
</select>

And in a query with JOINs, include the fragment with ‘alias’ and ‘prefix’ specified.

<select id="getPatientInfo" resultMap="PatientInfoMapper">
  select
  <include refid="patientColumns" />
  ,
  <include refid="patientInfoColumns">
    <property name="alias" value="PI." />
    <property name="prefix" value="pi_" />
  </include>
  from patient
  join patientInfo PI on ...
</select>

So, with your example, columnPrefix should be specified in the <association /> in PatientMapper.

If you have any difficulty, share an executable test case or example project on GitHub and I’ll help you make it work. :)

Regards,
Iwao


On Wed, Dec 5, 2018 at 8:46 PM Patrick Duflot <[hidden email]> wrote:
Hi all,

Is there a way to set a column prefix with the @ResultMap annotation?

private static final String PATIENT_INFO_COLS = "PI.emrId AS pi_emrId,
PI.version AS pi_version, PI.lastModified AS pi_lastModified,
PI.lastModifier AS pi_lastModifier, PI.name AS pi_name, PI.firstName
AS pi_firstName, PI.birthDate AS pi_birthDate";

@ResultMap("org.PatientInfoMapper", ???columnPrefix="pi_"???)
   @Select("SELECT " + PATIENT_INFO_COLS + " FROM patientInfo PI WHERE
PI.emrId=#{emrId}")
   PatientInfo getPatientInfo(@Param("emrId") String emrId);

The reason is that the static string PATIENT_INFO_COLS is also used
when selecting and joining with other tables with conflicting column
names.

   <resultMap id="VersionedEntityMapper"
      type="org.AbstractVersionedEntity">
      <result property="version" column="version" />
      <result property="lastModified" column="lastModified" />
      <result property="lastModifier" column="lastModifier" />
   </resultMap>

   <resultMap id="PatientInfoMapper" type="org.PatientInfo"
extends="VersionedEntityMapper">
      <result property="name" column="name" />
      <result property="firstName" column="firstName" />
      <result property="birthDate" column="birthDate" />
   </resultMap>

   <resultMap id="PatientLocationMapper" type="org.PatientLocation"
extends="VersionedEntityMapper">
      <result property="wardId" column="wardId"/>
      <result property="room" column="room"/>
   </resultMap>

   <resultMap id="PatientMapper" type="org..Patient">
      <id property="id" column="patientId" />
      <result property="emrId" column="emrId" />
      <result property="lastData" column="lastData" />
      <association property="info" javaType="org..PatientInfo"
resultMap="PatientInfoMapper"/>
      <association property="location" javaType="org..PatientLocation"
resultMap="PatientLocationMapper"/>
   </resultMap>

Best,

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.