相关推荐recommended
mybatis的<sql>标签使用
作者:mmseoamin日期:2024-01-19

记录:421

场景:使用MyBatis的标签元素,把重复的SQL语句集中到标签,使用标签的id属性指定唯一标识。在等标签的id属性指定Java的方法名称。Java接口的方法和xml映射文件的内部的标签就完成了绑定关系。

2.使用标签元素

场景:标签元素在标签元素中定义,使用标签引用标签。

2.1Java接口

@Repository
public interface Label04SqlMapper {
  List queryCity01(CityLabelPO cityPO);
  List queryCity02(CityLabelPO cityPO);
  List queryCity03(CityLabelPO cityPO);
}

2.2Java接口映射的xml文件




  
      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
  
  
  
  

3.测试

3.1测试代码

@Slf4j
@RestController
@RequestMapping("/hub/example/cityLabel")
public class CityLabelController {
  @Autowired
  private Label04SqlMapper label04SqlMapper;
  @GetMapping("/load04")
  public Object load04() {
    log.info("测试开始...");
    CityLabelPO cityPO = CityLabelPO.builder().cityId(3L).build();
    // 示例一
    List list01 = label04SqlMapper.queryCity01(cityPO);
    // 示例二
    cityPO = CityLabelPO.builder().cityName("上海").build();
    list01 = label04SqlMapper.queryCity02(cityPO);
    // 示例三
    cityPO = CityLabelPO.builder().cityName("杭州").build();
    list01 = label04SqlMapper.queryCity03(cityPO);
    log.info("测试结束...");
    return "执行成功";
  }
}

3.2测试请求

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

3.3执行SQL

示例使用标签后,根据不同条件组装不同查询的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",
  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_NAME = ?

示例三:

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_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;
}

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日