修改下面mybatis语句 select <choose> <when test="laborCostCity != null and laborCostCity !='' and serveCostType == null and serveCostType !=''"> ${laborCostCity} as laborCost,${serveCostType} as serveCost </when> </choose> from t_grading_new where `rank`=#{rank} and delFlag =0
时间: 2024-02-10 11:18:29 浏览: 31
根据你的需求,我修改了下面的MyBatis语句:
```
<select>
<choose>
<when test="laborCostCity != null and laborCostCity !='' and serveCostType != null and serveCostType !=''">
SELECT ${laborCostCity} AS laborCost, ${serveCostType} AS serveCost
</when>
<otherwise>
SELECT laborCost, serveCost
</otherwise>
</choose>
FROM t_grading_new
WHERE `rank` = #{rank}
AND delFlag = 0
</select>
```
这个语句可以根据条件动态选择要查询的字段,如果`laborCostCity`和`serveCostType`都非空,则查询这两个字段。否则,查询全部字段`laborCost`和`serveCost`。同时,筛选出`rank`等于参数`#{rank}`并且`delFlag`等于0的记录。
相关问题
修改下面mybatis语句select <choose> <when test="laborCostCity != null and laborCostCity !=''"> ${laborCostCity} as laborCost </when> <when test="serveCostType == null and serveCostType !=''"> ${serveCostType} as serveCost </when> </choose> from t_grading_new
这段 MyBatis 语句存在一些问题,可以尝试如下修改:
```
<select>
<choose>
<when test="laborCostCity != null and laborCostCity !=''">
SELECT ${laborCostCity} AS laborCost
</when>
<when test="serveCostType != null and serveCostType !=''">
SELECT ${serveCostType} AS serveCost
</when>
<otherwise>
SELECT * <!-- 如果没有上述两种情况,则查询所有列 -->
</otherwise>
</choose>
FROM t_grading_new
</select>
```
修改后的语句添加了 `<select>` 标签,同时在 `<choose>` 标签中添加了 `<otherwise>` 标签,以处理没有满足 `<when>` 条件的情况。此外,还修改了 `<when>` 条件判断中的 `serveCostType == null`,将其改为 `serveCostType != null`,避免出现语法错误。
<if test="bid != null and bid != ''">怎么给else 为null
在 MyBatis 中,如果您使用 `<if>` 标签对参数进行判断,可以使用 `<otherwise>` 标签来定义 if 判断条件不成立时的默认值,如下所示:
```xml
<select id="queryData" parameterType="java.lang.String" resultType="com.example.entity.Data">
SELECT * FROM data_table
WHERE bid = #{bid}
<if test="bid != null and bid != ''">
<!-- 如果 bid 不为空,则执行此处的 SQL 语句 -->
</if>
<otherwise>
<!-- 如果 bid 为空,则执行此处的 SQL 语句 -->
AND data_name IS NULL
</otherwise>
</select>
```
在上述示例中,我们使用 `<if>` 标签对 bid 参数进行判断,如果 bid 不为空,则执行标签中的 SQL 语句。如果 bid 为空,则执行 `<otherwise>` 标签中的 SQL 语句。在本例中,我们将 bid 参数与 data_name 字段进行了比较,如果 bid 为空,则查询条件为 data_name IS NULL。
需要注意的是,在使用 `<otherwise>` 标签时,必须将其放置在 `<if>` 标签的后面。如果 `<otherwise>` 标签放置在 `<if>` 标签的前面,则默认值将会被忽略。同时,如果查询条件包含多个参数,可以使用 `<choose>` 标签来定义多个 `<when>` 标签,用于判断不同的条件并返回不同的结果。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)