mybatis association query (same name field problem)

When using pagehelper in mybatis, if you encounter multi-table queries, especially associated queries (one-to-one, one-to-many),

Result set (use tags directly (this kind of SQL can only use tags in the collection for the association, and can not use the query or tags like the association condition push-in type))

The +sql (select o.*,r.* opm_User o left join opm_user_role ur on o.id = ur.userid) statement reflects the association, it is easy to encounter, the associated entity to be checked

Having the same column name will cause (1, only one one-to-many-to-many party (when the primary key name is the same)) 2. When pagehelper is used at the same time, there will be a problem of clearly defining the column in the text ( tmp_page.* here

Equivalent to having the same column), you can use aliases at this time

 

select *

  from (select tmp_page.*, rownum row_id

          from (select o.*,

                       r.id as r_id,

                       r.name as  aa,

                       r.organid as ff,

                       r.valid as fdf,

                       r.memo as f,

                       r.level1 as d,

                       r.subsystem as g

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

                 where 1 = 1 and o.organId = 1

                 order by o.code) tmp_page

         where rownum <= 20)

 where row_id > 0

 

 E.g:

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

    <!--

      WARNING - @mbggenerated

    -->

    <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" >

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

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

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

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

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

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

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

    </collection>

  </resultMap>

  

  <sql id="getRole">

    r.id as r_id,r.name as r_name,r.organid as r_organid,r.valid as r_valid,r.memo as r_memo,r.level1 as r_level1,r.subsystem as r_subsystem

  </sql>

  

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

 select o.*, <include refid="getRole"></include>

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

      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>

 

  ============ This is the original database column

   <resultMap id="BaseResultMap" type="com.esteel.system.bean.OpmUser" >

    <!--

      WARNING - @mbggenerated

    -->

    <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" />

  </resultMap>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326547926&siteId=291194637