Mybaits two kinds of related queries

There are two types of related queries:

1. Get it done with a general sql

This association is reflected in the use of tags in collect, the main entity is queried in sql, and the associated entity (because in one sql, note that the field names in the two entities (query result sets) cannot be the same)

, the same will cause problems in automatically assembled statements such as pagehelper (the column is not clearly defined), and if the id is the same, only one will be found by multiple parties)

 

See the previous article for this method, the problem of associated query fields with the same name

 

2. Use sub-queries. In the collection, sql sub-queries are used instead of labels. Another general query only queries the main body, and their associations are reflected in the sql of the sub-queries.

  1, connect, 2 condition column push

<resultMap id="BaseResultMapr" type="com.esteel.system.bean.OpmRole" >

    <!--

      WARNING - @mbggenerated

    -->

    <id column="ID" property="id" jdbcType="VARCHAR" />

    <result column="NAME" property="name" jdbcType="VARCHAR" />

    <result column="ORGANID" property="organid" jdbcType="VARCHAR" />

    <result column="VALID" property="valid" jdbcType="VARCHAR" />

    <result column="MEMO" property="memo" jdbcType="VARCHAR" />

    <result column="LEVEL1" property="level1" jdbcType="DECIMAL" />

    <result column="SUBSYSTEM" property="subsystem" jdbcType="VARCHAR" />

  </resultMap>

  

  <resultMap id="BaseResultMapVo" type="com.esteel.system.beanVo.OpmUserVo" >

    <id column="ID" property="id" jdbcType="VARCHAR" />

    <result column="CODE" property="code" jdbcType="VARCHAR" />

    <result column="PASSWORD" property="password" jdbcType="VARCHAR" />

    <result column="NAME" property="name" jdbcType="VARCHAR" />

    <result column="TELEPHONE" property="telephone" jdbcType="VARCHAR" />

    <result column="EMAIL" property="email" jdbcType="VARCHAR" />

    <result column="ORGANID" property="organid" jdbcType="VARCHAR" />

    <result column="VALID" property="valid" jdbcType="VARCHAR" />

    <result column="MEMO" property="memo" jdbcType="VARCHAR" />

    <result column="LEVEL1" property="level1" jdbcType="DECIMAL" />

    <result column="LAST_LOGIN_DATE" property="lastLoginDate" jdbcType="VARCHAR" />

    <result column="LAST_LOGIN_TIME" property="lastLoginTime" jdbcType="TIMESTAMP" />

    <result column="ONLINE_MARK" property="onlineMark" jdbcType="VARCHAR" />

    <result column="ONLINE_IP" property="onlineIp" jdbcType="VARCHAR" />

    <result column="SESSIONID" property="sessionid" jdbcType="VARCHAR" />

    <collection property="opmRole" ofType="com.esteel.system.bean.OpmRole" select="getr" column="{uId=ID}">

    </collection>

  </resultMap>

   <select id="getr" resultMap="BaseResultMapr">

  select r.* from opm_role r join opm_user_role ur on ur.roleid = r.id  join opm_User o on o.id = ur.userid  

  and o.id=#{uId}

  </select>

  

  <select id="getUserByMarkId" parameterType="map" resultMap="BaseResultMapVo">

 select o.*

       from opm_User o 

      where 1 = 1

 <if test="organid!=null and organid!=''"> 

      and o.organId = # {organs} 

 </if>

 <if test="valid!=null and valid!=''"> 

       and o.valid=#{valid} 

 </if>

 <if test="level1!=null and level1!=''"> 

      and o.LEVEL1=#{level1}

 </if>

  order by o.code

  </select>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326558098&siteId=291194637