Mybatis添加数据传入Map作为参数

Mybatis添加数据传入Map作为参数

  • mapper.java dao层
package com.location.bjgwserver.mapper;


import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Exceldao层
 * @author csz
 * @date 2020/3/17
 */

@Repository
public interface ExcelMapper  {

    //传入参数为map
    void importExcel(@Param(value="map") Map<String,Object> map,@Param(value="table_name") String tableName);

}

  • mapper.xml 操作sql
<?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.location.bjgwserver.mapper.ExcelMapper">

	<!--collection中的map是dao层传入的map参数,item中的value表示map中的值,index中的key表示map中键-->
	<!--foreach进行循环map取出里面的key和value-->
    <insert id="importExcel"  >
    insert into ${table_name}
        <foreach item="value" collection="map" index="key" open="("   separator="," close=")">
            ${key}
        </foreach>
    values
        <foreach item="value" collection="map" index="key"  open="("   separator="," close=")">
            #{value}
        </foreach>
    </insert>

</mapper>

你可能感兴趣的:(后端,数据库)