java 在 myBatis <association> 中传递多列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17193040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing multiple columns in myBatis <assoctiation>
提问by linuxeasy
I want to know, how do we pass multiple columns in a myBatis association tag.
我想知道,我们如何在一个 myBatis 关联标签中传递多个列。
For example, I have the following xml snippet in one my mapper.xml file:
例如,我的 mapper.xml 文件中有以下 xml 片段:
<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
如您所见,<association>
with property
subscription 只有一列,course_id
I want to pass 2 columns to it, and therefore the resultant code, how do we do that?
我想将 2 列传递给它,因此生成的代码,我们该怎么做?
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"
但如果我通过单, column="{course_id}" 或 column="course_id"
works without any issues.
工作没有任何问题。
Any idea guys?
有什么想法吗?
回答by Jarandinor
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.
prop1, prop2
关联查询的参数在哪里以及col1, col2
传递给该查询的 sql 列在哪里。
In your case:
在你的情况下:
CourseMapper.xml:
CourseMapper.xml:
column="{courseId=id,userId=user_id}"
...
select id, user_id, ... from course ...
CourseSubscriptionMapper.xml:
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.
我只是检查它对我来说工作正常。如果您有任何问题,请随时提问。
回答by hemantvsn
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()
org.apache.ibatis.builder.MapperBuilderAssistant.parseCompositeColumnName(String).columnName -- 解析@Result.column()元数据的API
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;
}