mybatis学习笔记(八):根据用户ID删除用户信息

需求

根据用户ID删除用户信息

环境说明

MyBatis全局配置文件、mysql数据库相关信息、映射文件User.xml等,都是基于这篇博客mybatis学习笔记(三):根据用户ID(主键)查询用户信息,有需要可以点击查看

映射文件

在User.xml中配置根据用户id删除用户信息的statement

添加内容如下:

    
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    delete>

程序代码

    //删除用户
    @Test
    public void deleteUserTest() throws IOException {

        String resource = "SqlMapConfig.xml";

        InputStream inputStream = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        //传入用户id删除用户信息
        sqlSession.insert("test.deleteUser", 17);

        //提交事务
        sqlSession.commit();

        sqlSession.close();
    }

程序执行结果

......

[DEBUG]17:23:32,532,main,[Class]JdbcTransaction, [Method]openConnection, Opening JDBC Connection
[DEBUG]17:23:32,817,main,[Class]PooledDataSource, [Method]popConnection, Created connection 1630521067.
[DEBUG]17:23:32,818,main,[Class]JdbcTransaction, [Method]setDesiredAutoCommit, Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@612fc6eb]
[DEBUG]17:23:32,820,main,[Class]deleteUser, [Method]debug, ==>  Preparing: delete from user where id=? 
[DEBUG]17:23:32,872,main,[Class]deleteUser, [Method]debug, ==> Parameters: 17(Integer)
[DEBUG]17:23:32,875,main,[Class]deleteUser, [Method]debug, <==    Updates: 1
[DEBUG]17:23:32,875,main,[Class]JdbcTransaction, [Method]commit, Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@612fc6eb]
[DEBUG]17:23:32,880,main,[Class]JdbcTransaction, [Method]resetAutoCommit, Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@612fc6eb]
[DEBUG]17:23:32,880,main,[Class]JdbcTransaction, [Method]close, Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@612fc6eb]

......

可以看出对应用户id的用户信息已删除,查看数据库数据,该记录确实已删除

你可能感兴趣的:(mybatis)