- @Update
- 1. RoleMapper接口增加接口方法
- 2. 单元测试
- @Delete
- 1. RoleMapper接口增加接口方法
- 2. 单元测试
@Update
1. RoleMapper接口增加接口方法
代码语言:javascript
复制 /**
*
*
* @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. 单元测试
代码语言:javascript
复制@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 ");
}
}
代码语言:javascript
复制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接口增加接口方法
代码语言:javascript
复制/**
*
*
* @Title: deleteSysRoleById
*
* @Description: deleteSysRoleById
*
* @param id
* @return
*
* @return: int
*/
@Delete("delete from sys_role where id = #{id}")
int deleteSysRoleById(Long id);
2. 单元测试
代码语言:javascript
复制@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 ");
}
}
代码语言:javascript
复制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