Mybatis返回One和List的区分方式

这篇文章主要介绍“Mybatis 返回 One 和 List 的区分方式”,在日常操作中,相信很多人在Mybatis 返回 One 和 List 的区分方式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Mybatis 返回 One 和 List 的区分方式”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联专业为企业提供滨海网站建设、滨海做网站、滨海网站设计、滨海网站制作等企业网站建设、网页设计与制作、滨海企业网站模板建站服务,十余年滨海做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

返回一个对象

官网示例:<T> T selectOne(String statement)

结果: 可能为 null。

注: 使用返回一个对象时,若数据库查询结果超过一条 record,Mybatis 则抛出非检查类异常 TooManyResultsException eg. : org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2 证明查询出两条

返回一个 List

官网示例:<E> List<E> selectList(String statement)

结果: 如果没有返回值,那么就是一个空的 list,不会出现 null 的情况,所以使用时不用进行判空,直接循环处理即可。

官网对接口定义的解释:

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)

到此,关于“Mybatis 返回 One 和 List 的区分方式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!

本文名称:Mybatis返回One和List的区分方式
网页URL:https://www.cdcxhl.com/article22/jpoojc.html

成都网站建设公司_创新互联,为您提供Google小程序开发网页设计公司响应式网站自适应网站网站维护

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设