MyBatis實現連結查詢的一種方法
阿新 • • 發佈:2019-02-11
現象:
在mybatis查詢中有時候需要查詢一張表,然後在通過這個查詢出來的欄位去連結另外一張表進行查詢!
解決辦法:
這裡通過sql查詢了一個物件,在通過這個物件中的id去查詢了另外一張表與這個id對應的資料集合!實現了物件中有集合的一種查詢!
2:實現呼叫儲存過程查詢資料!然後通過查詢出來的某個欄位去另外一張表中查詢某個欄位!需要欄位對應。通過儲存過程將可以實現分頁查詢!
<mapper namespace="com.oig.dao.AppUserDao">
<parameterMap type="java.util.Map" id="callPageParams" >
<parameter property="table" jdbcType="NVARCHAR" mode="IN"/>
<parameter property="fields" jdbcType="NVARCHAR" mode="IN"/>
<parameter property="pageSize" jdbcType="INTEGER" mode="IN"/>
<parameter property="pageIndex" jdbcType="INTEGER" mode="IN"/>
<parameter property ="pages" jdbcType="INTEGER" mode="OUT"/>
<parameter property="total" jdbcType="INTEGER" mode="OUT"/>
<parameter property="order_by" jdbcType="NVARCHAR" mode="IN"/>
<parameter property="else_if" jdbcType="NVARCHAR" mode="IN"/>
<parameter property="primaryKey" jdbcType="NVARCHAR" mode="IN"/>
</parameterMap>
<resultMap type="com.oig.bean.AppUserLoginInfo" id="UserLoginInfo">
<result property="tcode" column="tcode"/>
<result property="wcode" column="wcode"/>
<result property="usercode" column="usercode"/>
<result property="ip" column="ip"/>
<result property="device" column="device"/>
<result property="lastlogin" column="lastlogin"/>
<result property="logintimes" column="logintimes"/>
<result property="enddate" column="enddate"/>
<result property="token" column="token"/>
<association property="wname" column="wcode" select="findWname">
<result property="wname" column="wname"/>
</association>
</resultMap>
<select id="findUserInfo" parameterMap="callPageParams" resultMap="UserLoginInfo" statementType="CALLABLE">
{call dbo.P_Public_select(?,?,?,?,?,?,?,?,?)}
</select>
<select id="findWname" parameterType="java.lang.String" resultType="String">
select wname from wcode where wcode=#{wcode};
</select>
</mapper>