• Mybatis框架Delete节点元素的使用


    这个就也比较简单,需求:将我们最新插入的那条数据删除掉,从用户表中。

    UserMapper.xml

     UserMapper.java

     编写测试方法:

     1     @Test
     2     public void testDelete() {
     3         SqlSession sqlSession = null;
     4         Integer id=21;
     5         int count=0;//返回受影响的行数
     6         try {
     7             sqlSession = MyBatisUtil.createSqlSession();
     8             //调用mapper接口的方式实现
     9              count= sqlSession.getMapper(UserMapper.class).deleteUser(id);
    10             // int i=1/0;//模拟异常  后台日志打印出rolling back表示没有插入成功,事务进行了回滚
    11             mlogger.info("返回受影响的行数:" + count);
    12             sqlSession.commit();//后台日志打印出rolling back表示没有提交事务,事务进行了回滚,所以必须得提交事务才行
    13         } catch (Exception e) {
    14             // TODO: handle exception
    15         } finally {
    16             // 最后一定要注意:关闭会话
    17             MyBatisUtil.closeSqlSession(sqlSession);
    18 
    19         }
    20 
    21     }

    运行结果:

     1 [DEBUG] 2019-11-05 21:47:04,506 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
     2 [DEBUG] 2019-11-05 21:47:04,799 org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 176065613.
     3 [DEBUG] 2019-11-05 21:47:04,802 cn.smbms.dao.user.UserMapper.deleteUser - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@a7e8c4d]
     4 [DEBUG] 2019-11-05 21:47:04,803 cn.smbms.dao.user.UserMapper.deleteUser - ==>  Preparing: delete from smbms_user where id=? 
     5 [DEBUG] 2019-11-05 21:47:04,899 cn.smbms.dao.user.UserMapper.deleteUser - ==> Parameters: 21(Integer)
     6 [INFO] 2019-11-05 21:47:04,902 cn.smbms.dao.test.UserMapperTest - 返回受影响的行数:1
     7 [DEBUG] 2019-11-05 21:47:04,903 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Committing JDBC Connection [com.mysql.jdbc.JDBC4Connection@a7e8c4d]
     8 [DEBUG] 2019-11-05 21:47:04,907 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@a7e8c4d]
     9 [DEBUG] 2019-11-05 21:47:04,908 org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@a7e8c4d]
    10 [DEBUG] 2019-11-05 21:47:04,909 org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 176065613 to pool.
  • 相关阅读:
    Sign in with the app-specific password you generated. If you forgot the app-specific password or need to create a new one, go to appleid.apple.com
    Java Web项目搭建过程记录(struts2)
    微信小程序之菜鸟入门教学(二)
    微信小程序之菜鸟选手入门教学(一)
    html 表单input录入内容校验
    VUE中使用driver.js实现先手引导
    BScroll使用
    VUE使用screenfull实现全屏
    VUE打印功能
    VUE中使用XLSX实现导出excel表格
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/11801939.html
Copyright © 2020-2023  润新知