Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple columns in myBatis <assoctiation>

I want to know, how do we pass multiple columns in a myBatis association tag.

For example, I have the following xml snippet in one my mapper.xml file:

<resultMap type="com.mysite.domain.CourseBuilderCourses" id="ResultMapWithAssmnts" extends="BaseResultMap">

    <association property="totalAssignmentCnt" column="course_id" select="selectTotalAssgnmentsCnt"/>
    <association property="totalAssessmentCnt" column="course_id" select="selectTotalAssesmentsCnt"/>  
<!--  see this association >> --> <association property="subscription" column="course_id" select="com.mysite.persistence.mybatis.CourseSubscriptionMapper.selectByUsercId"/> 

  </resultMap>

As you can see, the <association> with property subscription has only one column, course_id

I want to pass 2 columns to it, and therefore the resultant code, how do we do that?

I tried the following combinations, none worked:

column="{course_id,user_id}"   // null,null are passed as parameters 
column="course_id,user_id"   // null,null are passed as parameters 
column="{COURSE_ID=course_id,USER_ID=user_id}"   // null,null are passed as parameters 

but if I pass single, column="{course_id}" or column="course_id"

works without any issues.

Any idea guys?

like image 367
linuxeasy Avatar asked Jun 19 '13 13:06

linuxeasy


1 Answers

You should use the following syntax for composite keys:

column="{prop1=col1,prop2=col2}".

Where prop1, prop2 are parameters of the associated query and col1, col2 are sql columns passed to that query.

In your case:

CourseMapper.xml:

column="{courseId=id,userId=user_id}" 
...
select id, user_id, ... from course ...

CourseSubscriptionMapper.xml:

<select id="selectByUsercId" ...>
    select ... where course_id=#{courseId} and user_id=#{userId}
</select>

I just checked it worked fine for me. If you have any questions, please feel free to ask.

like image 87
Jarandinor Avatar answered Oct 24 '22 10:10

Jarandinor