1. 程式人生 > >MyBatis註解方式之@Update/@Delete

MyBatis註解方式之@Update/@Delete

@Update 1. RoleMapper介面增加介面方法 2. 單元測試 @Delete 1. RoleMapper介面增加介面方法 2. 單元測試 @Update 1. RoleMapper介面增加介面方法     /**      *       *       * @Title: updateSysRoleById      *       * @Description: updateSysRoleById      *       * @param sysRole      * @return      *       * @return: int      */     @Update({ "update sys_role set role_name = #{roleName},enabled = #{enabled},create_by = #{createBy},create_time = #{createTime, jdbcType=TIMESTAMP} where id = #{id}" })     int updateSysRoleById(SysRole sysRole); 2. 單元測試 @Test     public void updateSysRoleByIdTest() {         logger.info("updateSysRoleByIdTest");         // 獲取SqlSession         SqlSession sqlSession = getSqlSession();         try {             // 獲取RoleMapper介面             RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

            // 先根據ID查詢出對應的sysRole             SysRole sysRole = roleMapper.selectSysRoleById((long) 1);             // roleName期望為管理員             Assert.assertEquals("管理員", sysRole.getRoleName());

            // 修改RoleName             sysRole.setRoleName("管理員Artisan");             // 修改CreateBy             sysRole.setCreateBy("Artisan");             // 修改使用者 ,返回受影響的行數             int result = roleMapper.updateSysRoleById(sysRole);

            // 只插入一條資料 ,期望是1             Assert.assertEquals(1, result);             logger.info("受影響的行數:" + result);

            // 期望的RoleName為管理員Artisan             Assert.assertEquals("管理員Artisan", sysRole.getRoleName());             // 期望的CreateBy為Artisan             Assert.assertEquals("Artisan", sysRole.getCreateBy());

            logger.info("sysRole:" + sysRole);

        } catch (Exception e) {             e.printStackTrace();         } finally {             // 為了保持測試資料的乾淨,這裡選擇回滾             // 由於預設的sqlSessionFactory.openSession()是不自動提交的             // 除非顯式的commit,否則不會提交到資料庫             sqlSession.rollback();             logger.info("為了保持測試資料的乾淨,這裡選擇回滾,不寫入mysql,請觀察日誌,回滾完成");

            sqlSession.close();             logger.info("sqlSession close successfully ");         }     } 2018-04-19 02:55:57,225  INFO [main] (BaseMapperTest.java:26) - sessionFactory bulit successfully 2018-04-19 02:55:57,230  INFO [main] (BaseMapperTest.java:29) - reader close successfully 2018-04-19 02:55:57,233  INFO [main] (RoleMapperTest.java:229) - updateSysRoleByIdTest 2018-04-19 02:55:57,726 DEBUG [main] (BaseJdbcLogger.java:145) - ==>  Preparing: SELECT a.id, a.role_name roleName, a.enabled, a.create_by createBy, a.create_time createTime FROM sys_role a WHERE a.id = ?  2018-04-19 02:55:57,799 DEBUG [main] (BaseJdbcLogger.java:145) - ==> Parameters: 1(Long) 2018-04-19 02:55:57,824 TRACE [main] (BaseJdbcLogger.java:151) - <==    Columns: id, roleName, enabled, createBy, createTime 2018-04-19 02:55:57,825 TRACE [main] (BaseJdbcLogger.java:151) - <==        Row: 1, 管理員, 1, 1, 2018-04-13 21:12:46.0 2018-04-19 02:55:57,828 DEBUG [main] (BaseJdbcLogger.java:145) - <==      Total: 1 2018-04-19 02:55:57,829 DEBUG [main] (BaseJdbcLogger.java:145) - ==>  Preparing: update sys_role set role_name = ?,enabled = ?,create_by = ?,create_time = ? where id = ?  2018-04-19 02:55:57,835 DEBUG [main] (BaseJdbcLogger.java:145) - ==> Parameters: 管理員Artisan(String), 1(Integer), Artisan(String), 2018-04-13 21:12:46.0(Timestamp), 1(Long) 2018-04-19 02:55:57,839 DEBUG [main] (BaseJdbcLogger.java:145) - <==    Updates: 1 2018-04-19 02:55:57,840  INFO [main] (RoleMapperTest.java:250) - 受影響的行數:1 2018-04-19 02:55:57,845  INFO [main] (RoleMapperTest.java:257) - sysRole:SysRole [id=1, roleName=管理員Artisan, enabled=1, createBy=Artisan, createTime=Fri Apr 13 21:12:46 BOT 2018, user=null, privilegeList=null] 2018-04-19 02:55:57,849  INFO [main] (RoleMapperTest.java:267) - 為了保持測試資料的乾淨,這裡選擇回滾,不寫入mysql,請觀察日誌,回滾完成 2018-04-19 02:55:57,851  INFO [main] (RoleMapperTest.java:270) - sqlSession close successfully  @Delete 1. RoleMapper介面增加介面方法 /**      *       *       * @Title: deleteSysRoleById      *       * @Description: deleteSysRoleById      *       * @param id      * @return      *       * @return: int      */     @Delete("delete from sys_role where id = #{id}")     int deleteSysRoleById(Long id); 2. 單元測試 @Test     public void deleteSysRoleByIdTest() {         logger.info("deleteSysRoleByIdTest");         // 獲取SqlSession         SqlSession sqlSession = getSqlSession();         try {             // 獲取roleMapper介面             RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);

            // 呼叫刪除介面             int result = roleMapper.deleteSysRoleById((long) 1);             // 期望影響的結果條數為 1             Assert.assertEquals(1, result);

            // 再次查詢             SysRole sysRole = roleMapper.selectSysRoleById((long) 1);             // 期望查詢出來的sysRole 為 null             Assert.assertNull(sysRole);         } catch (Exception e) {             e.printStackTrace();         } finally {             // 為了保持測試資料的乾淨,這裡選擇回滾             // 由於預設的sqlSessionFactory.openSession()是不自動提交的             // 除非顯式的commit,否則不會提交到資料庫             sqlSession.rollback();             logger.info("為了保持測試資料的乾淨,這裡選擇回滾,不寫入mysql,請觀察日誌,回滾完成");

            sqlSession.close();             logger.info("sqlSession close successfully ");         }     } 2018-04-19 02:56:23,024  INFO [main] (BaseMapperTest.java:26) - sessionFactory bulit successfully 2018-04-19 02:56:23,027  INFO [main] (BaseMapperTest.java:29) - reader close successfully 2018-04-19 02:56:23,030  INFO [main] (RoleMapperTest.java:276) - deleteSysRoleByIdTest 2018-04-19 02:56:23,517 DEBUG [main] (BaseJdbcLogger.java:145) - ==>  Preparing: delete from sys_role where id = ?  2018-04-19 02:56:23,589 DEBUG [main] (BaseJdbcLogger.java:145) - ==> Parameters: 1(Long) 2018-04-19 02:56:23,596 DEBUG [main] (BaseJdbcLogger.java:145) - <==    Updates: 1 2018-04-19 02:56:23,599 DEBUG [main] (BaseJdbcLogger.java:145) - ==>  Preparing: SELECT a.id, a.role_name roleName, a.enabled, a.create_by createBy, a.create_time createTime FROM sys_role a WHERE a.id = ?  2018-04-19 02:56:23,599 DEBUG [main] (BaseJdbcLogger.java:145) - ==> Parameters: 1(Long) 2018-04-19 02:56:23,625 DEBUG [main] (BaseJdbcLogger.java:145) - <==      Total: 0 2018-04-19 02:56:23,628  INFO [main] (RoleMapperTest.java:299) - 為了保持測試資料的乾淨,這裡選擇回滾,不寫入mysql,請觀察日誌,回滾完成 2018-04-19 02:56:23,631  INFO [main] (RoleMapperTest.java:302) - sqlSession close successfully