I can insert into db using MyBatis if I have an object like,
public class Student {
private int id;
private String name;
}
..
@Insert("INSERT INTO STUDENT (ID,NAME) VALUES (#{id}, #{name})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void insert(Student student);
But how can I use mybatis if I have a hashmap with values as,
studentDetailsMap = [{id,10},{name,"smith"}] //represented as key value pairs for understanding
What I have are big tables with so much columns and the data is available to mybatis as a hashmap for each row. There are so many such cases, so I dont want to create a lot pojo classes that is why I am trying to find a way to insert directly from hashmap to mybatis.
Create the mapper element in the mapper XML:
<insert id="fnInsert"> insert into ${tablename} <trim> <trim prefix="(" suffix=")" suffixOverrides=","> <foreach collection="mapdata" item="element" index="index" separator=",">${index}</foreach>
</trim> select <foreach collection="mapdata" item="element" index="index" separator=","> <trim> #{element} </trim> </foreach> from dual </trim> </insert>
public int insertMICTables(@Param("tablename") String tablename, @Param("mapdata") Map<String, String> mapdata);
Prepare input parameters:
Map<String, String> mapdata = new HashMap<String, String>(); mapdata.put("columna", "id01"); mapdata.put("columnb", "Hello manager"); this._epMapper.insertMICTables("tablename", mapdata);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.