Mybatisループ処理like

2904 ワード

Mybatisループ処理like
ユーザーテーブル
user_id
preference_tag
nick
123456
java,python
余喜
書籍表
plan_id
tag
10042
java,c++,python
10043
ruby,python,c
10044
android,ios
需要:ユーザーの好みのラベルに基づいて、それに対応する書籍を探し出します
上の図に戻るべきだ
plan_id
tag
10042
java,c++,python
10043
ruby,python,c
ユーザー・テーブルのpreferenceを解決するための考え方tagクエリーを取り出し、リストセットに入れ、書籍テーブルでlikeクエリーをループし、重複するデータを削除すればよい
    SELECT plan_id FROM push_plan WHERE 1=0
        UNION DISTINCT
        <if test="tags != null">
            "item" index="index" collection="tags">
                SELECT plan_id FROM push_plan
                WHERE tag LIKE CONCAT('%',#{item},'%')
                UNION DISTINCT
            
        if>
        SELECT plan_id FROM push_plan WHERE 1=0

主にmybatisのforeachメソッドを利用しており、inも同様に使用できますが、コードを変更する必要があります.
 <where>
            <if test="followingPlanIds != null">
                plan_id in
                <foreach item="item" index="index" collection="followingPlanIds"
                         open="(" separator="," close=")">#{item}
                foreach>
            if>
        where>

次のコードにはopen="("separator=","close=")が含まれていることに注意してください.in(xxx,xxx,xxx)が必要です.