demand

SELECT
    id ,
    gender ,
    nickname ,
    mobile ,
    avatar
  FROM
    dts_user
  WHERE
    gender = 1
  AND mobile LIKE '%56%'
Copy the code

The query results

Mybatis dynamic SQL configuration

Gender is fixed, fuzzy query mobile is changed to dynamic configuration

<! ParameterType ="user" resultType="user"> select id, resultType="user", gender ,nickname ,mobile ,avatar From dts_user WHERE gender = 1 AND mobile LIKE '%${mobile}%' </select>Copy the code

Step 2 >> Query condition gender (only search user data with gender 0 or 1) and fuzzy query mobile have been changed to dynamic configuration

<! ParameterType ="user" resultType="user"> select id, resultType="user", gender ,nickname ,mobile ,avatar From dts_user WHERE gender = #{gender} AND mobile LIKE '%${mobile}%' </select>Copy the code

Step 3 >>if tag to determine, optionally to use the query criteria

<! ParameterType ="user" resultType="user"> select id, resultType="user", gender ,nickname ,mobile ,avatar From dts_user WHERE 1=1 <if test="gender ! = null and gender ! = ''"> AND gender = #{gender} </if> <if test="mobile ! = null and mobile ! = ''"> AND mobile LIKE '%${mobile}%' </if> </select>Copy the code

Gender and USERNAME data are not passed when all user information is queried

<! ParameterType ="user" resultType="user"> select id, resultType="user", gender ,nickname ,mobile ,avatar From dts_user <! --> <where> <if test="gender! = null"> AND gender = #{gender} </if> <if test="mobile ! = null and mobile ! = ''"> AND mobile LIKE '%${mobile}%' </if> </where> </select>Copy the code

An example of real complexity provides only SQL

@Select(" <script> " + " select novel.id, novel.title, novel.cover_img, novel.author, novel.intro, novel.score, novel.last_chapter_name, novel.lately_follower, novel.total_follower, novel.retention_ratio, novel.last_chapter_name, novel.category_id, novel.category_name " + " from novel_t as novel inner join novel_category_t as cat on novel.category_id = cat.id " + " <where> novel.remove = 0 and novel.sell_status='UP' " + " <choose>" + " <when test='minCatList! =null and minCatList.size() > 0'>" + " and novel.category_id in " + " <foreach collection=\"minCatList\" item=\"item\" index=\"index\" open=\"(\" separator=\",\" close=\")\"> " + " #{item.id} " + " </foreach>" + " </when> " + " <otherwise>" + " and novel.category_id=#{majorId} " + " </otherwise>" + " </choose>" + " <if test='genderChannel! =\"\" and genderChannel! =null'> " + " and cat.gender_channel=#{genderChannel} " + " </if> " + " <if test='status! =\"\" and status! =\"all\"'> " + " and novel.serial_status=#{status} " + " </if> " + " </where> " + " order by novel.last_modified desc " + " </script> "Copy the code

Click the card below/wechat search, follow the public account “Tianyu Creative Music” (ID: GH_CC865e4C536b)

Tianyu Creative music school

Personal subscription number mainly provides: sharing the latest information IT tutorial free novel platform

20 original articles

The public,

This article uses the article synchronization assistant to synchronize