关于MyBatis mapper的insert, update, delete返回值

这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html

SqlSession

As mentioned above, the SqlSession instance is the most powerful class in MyBatis. It is where you'll find all of the methods to execute statements, commit or rollback transactions and acquire mapper instances.

There are over twenty methods on the SqlSession class, so let's break them up into more digestible groupings.

Statement Execution Methods
These methods are used to execute SELECT, INSERT, UPDATE and DELETE statements that are defined in your SQL Mapping XML files. They are pretty self explanatory, each takes the ID of the statement and the Parameter Object, which can be a primitive (auto-boxed or wrapper), a JavaBean, a POJO or a Map.

复制代码
<T> T selectOne(String statement, Object parameter)
<E> List<E> selectList(String statement, Object parameter)
<K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey)
int insert(String statement, Object parameter)
int update(String statement, Object parameter)
int delete(String statement, Object parameter)
复制代码

The difference between selectOne and selectList is only in that selectOne must return exactly one object or null (none). If any more than one, an exception will be thrown. If you don't' know how many objects are expected, use selectList. If you want to check for the existence of an object, you're better off returning a count (0 or 1). The selectMap is a special case in that it is designed to convert a list of results into a Map based on one of the properties in the resulting objects. Because not all statements require a parameter, these methods are overloaded with versions that do not require the parameter object.

The value returned by the insert, update and delete methods indicate the number of rows affected by the statement.

复制代码
<T> T selectOne(String statement)
<E> List<E> selectList(String statement)
<K,V> Map<K,V> selectMap(String statement, String mapKey)
int insert(String statement)
int update(String statement)
int delete(String statement)
复制代码

而在mapper的xml中, 不需要(也不能)给insert, update, delete指定resultType, 如这里所解释

http://mybatis.github.io/mybatis-3/sqlmap-xml.html

insert, update, delete只支持这些Attributes: id, parameterType, parameterMap, flushCache, timeout, statementType, useGeneratedKeys, keyProperty, keyColumn, databaseId

MyBatis在执行select语句时,如果没有在Mapper映射文件中显示声明返回值类型,会抛出异常,指出"A query was run and no Result Maps were found for the Mapped Statement"。\[3\]这意味着在Mapper映射文件中必须指定返回值类型,且该返回值类型需要与Mapper接口中声明的返回值类型一致,可以是Integer、Long、int或long,但不能返回Boolean类型。\[3\]对于insertupdatedelete语句,MyBatis会使用rowCountResult方法对返回值进行转换,如果返回的值大于0,则返回True,否则返回False。\[1\]对于query函数,如果工程配置的defaultExecutorType是REUSE,MyBatis会使用ReuseExecutor的doQuery方法进行查询,但无法确定list返回是否为null。\[2\] #### 引用[.reference_title] - *1* *3* [MyBatisMapper返回值类型](https://blog.csdn.net/tantiao666/article/details/106344829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [探索MybatisMapper默认返回值](https://blog.csdn.net/zhuqiuhui/article/details/87899146)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值