当前位置:   article > 正文

mybatis的<choose>标签使用_mybatis choose

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中如下:

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

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

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

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

1.2MyBatis的使用

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

  1. mybatis:
  2. 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接口

  1. @Repository
  2. public interface Label02ChooseMapper {
  3. List<CityLabelPO> queryCity(CityLabelDTO cityLabelDTO);
  4. }

2.2Java接口映射的xml文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.hub.example.mapper.Label02ChooseMapper">
  4. <select id="queryCity" parameterType="com.hub.example.domain.CityLabelDTO"
  5. resultType="com.hub.example.domain.CityLabelPO">
  6. select CITY_ID AS "cityId",
  7. <choose>
  8. <when test="flagColumn !=null and flagColumn !='' and flagColumn == '01' ">
  9. CITY_NAME AS "cityName",
  10. LAND_AREA AS "landArea",
  11. POPULATION AS "population",
  12. GROSS AS "gross",
  13. CITY_DESCRIBE AS "cityDescribe",
  14. DATA_YEAR AS "dataYear",
  15. UPDATE_TIME AS "updateTime"
  16. </when>
  17. <when test="flagColumn !=null and flagColumn !='' and flagColumn == '02' ">
  18. CITY_NAME AS "cityName",
  19. LAND_AREA AS "landArea"
  20. </when>
  21. <otherwise>
  22. CITY_NAME AS "cityName",
  23. LAND_AREA AS "landArea",
  24. POPULATION AS "population",
  25. GROSS AS "gross"
  26. </otherwise>
  27. </choose>
  28. from
  29. <choose>
  30. <when test="flagFrom !=null and flagFrom !='' and flagFrom == '01' ">
  31. t_city
  32. </when>
  33. <otherwise>
  34. t_city_01
  35. </otherwise>
  36. </choose>
  37. aa
  38. where
  39. <choose>
  40. <when test="cityId !=null and cityId !='' and flagWhere == '01' ">
  41. aa.CITY_ID = #{cityId}
  42. </when>
  43. <when test="cityName !=null and cityName !='' and flagWhere == '02' ">
  44. aa.CITY_NAME = #{cityName}
  45. </when>
  46. </choose>
  47. </select>
  48. </mapper>

3.测试

3.1测试代码

  1. @Slf4j
  2. @RestController
  3. @RequestMapping("/hub/example/cityLabel")
  4. public class CityLabelController {
  5. @Autowired
  6. private Label02ChooseMapper label02ChooseMapper;
  7. @GetMapping("/load02")
  8. public Object load02() {
  9. log.info("测试开始...");
  10. // 示例一
  11. CityLabelDTO labelDTO = CityLabelDTO.builder()
  12. .cityId(1L).flagColumn("01")
  13. .flagFrom("01").flagWhere("01").build();
  14. List<CityLabelPO> labelPOList = label02ChooseMapper.queryCity(labelDTO);
  15. // 示例二
  16. labelDTO = CityLabelDTO.builder()
  17. .cityName("上海").flagColumn("02")
  18. .flagFrom("02").flagWhere("02").build();
  19. labelPOList = label02ChooseMapper.queryCity(labelDTO);
  20. log.info("测试结束...");
  21. return "执行成功";
  22. }
  23. }

3.2测试请求

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

3.3执行SQL

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

示例一:

  1. SELECT
  2. CITY_ID AS "cityId",
  3. CITY_NAME AS "cityName",
  4. LAND_AREA AS "landArea",
  5. POPULATION AS "population",
  6. GROSS AS "gross",
  7. CITY_DESCRIBE AS "cityDescribe",
  8. DATA_YEAR AS "dataYear",
  9. UPDATE_TIME AS "updateTime"
  10. FROM
  11. t_city aa
  12. WHERE aa.CITY_ID = ?;

示例二:

  1. SELECT
  2. CITY_ID AS "cityId",
  3. CITY_NAME AS "cityName",
  4. LAND_AREA AS "landArea"
  5. FROM
  6. t_city_01 aa
  7. WHERE aa.CITY_NAME = ?;

4.支撑

4.1实体对象

(1)封装结果对象CityLabelPO

  1. @Data
  2. @Builder
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. public class CityLabelPO {
  6. private Long cityId;
  7. private String cityName;
  8. private Double landArea;
  9. private Long population;
  10. private Double gross;
  11. private String cityDescribe;
  12. private String dataYear;
  13. private Date updateTime;
  14. }

(2)封装参数CityLabelDTO

  1. @Data
  2. @Builder
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. public class CityLabelDTO {
  6. private Long cityId;
  7. private String cityName;
  8. private Double landArea;
  9. private Long population;
  10. private Double gross;
  11. private String cityDescribe;
  12. private String dataYear;
  13. private Date updateTime;
  14. // 标记查询的字段
  15. private String flagColumn;
  16. // 标记查询的表
  17. private String flagFrom;
  18. // 标记where条件
  19. private String flagWhere;
  20. }

4.2建表语句

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

以上,感谢。

2023年4月23日

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/651577
推荐阅读
相关标签
  

闽ICP备14008679号