mybatis的<choose>标签使用

记录:418

场景:使用MyBatis的<choose></choose>标签的<when></when>标签的test属性,对入参属性的值做非null(!=null)、非空(!='')判断,或者与常量做相等(==)或者不等判断(!=)。

版本:JDK 1.8,Spring Boot 2.6.3,mybatis-3.5.9。

1.基础知识

1.1MyBatis的标签

(1)查看MyBatis支持的标签

地址:http://mybatis.org/dtd/mybatis-3-mapper.dtd

(2)查看标签使用

以<mapper></mapper>标签元素为例,在mybatis-3-mapper.dtd中如下:

<!ELEMENT mapper (cache-ref | cache | resultMap* | parameterMap* | sql* | insert* | update* | delete* | select* )+>
<!ATTLIST mapper
namespace CDATA #IMPLIED
>

<!ELEMENT mapper(...)+>,表示这是一个标签元素mapper.

(..| insert*| update* | delete* | select*),表示在mapper元素中可以嵌套使用的元素清单。

<!ATTLIST mapper>,表示这是一个元素标签的支持的属性。

1.2MyBatis的使用

(1)在application.yml配置文件配置mybatis映射的xml文件位置。

mybatis:
  mapper-locations: classpath*:mapper/**/*.xml

(2)创建一个Java接口。在接口中添加方法。

(3)创建一个Java接口映射的xml文件。在xml中使用<mapper></mapper>标签的namespace属性指定Java接口的全路径。Java接口和xml映射文件就完成了绑定关系。

(4)在<mapper></mapper>标签内,使用<insert><update><delete><select>等标签的id属性指定Java的方法名称。Java接口的方法和xml映射文件的<mapper></mapper>内部的标签就完成了绑定关系。

2.使用<choose></choose>标签元素

场景:<choose></choose>标签元素在<insert><update><delete><select>等标签元素内使用。

一般组合:<choose><when></when><otherwise></otherwise></choose>

一般组合:<choose><when></when><when></when></choose>

示例功能:示例使用<choose></choose>标签,根据不同条件组装不同查询的SQL。

2.1Java接口

@Repository
public interface Label02ChooseMapper {
  List<CityLabelPO> queryCity(CityLabelDTO cityLabelDTO);
}

2.2Java接口映射的xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hub.example.mapper.Label02ChooseMapper">
  <select id="queryCity" parameterType="com.hub.example.domain.CityLabelDTO"
        resultType="com.hub.example.domain.CityLabelPO">
    select CITY_ID AS "cityId",
    <choose>
        <when test="flagColumn !=null and flagColumn !='' and flagColumn == '01' ">
            CITY_NAME AS "cityName",
            LAND_AREA AS "landArea",
            POPULATION AS "population",
            GROSS AS "gross",
            CITY_DESCRIBE AS "cityDescribe",
            DATA_YEAR AS "dataYear",
            UPDATE_TIME AS "updateTime"
        </when>
        <when test="flagColumn !=null and flagColumn !='' and flagColumn == '02' ">
            CITY_NAME AS "cityName",
            LAND_AREA AS "landArea"
        </when>
        <otherwise>
            CITY_NAME AS "cityName",
            LAND_AREA AS "landArea",
            POPULATION AS "population",
            GROSS AS "gross"
        </otherwise>
    </choose>
    from
    <choose>
        <when test="flagFrom !=null and flagFrom !='' and flagFrom == '01' ">
            t_city
        </when>
        <otherwise>
            t_city_01
        </otherwise>
    </choose>
    aa
    where
    <choose>
        <when test="cityId !=null and cityId !='' and flagWhere == '01' ">
            aa.CITY_ID = #{cityId}
        </when>
        <when test="cityName !=null and cityName !='' and flagWhere == '02' ">
            aa.CITY_NAME = #{cityName}
        </when>
    </choose>
  </select>
</mapper>

3.测试

3.1测试代码

@Slf4j
@RestController
@RequestMapping("/hub/example/cityLabel")
public class CityLabelController {
  @Autowired
  private Label02ChooseMapper label02ChooseMapper;
  @GetMapping("/load02")
  public Object load02() {
    log.info("测试开始...");
    // 示例一
    CityLabelDTO labelDTO = CityLabelDTO.builder()
            .cityId(1L).flagColumn("01")
            .flagFrom("01").flagWhere("01").build();
    List<CityLabelPO> labelPOList = label02ChooseMapper.queryCity(labelDTO);
    // 示例二
    labelDTO = CityLabelDTO.builder()
            .cityName("上海").flagColumn("02")
            .flagFrom("02").flagWhere("02").build();
    labelPOList = label02ChooseMapper.queryCity(labelDTO);
    log.info("测试结束...");
    return "执行成功";
  }
}

3.2测试请求

URL:http://127.0.0.1:18080/hub-example/hub/example/cityLabel/load02

3.3执行SQL

示例使用<choose></choose>标签后,根据不同条件组装不同查询的SQL,适配不同业务场景。

示例一:

SELECT
  CITY_ID AS "cityId",
  CITY_NAME AS "cityName",
  LAND_AREA AS "landArea",
  POPULATION AS "population",
  GROSS AS "gross",
  CITY_DESCRIBE AS "cityDescribe",
  DATA_YEAR AS "dataYear",
  UPDATE_TIME AS "updateTime"
FROM
  t_city aa
WHERE aa.CITY_ID = ?;

示例二:

SELECT
  CITY_ID AS "cityId",
  CITY_NAME AS "cityName",
  LAND_AREA AS "landArea"
FROM
  t_city_01 aa
WHERE aa.CITY_NAME = ?;

4.支撑

4.1实体对象

(1)封装结果对象CityLabelPO

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CityLabelPO {
  private Long cityId;
  private String cityName;
  private Double landArea;
  private Long population;
  private Double gross;
  private String cityDescribe;
  private String dataYear;
  private Date updateTime;
}

(2)封装参数CityLabelDTO

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CityLabelDTO {
  private Long cityId;
  private String cityName;
  private Double landArea;
  private Long population;
  private Double gross;
  private String cityDescribe;
  private String dataYear;
  private Date updateTime;
  // 标记查询的字段
  private String flagColumn;
  // 标记查询的表
  private String flagFrom;
  // 标记where条件
  private String flagWhere;
}

4.2建表语句

CREATE TABLE t_city (
  CITY_ID BIGINT(16) NOT NULL COMMENT '唯一标识',
  CITY_NAME VARCHAR(64) COLLATE utf8_bin NOT NULL COMMENT '城市名',
  LAND_AREA DOUBLE DEFAULT NULL COMMENT '城市面积',
  POPULATION BIGINT(16) DEFAULT NULL COMMENT '城市人口',
  GROSS DOUBLE DEFAULT NULL COMMENT '生产总值',
  CITY_DESCRIBE VARCHAR(512) COLLATE utf8_bin DEFAULT NULL COMMENT '城市描述',
  DATA_YEAR VARCHAR(16) COLLATE utf8_bin DEFAULT NULL COMMENT '数据年份',
  UPDATE_TIME DATETIME DEFAULT NULL COMMENT '更新时间'
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='城市信息表';

以上,感谢。

2023年4月23日

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
)">
< <上一篇
下一篇>>