update用法:
1.接口类中添加的方法:
int updateById(SysUser sysUser);
2.映射文件中添加的代码:
<update id="updateById"> UPDATE sys_user SET user_name=#{userName}, user_password=#{userPassword}, user_email=#{userEmail}, user_info=#{userInfo}, head_img=#{headImg,jdbcType=BLOB}, create_time=#{createTime,jdbcType=TIMESTAMP} WHERE id=#{id} </update>
3.测试代码:
public void testInsert(){ SqlSession sqlSession = getSqlSession(); try{ UserMapper userMapper = sqlSession.getMapper(UserMapper.class); SysUser user = new SysUser(); user.setUserName("test2"); user.setUserPassword("123456"); user.setUserEmail("[email protected]"); user.setUserInfo("test info"); user.setHeadImg(new byte[]{1,2,3}); user.setCreateTime(new Date()); int result = userMapper.insert(user); Assert.assertEquals(1,result); Assert.assertNull(user.getId()); }finally { sqlSession.rollback(); sqlSession.close(); } }
delete用法:
1.接口类中添加的方法:
int deleteById(Long id); int deleteById(SysUser sysUser);
2.映射文件中添加的代码:
<delete id="deleteById"> DELETE FROM sys_user WHERE id = #{id} </delete> <delete id="deleteById2"> DELETE FROM sys_user WHERE id = #{id} </delete>
3.测试代码:
@Test public void testUpdateById(){ SqlSession sqlSession = getSqlSession(); try{ UserMapper userMapper = sqlSession.getMapper(UserMapper.class); SysUser user = userMapper.selectById(1L); Assert.assertEquals("admin",user.getUserName()); user.setUserName("junjie"); user.setUserPassword("123456"); user.setUserEmail("[email protected]"); user.setUserInfo("junjie test info"); int result = userMapper.updateById(user); Assert.assertEquals(1,result); user=userMapper.selectById(1L); Assert.assertEquals("junjie",user.getUserName()); }finally { sqlSession.commit(); sqlSession.close(); } } public void testDeleteById(){ SqlSession sqlSession = getSqlSession(); try{ UserMapper userMapper = sqlSession.getMapper(UserMapper.class); SysUser user = userMapper.selectById(1L); //System.out.println(); //Assert.assertNotNull(user); //Assert.assertEquals(1,userMapper.deleteById(1L)); //SysUser user2 = userMapper.selectById(1001L); //Assert.assertNotNull(user2); //Assert.assertEquals(1,userMapper.deleteById(user2)); //Assert.assertNull(userMapper.selectById(1001L)); }finally { sqlSession.rollback(); sqlSession.close(); } }