- UsersMapper.java
public interface UsersMapper {
public Integer update(Users user);
}
- UsersMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace="com.mybatis.dao.UsersMapper">
<update id="update" parameterType="Users">
update user
set name = #{name}, sex=#{sex}
</update>
</mapper>
- Test.java
@Test
public void updateUser(Users user) {
SqlSession sqlSession=null;
Integer count=0;
try{
sqlSession=MyBatisUtil.createSqlSession();
User user=new User()
user.setId(1);
user.setName("李四");
user.setAdress("上海");
count=sqlSession.getMapper(UsersMapper.class).update(user);
sqlSession.commit();
System.out.println("count:"+count);
}catch(Exception e){
e.printStackTrace();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
}