测试环境
- springboot:1.5.4-RELEASE
- mybatis:3.4.1
- Mybatis-spring:1.3.0
select list
-
SQL语句
@Select("select * from order where uid=#{uid}") List<Order> selectList(@Param("uid") long uid);
-
Select result 为List的话,如果没有查询到相关结果,会返回new ArrayList
-
判断为空不能用==null,可以使用CollectionUtils.isEmpty()方法
-
源码分析
Spring集成Mybatis,最终会走org.apache.ibatis.executor.resultset.DefaultResultSetHandler的handleResultSets(Statement stmt)方法,该方法中,会先new ArrayList,当查询结果有值的话,利用查询结果填充该list
select entity
-
sql语句:与list的完全相同,只是返回值不同
@Select("select * from order where uid=#{uid}") Order selectList(@Param("uid") long uid);
-
Select result为实体类的话,如果没有查询到结果,会返回null
-
判断为空,用==null
-
源码分析
如下图,返回实体类的话,会走org.apache.ibatis.session.defaults.DefaultSqlSession的selectOne方法,该方法内部调用selectList。当list为空的时候,会直接返回null。所以在查询不到结果时,会返回null
结论
- select list,如果为空,则返回空的list[]
- select entity,如果为空,则返回null