mybatis框架入门程序:演示通过mybatis实现数据库的删除操作

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

2.删除用户的映射文件:

<!-- 删除 -->
    <delete id="deleteUserById" parameterType="int">
        delete from user where id=#{id}
    </delete>

3.测试程序:

    @Test
    public void testDeleteUserById() throws Exception{
        //通过流将核心配置文件读取进来
        InputStream inputStream=Resources.getResourceAsStream("config/SqlMapConfig.xml");
        //通过核心配置文件输入流来创建工厂
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
        //通过工厂创建session
        SqlSession openSession=factory.openSession();
        //通过会话删除
        openSession.delete("test.deleteUserById", 30);
        //一定要提交事务,做查找的时候可以不用提交事务,但是增删改必须要提交事务。
        //提交事务 mybatis会自动开启事务,但是它不知道何时提交,需要手动提交事务
        openSession.commit();
        //关闭资源
        openSession.close();
        //factory没有close(),因为session关闭之后,factory也就关闭了。
    }
原文地址:https://www.cnblogs.com/wyhluckdog/p/10150373.html