数据库更新 ‹update›

Exisi 2021-03-27 07:27:01
Categories: Tags:

 

public interface UsersMapper {

 

public Integer update(Users user);

 

}

 

 

 

<?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

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);

}

}