1.mybatis的基本配置工作可以在我的这篇博客中查看:https://www.cnblogs.com/wyhluckdog/p/10149480.html

2.修改用户的配置文件:

<update id="updateUser" parameterType="com.huida.po.User">
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
    </update>

传入的参数类型为po类型。

3.测试程序:

@Test
    public void testUpdateUserById() throws Exception{
        //通过流将核心配置文件读取进来
        InputStream inputStream=Resources.getResourceAsStream("config/SqlMapConfig.xml");
        //通过核心配置文件输入流来创建工厂
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        //通过工厂创建session
        SqlSession openSession=factory.openSession();
        //通过会话修改
        User user=new User();
        user.setId(1);
        user.setUsername("xiaoming");
        openSession.update("test.updateUser", user);
        
        //一定要提交事务,做查找的时候可以不用提交事务,但是增删改必须要提交事务。
        //提交事务 mybatis会自动开启事务,但是它不知道何时提交,需要手动提交事务
        openSession.commit();
        //关闭资源
        openSession.close();
        //factory没有close(),因为session关闭之后,factory也就关闭了。
    }

我们的修改操作是根据id进行修改的,所以我们可以通过po类User来修改Id,实际上就是通过id值相同来进行覆盖修改。

posted on 2018-12-20 16:56  wyhluckydog  阅读(294)  评论(0编辑  收藏  举报