mybatis中数组和List使用下标取值以及传入多个参数时取值
记录:416
场景:在mybatis中接口的入参是数组和List时,使用下标从数组和List取值。接口的入参有多个参数时取值,以及注解@Param使用。
版本:JDK 1.8,Spring Boot 2.6.3,mybatis-3.5.9。
1.入参是数组时使用下标取值
入参是数组时使用下标取值,和Java代码中数组使用下标取值一样。
1.1Mapper接口
@Repository
public interface GetValueByIndexMapper {int insertCityByArray(long[] cityInfo);
}
1.2Mapper.xml
<?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.GetValueByIndexMapper"><insert id="insertCityByArray" parameterType="long[]">INSERT INTO t_city_index(CITY_ID, POPULATION, GROSS)VALUES (#{cityInfo[0]}, #{cityInfo[1]}, #{cityInfo[2]})</insert>
</mapper>
2.入参是List时使用下标取值
入参是数组时使用下标取值,和Java代码中数组使用下标取值一样。
2.1Mapper接口
public interface GetValueByIndexMapper {int insertCityByList(List<Long> cityInfo);
}
2.2Mapper.xml
<?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.GetValueByIndexMapper"><insert id="insertCityByList" parameterType="java.util.List">INSERT INTO t_city_index(CITY_ID, POPULATION, GROSS)VALUES (#{cityInfo[0]}, #{cityInfo[1]}, #{cityInfo[2]})</insert>
</mapper>
3.入参有多个参数时取值
入参有多个参数时取值,第一个参数时一个实体类,第二个参数时一个基本类型。使用@Param注解在接口的方法上指定MyBatis在XML中取值的参数名称。
3.1Mapper接口
public interface GetValueByIndexMapper {int insertCityMultipleArgs(@Param("cityArgs") CityArgs cityArgs, @Param("gross") long gross);
}
3.2Mapper.xml
<?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.GetValueByIndexMapper"><insert id="insertCityMultipleArgs">INSERT INTO t_city_index(CITY_ID, POPULATION, GROSS)VALUES (#{cityArgs.cityId}, #{cityArgs.population}, #{gross})</insert>
</mapper>
4.测试
4.1测试代码
@Slf4j
@RestController
@RequestMapping("/hub/example/cityIndex")
public class GetValueByIndexController {@Autowiredprivate GetValueByIndexMapper getValueByIndexMapper;@GetMapping("/load01")public Object load01() {log.info("测试开始...");long[] paraList01 = new long[]{1L, 2L, 3L};getValueByIndexMapper.insertCityByArray(paraList01);List<Long> paraList02 = Arrays.asList(11L, 12L, 12L);getValueByIndexMapper.insertCityByList(paraList02);log.info("测试结束...");return "执行成功";}@GetMapping("/load02")public Object load02() {log.info("测试开始...");long[] paraList01 = new long[]{1L, 2L, 3L};CityArgs cityArgs = CityArgs.builder().cityId(111L).population(222L).build();long gross = 333L;getValueByIndexMapper.insertCityMultipleArgs(cityArgs,gross);log.info("测试结束...");return "执行成功";}
}
4.2测试URL
URL01:http://127.0.0.1:18080/hub-example/hub/example/cityIndex/load01
URL02:http://127.0.0.1:18080/hub-example/hub/example/cityIndex/load02
5.辅助
5.1实体对象
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CityArgs {private Long cityId;private Long population;
}
5.2建表语句
CREATE TABLE t_city_index (CITY_ID BIGINT(16) NOT NULL COMMENT '唯一标识',POPULATION BIGINT(16) DEFAULT NULL COMMENT '城市人口',GROSS BIGINT(20) DEFAULT NULL COMMENT '生产总值'
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='城市信息表';
以上,感谢。
2023年4月18日