I have make sql query by MyBatis and MariaDB in Spring.
This is my failed MyBatis codes.
<resultMap type="Member" id="memberMap">
<result property="uid" column="uid" />
<result property="id" column="id" />
<result property="name" column="name" />
<collection property="privacyList" column="{ mid = uid, type = 'phone'}" ofType="MemPhone" select="getPrivacyList" />
</resultMap>
<resultMap type="MemPrivacy" id="memPrivacyMap">
<result property="uid" column="uid" />
<result property="mid" column="mid" />
<result property="type" column="type" />
<result property="name" column="name" />
<result property="content" column="content" />
<result property="sort" column="sort" />
</resultMap>
<select id="getMemberList" resultMap="memberMap">
select * from `member`
</select>
<select id="getPrivacyList" resultMap="memPrivacyMap" parameterType="map">
select *
from `mem_privacy`
where `mid` = #{ mid } and `type` = #{ type }
</select>
Because column have to be defined as a column, not a string in line 4.
So, i changed this as followed code.
select *, 'phone' as `type_phone`
from `mem_privacy`
where `mid` = #{ mid } and `type` = #{ type }
<collection property="privacyList" column="{ mid = uid, type = type_phone }" ofType="MemPhone" select="getPrivacyList" />
Select string value as column and use it to collection column.
Changed codes was ran well.
But, i like to use just string in there.
How can i this?
Somebody help me~~
in your resultMap
with the name memberMap
, you tell myBatis, to 'build' the collection
for Member#privacyList
via the select referred by select="getPrivacyList"
the column
-property, passes the content of the declared columns as parameters into the referred select.
in your case ( column="{ mid = uid, type = type_phone }"
) content of column uid as parameter named mid and content of column type_phone as parameter type. the corresponding contents are available as #{mid}
/ ${mid}
and #{type}
/ ${type}
in your 'sub-query'.
if you want to pass a specified String into the select-statement, you have to add a column to your getMemberList
-select-statement
SELECT member.*, 'phone' AS desired_type FROM MEMBER
now you can pass this columns content, that is a String, into your sub-query via `column="{ mid = uid, desired_type = desired_type}".
i suggest to not use sql or database key-words, as type
, for column-names
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.