MyBatis中map的应用

假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用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传递参数,直接在sql中取出key即可!【parameterType=“map”】
  • 对象传递参数,直接在sql中取对象的属性即可!【parameterType=“Object”】
  • 只有一个基本类型参数的情况下,可以直接在sql中取到! 多个参数用Map,或者注解!
发布了29 篇原创文章 · 获赞 49 · 访问量 1782

猜你喜欢

转载自blog.csdn.net/qq_41256881/article/details/105362588