## 动态 SQL（MyBatis 风格）

在三引号 """...""" SQL 中使用 XML 标签实现动态条件。

### if 标签
```magicscript
return db.select("""
    select * from user
    <where>
        <if test="name != null and name != ''">
            and name like concat('%', #{name}, '%')
        </if>
        <if test="status != null">
            and status = #{status}
        </if>
        <if test="minAge != null">
            and age >= #{minAge}
        </if>
    </where>
    order by create_time desc
""")
```

### if / elseif / else
```magicscript
return db.select("""
    select * from user where
    <if test="type == 'vip'">
        level >= 3
    </if>
    <elseif test="type == 'normal'">
        level >= 1
    </elseif>
    <else>
        1 = 1
    </else>
""")
```

### where 标签
自动处理第一个 AND/OR，避免 WHERE AND 语法错误：
```magicscript
return db.select("""
    select * from user
    <where>
        <if test="name != null">and name = #{name}</if>
        <if test="age != null">and age = #{age}</if>
    </where>
""")
```

### set 标签（更新）
自动处理末尾多余逗号：
```magicscript
return db.update("""
    update user
    <set>
        <if test="name != null">name = #{name},</if>
        <if test="age != null">age = #{age},</if>
        <if test="status != null">status = #{status},</if>
    </set>
    where id = #{id}
""")
```

### foreach 标签（批量）
```magicscript
var idList = [1, 2, 3, 4, 5]
return db.select("""
    select * from user
    where id in
    <foreach collection="idList" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
""")
```

### 综合示例：条件搜索 + 分页
```magicscript
return db.page("""
    select u.*, d.dept_name
    from user u
    left join department d on d.id = u.dept_id
    <where>
        <if test="keyword != null and keyword != ''">
            and (u.name like concat('%',#{keyword},'%')
                 or u.phone like concat('%',#{keyword},'%'))
        </if>
        <if test="deptId != null">
            and u.dept_id = #{deptId}
        </if>
        <if test="startDate != null">
            and u.create_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and u.create_time &lt;= #{endDate}
        </if>
    </where>
    order by u.create_time desc
""")
```