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:
&l
Multiple column names can be passed as key value pairs
@Results(value = { @Result(property = "CourseSubscription", column = "{courseId=id,userId=user_id}
For more info, you can refer the
org.apache.ibatis.builder.MapperBuilderAssistant.parseCompositeColumnName(String).columnName -- API which parses the meta-data of @Result.column()
private List<ResultMapping> parseCompositeColumnName(String columnName) {
List<ResultMapping> composites = new ArrayList<ResultMapping>();
if (columnName != null && (columnName.indexOf('=') > -1 || columnName.indexOf(',') > -1)) {
StringTokenizer parser = new StringTokenizer(columnName, "{}=, ", false);
while (parser.hasMoreTokens()) {
String property = parser.nextToken();
String column = parser.nextToken();
ResultMapping.Builder complexBuilder = new ResultMapping.Builder(configuration, property, column, configuration.getTypeHandlerRegistry().getUnknownTypeHandler());
composites.add(complexBuilder.build());
}
}
return composites;
}
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.