Add the following update statement in
<!--update Label--> <update parameterType="string"> update website set name = #{name} </update>
Add an updateWebsite() method to the WebsiteMapper interface
int updateWebsite(String name);
The parameter is a String type string; the return value is type int, indicating the number of rows of the affected records after the SQL statement is executed.
Update tag common properties
Attribute name | describe | Remark |
---|---|---|
id | It is combined with the Mapper namespace and is a unique identifier for MyBatis call | If the namespace + id is not unique, then MyBatis throws an exception |
parameterType | The fully qualified name or alias of the parameter type passed in the SQL statement, which is an optional property. | Supports basic data types and complex data types such as JavaBean and Map |
flushCache | This property is used to set whether the secondary cache and local cache will be cleared after performing this operation. The default value is true. | - |
timeout | This property is used to set the timeout time of SQL execution, and if it times out, an exception will be thrown. | - |
statementType | The statement type used when executing SQL, defaults to PREPARED, optional values: STATEMENT, PREPARED and CALLABLE. | - |
Note: There is no resultType attribute in the update tag, and only query operations need to specify the returned result type accordingly.
Pass multiple parameters
In the above example, we only use one String parameter in the update statement. In the actual development process, most of the time, our update statement needs to use multiple parameters. Mybatis provides us with the following 3 ways to pass multiple parameters to the mapper:
- Pass parameters using Map
- Pass parameters using annotations
- Pass parameters using JavaBean
Pass parameters using Map
You can encapsulate parameters into a Map object and pass them to MyBatis' mapper. In the WebsiteMapper interface, define an updateWebsiteByMap() method and use Map to pass parameters.
int updateWebsiteByMap(Map<String, Object> params);
Define an update statement using the update tag and receive parameters passed through the Map
<!--Update statement reception Map Passed parameters--> <update parameterType="map"> update website set name = #{name},url= #{url} where id = #{id} </update>
Pass parameters using annotations
You can also use the @Param annotation provided by MyBatis to pass parameters to the annotator. In the WebsiteMapper interface, define an updateWebsiteByParam() method and use the @Param annotation to pass parameters.
int updateWebsiteByParam(@Param("name") String name, @Param("url") String url, @Param("id") Integer id);
Use the update tag to define an update statement and receive parameters passed through the @Param annotation
<!--Update statement reception @Param Parameters passed by annotation--> <update > update website set name = #{name},url= #{url} where id = #{id} </update>
Pass parameters using JavaBean
In case of too many parameters, the parameters can be encapsulated into JavaBean (entity class) objects through the setter method.
<!--Update statement reception JavaBean Passed parameters--> <update parameterType=""> update website set name = #{name},url= #{url} where id = #{id} </update>
the difference
The differences between the above 3 methods are as follows:
- Passing parameters using Map will lead to the loss of business readability, which will lead to difficulties in subsequent expansion and maintenance, so in practical applications we should decisively discard this method.
- Passing parameters using @Param annotation will be affected by the number of parameters. When n≤5, it is the best way to pass parameters because it is more intuitive; when n>5, multiple parameters will cause difficulties in calling.
- When the number of parameters is greater than 5, it is recommended to use the JavaBean method.
This is the end of this article about the MyBatis update tag. For more related contents of the MyBatis update tag, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!