MyBatis in map application

Assuming that too much of our entity class, or database tables, fields or parameters, we should consider using the Map!

//万能Map
    int addUser(Map<String,Object> map);
    <!--对象中的属性,可以直接取出来 parameterType=传递map中的key-->
    <insert id="addUser" parameterType="map">
        insert into mybatis.user (id, name, pwd) values (#{userId},#{userName},#{password});
    </insert>
	//万能map
    @Test
    public void addUser(){
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",4);
        map.put("userName","王五");
        map.put("password","123111");
        mapper.addUser(map);
        //提交事务
        sqlSession.commit();
        sqlSession.close();
    }
  • Map transfer parameters, can be directly taken in the sql key! [ParameterType = "map"]
  • Passing the object parameters, the object is taken directly to the sql attribute! [ParameterType = "Object"]
  • Only a case where the basic type of the parameter to be taken directly in the sql! Multiple parameters Map, or comment!
Published 29 original articles · won praise 49 · views 1782

Guess you like

Origin blog.csdn.net/qq_41256881/article/details/105362588