PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2022-09-01 01:01:01  修改时间:2024-12-19 10:07:22  分类:06.Java框架/系统  编辑

获取map的key并遍历

<foreach collection="map.keys" open="(" close=")" separator="," index="index" item="key">

获取map的value并遍历

<foreach collection="map.values" open="(" close=")" separator="," item="value" index="index">

同时遍历map的key和value(注意:.entrySet() 是可以省略的)

<foreach collection="after.entrySet()" separator="," index="key" item="value" >
    ${key}=#{value}
</foreach>

代码示例

dao层对应的增删改方法

修改是传了两个map作为参数一个是修改前的map作为条件另一个是修改后的数据

int insert(@Param("map") HashMap<String, String> map,@Param("tableName") String tableName);
 
int delete(@Param("map") HashMap<String, String> map,@Param("tableName") String tableName);
 
int update(@Param("before") HashMap<String, String> before,@Param("after") HashMap<String, String> after,@Param("tableName") String tableName);

以下三个代码段则是对应mybatis的语句

<insert id="insert">
	insert into ${tableName}
	<foreach collection="map.keys" open="(" close=")" separator="," index="index" item="key">
		${key}
	</foreach>
	values
	<foreach collection="map.values" open="(" close=")" separator="," item="value" index="index">
		#{value}
	</foreach>
</insert>
<delete id="delete">
	delete from ${tableName} where
	<foreach collection="map.entrySet()" index="key" item="value" separator="and">
		${key}=#{value}
	</foreach>
</delete>
<update id="update">
	update ${tableName} set
	<foreach collection="after.entrySet()" separator="," index="key" item="value" >
		${key}=#{value}
	</foreach>
	where
	<foreach collection="before.entrySet()" index="key" item="value" separator="and">
		${key}=#{value}
	</foreach>
</update>

其实 .entrySet() 是可以省略的:

<update id="update">
	update ${tableName} set
	<foreach collection="after" separator="," index="key" item="value" >
		${key}=#{value}
	</foreach>
	where
	<foreach collection="before" index="key" item="value" separator="and">
		${key}=#{value}
	</foreach>
</update>

 

 

参考:

  1. MyBatis获取Map中的key和value
  2. MyBatis如何遍历Map的key和value
  3. MyBatis 遍历Map
  4. MyBatis之foreach遍历Map实现
  5. MyBatis循环Map(高级用法)

 


工作中的一个模块:系统设置

SysSettingMapper.java

public interface SysSettingMapper extends BaseMapper<SysSetting> {
    int batchInsert(@Param("settingMap") Map<String, Object> settingMap);
}

SysSettingMapper.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.wanma.framework_web.mapper.SysSettingMapper">
    <insert id="batchInsert">
        
        <foreach collection="settingMap" index="key" item="value" separator=";">
            INSERT INTO sys_setting(`name`, `value`) VALUES
             (#{key}, #{value})
            ON DUPLICATE KEY UPDATE `value` = #{value}
        </foreach>
        
    </insert>
</mapper>

表:sys_setting

--
-- 表的结构 `sys_setting`
--

CREATE TABLE `sys_setting` (
  `id` int(11) NOT NULL COMMENT '自增ID',
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '名称',
  `value` varchar(600) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '值',
  `add_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统设置表';