在实际项目中,经常会用到多表关联查询。比如常见的多对一,一对多等。在MyBatis中是这样使用的。

<resultMap id="BaseResultMap" type="cloud.mobe.cms.dto.JobDto" >
   <id column="j_id" property="jId" jdbcType="VARCHAR" />
   <result column="j_title" property="jTitle" jdbcType="VARCHAR" />
   <result column="j_belong" property="jBelong" jdbcType="VARCHAR" />
   <result column="j_type" property="jType" jdbcType="VARCHAR" />
   <result column="j_content" property="jContent" jdbcType="LONGVARCHAR" />
   <association property="jobBelong" javaType="com.lssrc.cms.entity.JobBelong">
      <id column="jb_id" property="jbId" jdbcType="VARCHAR" />
      <result column="jb_name" property="jbName" jdbcType="VARCHAR" />
   </association>
   <association property="jobType" javaType="com.lssrc.cms.entity.JobType">
      <id column="jt_id" property="jtId" jdbcType="VARCHAR" />
      <result column="jt_name" property="jtName" jdbcType="VARCHAR" />
   </association>
</resultMap>

在SQL中这样写就可以:

<select id="selectByPage" resultMap="BaseResultMap">
   select
   *
   from shsj_job, shsj_job_belong, shsj_job_type
   where
   shsj_job.j_type=shsj_job_type.jt_id and
   shsj_job.j_belong=shsj_job_belong.jb_id
   order by j_id desc
   limit #{start},#{pageSize}
</select>

感谢大家的阅读, 如有疑问可以加我微信