> 文章列表 > MyBatis的parameterType传入参数类型和resultType返回结果类型

MyBatis的parameterType传入参数类型和resultType返回结果类型

MyBatis的parameterType传入参数类型和resultType返回结果类型

记录:413

场景:MyBatis的parameterType传入参数类型和resultType返回结果类型。

版本:JDK 1.8,Spring Boot 2.6.3,mybatis-3.5.9。

1.传入参数parameterType是Integer

传入参数类型parameterType:java.lang.Integer。

返回结果类型resultType: java.lang.String。

1.1Mapper接口代码

@Repository
public interface CityTypeMapper {String getCity01(Integer cityId);
} 

1.2XML代码

<?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.传入参数parameterType是Long

传入参数类型parameterType: java.lang.Long。

返回结果类型resultType: java.lang.String。

2.1Mapper接口代码

@Repository
public interface CityTypeMapper {String getCity02(Long cityId);
}

2.2XML代码

<?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.传入参数parameterType是Double

传入参数类型parameterType: java.lang.Double。

返回结果类型resultType: java.lang.Double。

3.1Mapper接口代码

@Repository
public interface CityTypeMapper {Double getCity03(Double gross);
}

3.2XML代码

<?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.传入参数parameterType是String

传入参数类型parameterType: java.lang.String。

返回结果类型resultType: java.lang.Long。

4.1Mapper接口代码

@Repository
public interface CityTypeMapper {Long getCity04(String cityName);
}

4.2XML代码

<?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.传入参数parameterType是List<Long>

传入参数类型parameterType: java.util.ArrayList。

返回结果类型resultType: java.lang.Long。

5.1Mapper接口代码

@Repository
public interface CityTypeMapper {long[] getCity05(List<Long> cityIdList);
}

5.2XML代码

<?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.传入参数parameterType是Long[]

传入参数类型parameterType: long[]。

返回结果类型resultType: java.lang.String。

6.1Mapper接口代码

@Repository
public interface CityTypeMapper {List<String> getCity06(Long[] cityIdList);
}

6.2XML代码

<?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.传入参数parameterType是Map<String, Object>

传入参数类型parameterType: java.util.Map。

返回结果类型resultType: com.hub.example.domain.CityPO。

7.1Mapper接口代码

@Repository
public interface CityTypeMapper {CityPO getCity07(Map<String, Object> cityMap); 
}

7.2XML代码

<?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_citywhere CITY_ID = #{cityId}AND CITY_NAME = #{cityName}</select>   
</mapper>

8.传入参数parameterType是CityPO

传入参数类型parameterType: com.hub.example.domain.CityPO。

返回结果类型resultType: java.util.Map。

8.1Mapper接口代码

@Repository
public interface CityTypeMapper {Map<String, Object> getCity08(CityPO cityPO);   
}

8.2XML代码

<?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_citywhere CITY_ID = #{cityId}AND CITY_NAME = #{cityName}</select>   
</mapper>

9.传入参数parameterType是Date

传入参数类型parameterType: java.util.Date。

返回结果类型resultType: java.lang.String。

9.1Mapper接口代码

@Repository
public interface CityTypeMapper {String getCity09(Date date);
}

9.2XML代码

<?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_NAMEFROM t_citywhere UPDATE_TIME = #{date}</select>   
</mapper>

10.传入参数parameterType是List<Long>

传入参数类型parameterType: java.util.ArrayList。

返回结果类型resultType: com.hub.example.domain.CityPO。

10.1Mapper接口代码

@Repository
public interface CityTypeMapper {List<CityPO> getCity10(List<Long> paraList);   
}

10.2XML代码

<?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_cityWHERE CITY_ID IN<foreach collection="list" item="cityId" open="(" separator="," close=")">#{cityId}</foreach></select>   
</mapper>

11.传入参数parameterType是List<Long>

传入参数类型parameterType: java.util.ArrayList。

返回结果类型resultType: java.util.Map。

11.1Mapper接口代码

@Repository
public interface CityTypeMapper {List<Map<String, Object>> getCity11(List<Long> paraList);
}

11.2XML代码

<?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_cityWHERE CITY_ID IN<foreach collection="list" item="cityId" open="(" separator="," close=")">#{cityId}</foreach></select>   
</mapper>

12.测试代码

12.1代码

@Slf4j
@RestController
@RequestMapping("/hub/example/cityType")
public class CityTypeController {@Autowiredprivate CityTypeMapper cityTypeMapper;@GetMapping("/load01")public Object load01() {log.info("测试开始...");// 1.parameterType: java.lang.Integer; resultType: java.lang.StringString cityName01 = cityTypeMapper.getCity01(2);// 2.parameterType: java.lang.Long; resultType: java.lang.StringString cityName02 = cityTypeMapper.getCity02(1L);// 3.parameterType: java.lang.Double; resultType: java.lang.Doubledouble landArea = cityTypeMapper.getCity03(1.81D);// 4.parameterType: java.lang.String; resultType: java.lang.Longlong cityId = cityTypeMapper.getCity04("苏州");List<Long> paraList01 = Arrays.asList(1L, 2L);// 5.parameterType: java.util.ArrayList; resultType: java.lang.Longlong[] population = cityTypeMapper.getCity05(paraList01);// 6.parameterType: long[]; resultType: java.lang.StringLong[] paraList02 = new Long[]{1L, 2L};List<String> cityNameList = cityTypeMapper.getCity06(paraList02);// 7.parameterType: java.util.Map; resultType: com.hub.example.domain.CityPOMap<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.MapCityPO cityPO02 = CityPO.builder().cityId(3L).cityName("苏州").build();Map<String, Object> map02 = cityTypeMapper.getCity08(cityPO02);// 9.parameterType: java.util.Date; resultType: java.lang.StringDate 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.CityPOList<Long> paraList = Arrays.asList(1L, 2L, 3L);List<CityPO> list01 = cityTypeMapper.getCity10(paraList);// 11.parameterType: java.util.ArrayList; resultType: java.util.MapList<Long> paraList03 = Arrays.asList(1L, 2L, 3L);List<Map<String, Object>> list02 = cityTypeMapper.getCity11(paraList03);log.info("测试结束...");
return "执行成功";}
}

12.2测试请求

URL01: http://127.0.0.1:18080/hub-example/hub/example/cityType/load01

13.基础支撑

13.1实体对象

@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 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引入MyBatis依赖

使用mybatis-spring-boot-starter方式引入mybatis,对应mybatis-3.5.9和mybatis-spring-2.0.7核心依赖。

<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version>
</dependency>

以上,感谢。

2023年4月17日

小吃零食