Records : 413
Scenario : The parameterType of MyBatis passes in the parameter type and resultType returns the result type.
Versions : JDK 1.8, Spring Boot 2.6.3, mybatis-3.5.9.
1. The incoming parameter parameterType is Integer
Incoming parameter type parameterType: java.lang.Integer.
Return result type resultType: java.lang.String.
1.1 Mapper interface code
@Repository
public interface CityTypeMapper {
String getCity01(Integer cityId);
}
1.2XML code
<?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.CityTypeMapper">
<select id="getCity01" parameterType="java.lang.Integer" resultType="java.lang.String">
select CITY_NAME FROM t_city WHERE CITY_ID=#{cityId}
</select>
</mapper>
2. The incoming parameter parameterType is Long
Incoming parameter type parameterType: java.lang.Long.
Return result type resultType: java.lang.String.
2.1 Mapper interface code
@Repository
public interface CityTypeMapper {
String getCity02(Long cityId);
}
2.2XML code
<?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.CityTypeMapper">
<select id="getCity02" parameterType="java.lang.Long" resultType="java.lang.String">
select CITY_NAME FROM t_city WHERE CITY_ID=#{cityId}
</select>
</mapper>
3. The incoming parameter parameterType is Double
Incoming parameter type parameterType: java.lang.Double.
Return result type resultType: java.lang.Double.
3.1 Mapper interface code
@Repository
public interface CityTypeMapper {
Double getCity03(Double gross);
}
3.2XML code
<?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.CityTypeMapper">
<select id="getCity03" parameterType="java.lang.Double" resultType="java.lang.Double">
select LAND_AREA FROM t_city WHERE GROSS=#{gross}
</select>
</mapper>
4. The incoming parameter parameterType is String
Incoming parameter type parameterType: java.lang.String.
Return result type resultType: java.lang.Long.
4.1Mapper interface code
@Repository
public interface CityTypeMapper {
Long getCity04(String cityName);
}
4.2XML code
<?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.CityTypeMapper">
<select id="getCity04" parameterType="java.lang.String" resultType="java.lang.Long">
select CITY_ID FROM t_city WHERE CITY_NAME=#{cityName}
</select>
</mapper>
5. The incoming parameter parameterType is List<Long>
Incoming parameter type parameterType: java.util.ArrayList.
Return result type resultType: java.lang.Long.
5.1Mapper interface code
@Repository
public interface CityTypeMapper {
long[] getCity05(List<Long> cityIdList);
}
5.2XML code
<?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.CityTypeMapper">
<select id="getCity05" parameterType="java.util.ArrayList" resultType="java.lang.Long" >
select POPULATION FROM t_city WHERE CITY_ID IN
<foreach collection="list" item="cityId" open="(" separator="," close=")">
#{cityId}
</foreach>
</select>
</mapper>
6. The incoming parameter parameterType is Long[]
Incoming parameter type parameterType: long[].
Return result type resultType: java.lang.String.
6.1 Mapper interface code
@Repository
public interface CityTypeMapper {
List<String> getCity06(Long[] cityIdList);
}
6.2XML code
<?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.CityTypeMapper">
<select id="getCity06" parameterType="long[]" resultType="java.lang.String">
select CITY_NAME FROM t_city WHERE CITY_ID IN
<foreach collection="array" item="cityId" open="(" separator="," close=")">
#{cityId}
</foreach>
</select>
</mapper>
7. The incoming parameter parameterType is Map<String, Object>
Incoming parameter type parameterType: java.util.Map.
Return result type resultType: com.hub.example.domain.CityPO.
7.1 Mapper interface code
@Repository
public interface CityTypeMapper {
CityPO getCity07(Map<String, Object> cityMap);
}
7.2XML code
<?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.CityTypeMapper">
<select id="getCity07" parameterType="java.util.Map" resultType="com.hub.example.domain.CityPO">
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
where CITY_ID = #{cityId}
AND CITY_NAME = #{cityName}
</select>
</mapper>
8. The incoming parameter parameterType is CityPO
Incoming parameter type parameterType: com.hub.example.domain.CityPO.
Return result type resultType: java.util.Map.
8.1Mapper interface code
@Repository
public interface CityTypeMapper {
Map<String, Object> getCity08(CityPO cityPO);
}
8.2XML code
<?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.CityTypeMapper">
<select id="getCity08" parameterType="com.hub.example.domain.CityPO" resultType="java.util.Map">
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
where CITY_ID = #{cityId}
AND CITY_NAME = #{cityName}
</select>
</mapper>
9. The incoming parameter parameterType is Date
Incoming parameter type parameterType: java.util.Date.
Return result type resultType: java.lang.String.
9.1Mapper interface code
@Repository
public interface CityTypeMapper {
String getCity09(Date date);
}
9.2XML code
<?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.CityTypeMapper">
<select id="getCity09" parameterType="java.util.Date" resultType="java.lang.String">
select CITY_NAME
FROM t_city
where UPDATE_TIME = #{date}
</select>
</mapper>
10. The incoming parameter parameterType is List<Long>
Incoming parameter type parameterType: java.util.ArrayList.
Return result type resultType: com.hub.example.domain.CityPO.
10.1 Mapper interface code
@Repository
public interface CityTypeMapper {
List<CityPO> getCity10(List<Long> paraList);
}
10.2XML code
<?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.CityTypeMapper">
<select id="getCity10" parameterType="java.util.ArrayList" resultType="com.hub.example.domain.CityPO">
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
WHERE CITY_ID IN
<foreach collection="list" item="cityId" open="(" separator="," close=")">
#{cityId}
</foreach>
</select>
</mapper>
11. The incoming parameter parameterType is List<Long>
Incoming parameter type parameterType: java.util.ArrayList.
Return result type resultType: java.util.Map.
11.1 Mapper interface code
@Repository
public interface CityTypeMapper {
List<Map<String, Object>> getCity11(List<Long> paraList);
}
11.2XML code
<?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.CityTypeMapper">
<select id="getCity11" parameterType="java.util.ArrayList" resultType="java.util.Map">
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
WHERE CITY_ID IN
<foreach collection="list" item="cityId" open="(" separator="," close=")">
#{cityId}
</foreach>
</select>
</mapper>
12. Test code
12.1 Code
@Slf4j
@RestController
@RequestMapping("/hub/example/cityType")
public class CityTypeController {
@Autowired
private CityTypeMapper cityTypeMapper;
@GetMapping("/load01")
public Object load01() {
log.info("测试开始...");
// 1.parameterType: java.lang.Integer; resultType: java.lang.String
String cityName01 = cityTypeMapper.getCity01(2);
// 2.parameterType: java.lang.Long; resultType: java.lang.String
String cityName02 = cityTypeMapper.getCity02(1L);
// 3.parameterType: java.lang.Double; resultType: java.lang.Double
double landArea = cityTypeMapper.getCity03(1.81D);
// 4.parameterType: java.lang.String; resultType: java.lang.Long
long cityId = cityTypeMapper.getCity04("苏州");
List<Long> paraList01 = Arrays.asList(1L, 2L);
// 5.parameterType: java.util.ArrayList; resultType: java.lang.Long
long[] population = cityTypeMapper.getCity05(paraList01);
// 6.parameterType: long[]; resultType: java.lang.String
Long[] paraList02 = new Long[]{1L, 2L};
List<String> cityNameList = cityTypeMapper.getCity06(paraList02);
// 7.parameterType: java.util.Map; resultType: com.hub.example.domain.CityPO
Map<String, Object> map = new HashMap<>();
map.put("cityId", 3L);
map.put("cityName", "苏州");
CityPO cityPO = cityTypeMapper.getCity07(map);
// 8.parameterType: com.hub.example.domain.CityPO; resultType: java.util.Map
CityPO cityPO02 = CityPO.builder().cityId(3L).cityName("苏州").build();
Map<String, Object> map02 = cityTypeMapper.getCity08(cityPO02);
// 9.parameterType: java.util.Date; resultType: java.lang.String
Date date = DateUtil.parse("2023-03-10 05:39:16", "yyyy-MM-dd HH:mm:ss");
String time = cityTypeMapper.getCity09(date);
// 10.parameterType: java.util.ArrayList; resultType: com.hub.example.domain.CityPO
List<Long> paraList = Arrays.asList(1L, 2L, 3L);
List<CityPO> list01 = cityTypeMapper.getCity10(paraList);
// 11.parameterType: java.util.ArrayList; resultType: java.util.Map
List<Long> paraList03 = Arrays.asList(1L, 2L, 3L);
List<Map<String, Object>> list02 = cityTypeMapper.getCity11(paraList03);
log.info("测试结束...");
return "执行成功";
}
}
12.2 Test Request
URL01: http://127.0.0.1:18080/hub-example/hub/example/cityType/load01
13. Foundation support
13.1 Entity objects
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CityPO implements Serializable {
private Long cityId;
private String cityName;
private Double landArea;
private Long population;
private Double gross;
private String cityDescribe;
private String dataYear;
private Date updateTime;
}
13.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='城市信息表';
13.3 Introduce MyBatis dependency
Use the mybatis-spring-boot-starter method to introduce mybatis, corresponding to mybatis-3.5.9 and mybatis-spring-2.0.7 core dependencies.
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
Above, thanks.
April 17, 2023