mybatais中<set>标签作用
时间: 2023-05-30 07:04:17 浏览: 57
<set>标签在Mybatis中用于设置一个变量,可以在后续的SQL语句中引用。常用于动态SQL语句中,可以根据条件不同设置不同的变量值。例如:
```
<select id="getUserList" parameterType="map" resultType="User">
select * from user
<where>
<if test="name != null">
and name = #{name}
</if>
<if test="age != null">
and age = #{age}
</if>
<if test="sex != null">
and sex = #{sex}
</if>
<set var="orderByClause">
<if test="orderBy != null and orderBy != ''">
order by #{orderBy}
</if>
<if test="orderBy == null or orderBy == ''">
order by id desc
</if>
</set>
</where>
${orderByClause}
</select>
```
在这个例子中,<set>标签设置了一个变量orderByClause,根据orderBy参数的不同可以设置不同的排序方式。最后将变量插入到SQL语句中,完成动态SQL的构建。
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)