现在有两张表professional专业表和classes班级表,专业表中的proid作为班级表的外键存在。
professional:
public class Professional {
private int proid;
private String proname;
private String prodesc;
private Date createdate;
classes:
public class Classes {
private int clsid;
private String clsname;
private String clsdesc;
private Professional pro;
private Date createdate;
private Date enddate;
第一种解决方案:
<resultMap type="Classes" id="ClsResult">
<id column="clsid" property="clsid"/>
<result column="clsname" property="clsname"/>
<result column="clsdesc" property="clsdesc"/>
<result column="createdate" property="createdate"/>
<result column="enddate" property="enddate"/>
<association column="proid" property="pro" javaType="Professional" select="com.oa.dao.professionalDao.selProById"></association>
</resultMap>
<select id="selClsById" parameterType="int" resultMap="ClsResult">
SELECT * FROM classes WHERE clsid = #{id}
</select>
第二种解决方案(表连接查询):
如果采用表连接的方式进行查询,那么resultmap中每一个列名column和属性property都必须要指定
<resultMap type="Classes" id="ClsResult">
<id column="clsid" property="clsid"/>
<result column="clsname" property="clsname"/>
<result column="clsdesc" property="clsdesc"/>
<result column="createdate" property="createdate"/>
<result column="enddate" property="enddate"/>
<association property="pro" javaType="Professional">
<result column="proid" property="proid"/>
<result column="proname" property="proname"/>
<result column="prodesc" property="prodesc"/>
<result column="pcreatedate" property="createdate"/>
</association>
</resultMap>
<select id="selClsById" parameterType="int" resultMap="ClsResult">
select classes.*,professional.proname,professional.prodesc,professional.createdate as pcreatedate from classes INNER JOIN professional on classes.proid = professional.proid where classes.clsid = 1
</select>