Records : 421
Scenario : Use the <sql></sql> tag element of MyBatis to gather repeated SQL statements into the <sql> tag, and use the tag's id attribute to specify a unique identifier. Use <include></include> tags in <insert><update><delete><select> and other tags to refer to <sql></sql> tags.
Versions : JDK 1.8, Spring Boot 2.6.3, mybatis-3.5.9.
1. Basic knowledge
1.1 MyBatis label
(1) View the tags supported by MyBatis
Address: http://mybatis.org/dtd/mybatis-3-mapper.dtd
(2) View label usage
Take the <mapper></mapper> tag element as an example, as follows in mybatis-3-mapper.dtd:
<!ELEMENT mapper (cache-ref | cache | resultMap* | parameterMap* | sql* | insert* | update* | delete* | select* )+>
<!ATTLIST mapper
namespace CDATA #IMPLIED
>
<!ELEMENT mapper(...)+>, indicating that this is a label element mapper.
(..|insert*|update* | delete* | select*), indicating the list of elements that can be nested in the mapper element.
<!ATTLIST mapper>, indicating that this is a supported attribute of an element tag.
1.2 Use of MyBatis
(1) Configure the location of the xml file mapped by mybatis in the application.yml configuration file.
mybatis:
mapper-locations: classpath*:mapper/**/*.xml
(2) Create a Java interface. Add methods to the interface.
(3) Create a Java interface mapping xml file. Use the namespace attribute of the <mapper></mapper> tag in xml to specify the full path of the Java interface. The Java interface and the xml mapping file complete the binding relationship.
(4) In the <mapper></mapper> tag, use the id attribute of the tags such as <insert><update><delete><select> to specify the Java method name. The method of the Java interface and the tags inside the <mapper></mapper> of the xml mapping file complete the binding relationship.
2. Use the <sql></sql> tag element
Scenario : The <sql></sql> tag element is defined in the <mapper> tag element, use the <include></include> tag to refer to the <sql> tag.
2.1 Java interface
@Repository
public interface Label04SqlMapper {
List<CityLabelPO> queryCity01(CityLabelPO cityPO);
List<CityLabelPO> queryCity02(CityLabelPO cityPO);
List<CityLabelPO> queryCity03(CityLabelPO cityPO);
}
2.2 XML file for Java interface mapping
<?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.Label04SqlMapper">
<sql id="selectSql">
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
</sql>
<select id="queryCity01" resultType="com.hub.example.domain.CityLabelPO">
<include refid="selectSql"></include>
WHERE aa.CITY_ID = #{cityId}
</select>
<select id="queryCity02" resultType="com.hub.example.domain.CityLabelPO">
<include refid="selectSql"></include>
WHERE aa.CITY_NAME = #{cityName}
</select>
<select id="queryCity03" resultType="com.hub.example.domain.CityLabelPO">
<include refid="selectSql"></include>
<where>
<if test="cityName !=null and cityName !='' ">
aa.CITY_NAME = #{cityName}
</if>
</where>
</select>
</mapper>
3. Test
3.1 Test code
@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<CityLabelPO> 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 Test request
URL:http://127.0.0.1:18080/hub-example/hub/example/cityLabel/load04
3.3 Execute SQL
The example uses the <sql></sql> tag to assemble SQL for different queries according to different conditions to adapt to different business scenarios.
Example one:
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 = ?
Example two:
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 = ?
Example three:
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. Support
4.1 Entity objects
(1) Encapsulate the result object 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 statement
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='城市信息表';
Above, thanks.
April 23, 2023