当前位置:   article > 正文

mybatis的<if>标签使用_mybatis

mybatis

记录:417

场景:使用MyBatis的<if></if>标签的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.使用<if></if>标签元素

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

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

2.1Java接口

  1. @Repository
  2. public interface Label01IFMapper {
  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.Label01IFMapper">
  4. <select id="queryCity" parameterType="com.hub.example.domain.CityLabelDTO"
  5. resultType="com.hub.example.domain.CityLabelPO">
  6. select CITY_ID AS "cityId",
  7. <if test="flagColumn !=null and flagColumn !='' and flagColumn == '01' ">
  8. CITY_NAME AS "cityName",
  9. LAND_AREA AS "landArea",
  10. POPULATION AS "population",
  11. GROSS AS "gross",
  12. CITY_DESCRIBE AS "cityDescribe",
  13. DATA_YEAR AS "dataYear",
  14. UPDATE_TIME AS "updateTime"
  15. </if>
  16. <if test="flagColumn !=null and flagColumn !='' and flagColumn == '02' ">
  17. CITY_NAME AS "cityName",
  18. LAND_AREA AS "landArea",
  19. POPULATION AS "population",
  20. GROSS AS "gross"
  21. </if>
  22. from
  23. <if test="flagFrom !=null and flagFrom !='' and flagFrom == '01' ">
  24. t_city
  25. </if>
  26. <if test="flagFrom !=null and flagFrom !='' and flagFrom == '02' ">
  27. t_city_01
  28. </if>
  29. aa
  30. where
  31. <if test="cityId !=null and cityId !='' and flagWhere == '01' ">
  32. aa.CITY_ID = #{cityId}
  33. </if>
  34. <if test="cityName !=null and cityName !='' and flagWhere == '02' ">
  35. aa.CITY_NAME = #{cityName}
  36. </if>
  37. </select>
  38. </mapper>

3.测试

3.1测试代码

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

3.2测试请求

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

3.3执行SQL

示例使用<if></if>标签后,根据不同条件组装不同查询的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. POPULATION AS "population",
  6. GROSS AS "gross"
  7. FROM
  8. t_city_01 aa
  9. 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/小蓝xlanll/article/detail/255714
推荐阅读
相关标签
  

闽ICP备14008679号