当前位置: 代码迷 >> Java Web开发 >> Ibatis中,两个表关联查询,返回类型如何写
  详细解决方案

Ibatis中,两个表关联查询,返回类型如何写

热度:95   发布时间:2016-04-17 11:07:20.0
Ibatis中,两个表关联查询,返回类型怎么写
语句如下:
XML code
<!-- 从议题库表,汇报单位表,会议信息表查询-->    <select id="searchTopicInfoByTopicId" parameterClass="TbMmsTopicLib" resultMap="TbMmsTopicLibMap">        SELECT            T.TOPIC_ID            ,T.TOPIC_TITLE    <!--  议题名称-->            ,R.UNIT_NAME        <!-- 汇报单位-->            ,T.APPROVAL_LEADER <!-- 审签领导-->            ,T.APPROVAL_DATE <!-- 审签日期-->            ,M.MEETING_NUM <!-- 第几届会议:会议名称-->            ,M.MEETING_DATE <!-- 会议时间-->        FROM            TB_MMS_TOPIC_LIB T        LEFT JOIN            TB_MMS_REPORT_UNIT R        ON            T.TOPIC_ID = R.TOPIC_ID        RIGHT JOIN            TB_MMS_MEETINGS M        ON            T.MEETING_ID = M.MEETING_ID        WHERE            T.TOPIC_ID = #topicId#    </select>


返回类型应该写什么,resultMap和resultClass有什么区别,什么时候用哪个怎么判断?


在<resultMap id="TbMmsTopicLibMap" class="com.cloudsoaring.mms.entity.TbMmsTopicLibEntity">这里应该怎么配置,对这个没用过,很模糊,

------解决方案--------------------
引用
1. 返回类型应该写什么,resultMap和resultClass有什么区别,什么时候用哪个怎么判断?
回答:
resultMap属于直接映射,可以把结果集中的数据库字段与实体类中的属性一一对应,这样通过select语句得到的结果就会准确的对上号
resultclass属于隐身映射,虽然你指定resultclass=“”,具体某一个类,但是select语句得到的结果是一条实力记录,但如果数据库字段与类的属性名字不一致,这个时候就会出现映射错误,有一种方式可以解决就是在写select语句时,给每个字段用as运算符取名字与属性一样:例如:select realname as name...其中realname是字段列名,name是属性字段名

resultmap比resultclass性能要高。尽量使用resultmap

2. 在<resultMap id="TbMmsTopicLibMap" class="com.cloudsoaring.mms.entity.TbMmsTopicLibEntity">这里应该怎么配置
看实例中红字部分:
<!-- 为AmUser类设置一个别名 -->
<typeAlias alias="amUser"
type="com.ringid.inner.service.database.pojo.AmUsers" />


 <!-- 查询返回对象 -->
<resultMap id="relAmUser" class="amUser">
<result property="personId" column="PERSON_ID" />
<result property="localeCode" column="LOCALE_CODE" />
<result property="status" column="STATUS" nullValue="0"/>
<result property="accountExpDate" column="ACCOUNT_EXP_DATE" />
<result property="selfRegistrationId" column="SELF_REGISTRATION_ID" />
<result property="dn" column="DN" />
</resultMap>

<!-- 高级查询am用户对象信息 -->
<select id="highFindAmUserObjects" resultMap="relAmUser"
parameterClass="userfilter">
select * from am_users
<dynamic prepend="where">
<isNotEqual prepend="AND" property="personId" compareValue="-1"> person_id = #personId# 
</isNotEqual>
<isNotNull prepend="AND" property="usrLogin"> usr_login = #usrLogin# 
</isNotNull>
<isNotEqual prepend="AND" property="status" compareValue="-1"> status = #status# 
</isNotEqual>
<isNotNull prepend="AND" property="email"> email = #email# 
</isNotNull>
<isNotNull prepend="AND" property="personalMobile"> personal_mobile = #personalMobile# 
</isNotNull>
</dynamic>
order by usr_login asc, person_id desc
</select>
  相关解决方案