mybatis update
时间: 2023-09-08 16:15:13 浏览: 124
MyBatis is a Java-based persistence framework that provides support for executing SQL statements and mapping the results back to Java objects. To update data in a database using MyBatis, you can use the following steps:
1. Define an update statement in a MyBatis mapper XML file or annotation-based mapper interface.
```
<update id="updateUser" parameterType="User">
UPDATE users SET name=#{name}, age=#{age} WHERE id=#{id}
</update>
```
2. Create a MyBatis SqlSession object.
```
SqlSession sqlSession = sqlSessionFactory.openSession();
```
3. Call the update() method of the SqlSession object, passing in the update statement ID and the object to be updated.
```
User user = new User();
user.setId(1);
user.setName("John");
user.setAge(30);
sqlSession.update("updateUser", user);
```
4. Call the commit() method of the SqlSession object to commit the transaction.
```
sqlSession.commit();
```
5. Close the SqlSession object.
```
sqlSession.close();
```
These steps will execute the update statement and update the data in the database.
阅读全文