iBATIS:利用resultMap复合查询
sqlmap文件中:
resultMap=resultMap标签中的id属性
resultClass=类
typeAlias
标签定义类别名
下面这个sqlmap定义了一个复合查询,从method_table表中查出method_id
和rule_type
字段,
填充com.xjb.Method
类的同名属性;
而ruleList
属性是另外一个查询,从rule_table查出多条记录,每条记录对应一个com.xjb.Rule
JavaBean类,
然后多条记录作为一个List填充ruleList
属性。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Method">
<typeAlias alias="method" type="com.xjb.Method"/>
<typeAlias alias="rule" type="com.xjb.Rule"/>
<resultMap class="method" id="methodResultMap">
<result column="method_id" property="method_id" />
<result column="rule_type" property="rule_type" />
<result property="ruleList" column="rule_type" select="Method.getRuleListByType" />
</resultMap>
<sql id="method_colomns">
method_id,rule_type
</sql>
<select id="getMethodList" parameterClass="map" resultMap="methodResultMap">
select <include refid="method_colomns"/> from method_table
<dynamic prepend="WHERE">
<isNotEmpty prepend="AND" property="method_id"> method_id = #method_id# </isNotEmpty>
<isNotEmpty prepend="AND" property="rule_type"> rule_type = #rule_type# </isNotEmpty>
</dynamic>
<isNotEmpty prepend="order by " property="_order">
$_order$
</isNotEmpty>
<isNotEmpty prepend="limit " property="_limit">
#_limit._pstart#,#_limit._psize#
</isNotEmpty>
</select>
<select id="getRuleListByType" parameterClass="String" resultClass="rule">
select * from rule_table
<dynamic prepend="WHERE">
<isNotEmpty prepend="AND" property="value"> rule_type = #value# </isNotEmpty>
</dynamic>
</select>
</sqlMap>
bean类
package com.xjb;
import java.io.Serializable;
public class Method implements Serializable {
private static final long serialVersionUID = 000L;
String method_id;
String rule_type;
List<Rule> ruleList;
public String getMethod_id() {
return method_id;
}
public void setMethod_id(String method_id) {
this.method_id = method_id;
}
public String getRule_type() {
return rule_type;
}
public void setRule_type(String rule_type) {
this.rule_type = rule_type;
}
public List<Rule> getRuleList() {
return ruleList;
}
public void setRuleList(List<Rule> ruleList) {
this.ruleList = ruleList;
}
@Override
public String toString() {
return "Method [method_id=" + method_id + ", rule_type=" + rule_type + "]";
}
}