UserMapper.xml
UPDATE UserData SET money = #{_money} WHERE userid = #{_userid}
UserMapper.java
public interface UserDataMapper{ public int updatUserMoney(UserData); }
User.java
public class UserData { private String _userid; private int _money; ... }
Call
SqlSessionFactoryBuilder sqlSessionFactory = new SqlSessionFactoryBuilder() .build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); BaseDataMapper mapper = sqlSession.getMapper(BaseDataMapper.class); int updateCount = mapper.updatUserMoney(UserData); sqlSession.commit(true); //important!
這裡要注意的是有兩個地方:
- 如果需要接收Update後的row column count,記得必須把Mapper的回傳type設定為int,這樣子在呼叫的時候才有辦法讓mybatis回傳row count
- 因為會影響實際數據,所以記得要做session.commit(true)否則無法正常更新
沒有留言:
張貼留言