【MyBatis框架】动态SQL之set详解

set:

用于修改表中的数据,自动去掉没用的逗号。


实例讲解:

  • 编写接口
int updateBook(Map map);
  • 编写xml配置文件
<update id="updateBook" parameterType="map" >
        update book
        <set>
            <if test="name!=null">
                name=#{name},
            if>
            <if test="author!=null">
                author=#{author}
            if>
        set>
        where id=#{id}
    update>
  • 测试
 @org.junit.Test
       public  void testIf(){
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
            BookMapper bookMapper=sqlSession.getMapper(BookMapper.class);
            HashMap map=new HashMap();
          map.put("name","三国");
         map.put("author","娃哈");
           map.put("id","78eb87ec.c6ac.4b1c.881a.aabccff3a670");

         bookMapper.updateBook(map);

            sqlSession.close();
       }

【MyBatis框架】动态SQL之set详解_第1张图片

你可能感兴趣的:(MyBatis框架,mysql,java,mybatis)