MyBatis Map Parameter Tutorial

In MyBatis, you can use a map to receive values. The specific steps are as follows:

  1. Add a parameter of type map to the method in the Mapper interface.
void insertUser(Map<String, Object> userMap);
  1. They have been together for three years now.
<insert id="insertUser" parameterType="java.util.Map">
    INSERT INTO user (id, username, password) 
    VALUES (#{id}, #{username}, #{password})
</insert>
  1. When calling the Mapper method, you need to create a map object and store the parameters to be passed to the Mapper as key-value pairs in the map.
Map<String, Object> userMap = new HashMap<>();
userMap.put("id", 1);
userMap.put("username", "admin");
userMap.put("password", "123456");

userMapper.insertUser(userMap);

This way, parameters can be passed to the map and MyBatis can be used to operate on the database.

bannerAds