mybatis中如何将多个表的查询结果,放入结果集中返回
1、首先需要将resultMap进行改造,为了避免对其他sql的影响建议另外定义一个resultMapExtral,避免id相同,
2、然后在resultMapExtral中添加其它表的字段,若多个表中的字段名相同,要对查询结果和resultMapExtral中相同的字段另起别名,
3、最后另外定义一个实体bean接收结果。相当于重新定义一个包含所有返回字段的entity,而不能用原先单表的entity。(RccpWorkerCenterCapacityEntityExtral)
4、也可以直接返回JsonObject对象,不用返回entity,这样就不需要用resultMap了,更简单一些。
定义结果集:
<resultMap id="BaseResultMapExtral" type="com.cybertron.service.rccp.entity.RccpWorkerCenterCapacityEntityExtral"> <id column="workcenter_capacity_id" jdbcType="INTEGER" property="workercenterCapacityId" /> <result column="process_number" jdbcType="INTEGER" property="processNumber" /> <result column="product_name" jdbcType="VARCHAR" property="productName" /> <result column="product_id" jdbcType="INTEGER" property="productId" /> <result column="average_volume" jdbcType="INTEGER" property="averageVolume" /> <result column="single_process_time" jdbcType="DOUBLE" property="singleProcessTime" /> <result column="change_model_time" jdbcType="DOUBLE" property="changeModelTime" /> <result column="product_prepare_time" jdbcType="DOUBLE" property="productPrepareTime" /> <result column="take_down_time" jdbcType="DOUBLE" property="takeDownTime" /> <result column="unit_transport_size" jdbcType="DOUBLE" property="unitTransportSize" /> <result column="s_resource_id" jdbcType="NUMERIC" property="sResourceId" /> <result column="ad_wf_node_id" jdbcType="NUMERIC" property="adWfNodeId" /> <result column="name" jdbcType="VARCHAR" property="workcenterName" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <result column="create_user" jdbcType="VARCHAR" property="createUser" /> <result column="modify_time" jdbcType="TIMESTAMP" property="modifyTime" /> <result column="modify_user" jdbcType="VARCHAR" property="modifyUser" /> <result column="isdelete" jdbcType="BOOLEAN" property="isdelete" /> <result column="isdefault" jdbcType="BOOLEAN" property="checkDefault" /> <result column="is_key_wc" jdbcType="BOOLEAN" property="isKeyWc" /> <result column="parent_product_id" jdbcType="NUMERIC" property="paproductId" /> <result column="parentProductName" jdbcType="VARCHAR" property="parentProductName" /> <result column="adWfNodeName" jdbcType="VARCHAR" property="adWfNodeName" /> </resultMap>
sql查询语句:
<select id="queryAll" resultMap="BaseResultMapExtral"> select rwc.* ,mp.name parentProductName,awn.name adWfNodeName from rccp_workcenter_capacity rwc,m_product mp,ad_wf_node awn where mp.m_product_id=rwc.parent_product_id and awn.ad_wf_node_id=rwc.ad_wf_node_id </select>