@ResultMap with column prefix

104 views
Skip to first unread message

Patrick Duflot

unread,
Dec 5, 2018, 6:46:26 AM12/5/18
to mybati...@googlegroups.com
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,

Iwao AVE!

unread,
Dec 11, 2018, 9:18:49 AM12/11/18
to mybatis-user

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


--
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 mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages