可以参考之前的博文mybatis首次使用并查询数据库数据
下文在该项目基础上进行优化操作,UserDao.java改名为UserMapper.java,UserDao.xml改名为UserMapper.xml,也可以不改名字内容一样。
@Test
public void testDeleteUserById() throws IOException {
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUserByUserId(43);
//sqlSession.commit();
//通过commit方法可以实现数据库里面数据的真实删除
}
void deleteUserByUserId(Integer id);
<delete id="deleteUserByUserId" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
@Test
public void testUpdateUserById(){
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUsername("郭襄");
user.setPassword("111");
user.setBirthday(new Date());
user.setSex("女");
user.setAddress("峨眉山");
user.setId(42);
userMapper.updateUserById(user);
//sqlSession.commit();
}
void updateUserById(User user);
<update id="updateUserById" parameterType="cn.fpl1116.pojo.User">
UPDATE user
SET username=#{username}, password=#{password}, birthday=#{birthday}, sex=#{sex}, address=#{address}
WHERE id=#{id}
</update>