一、如何使用
当数据库返回的数据是null,那么使用resultType="java.lang.Integer"
当数据库返回的数据是0,既可以使用resultType="int"
也可以使用resultType="java.lang.Integer"
二、案例
工具:mybatis+pgsql
resultType="java.lang.Integer" 使用代码块
dao代码:
Integer selectMaxAgeBySex(String sex); 查询性别是女生的最大年龄
sql:
<select id = "selectMaxAgeBySex" resultType="java.lang.Integer">
select max(age) from user where sex = '女';
</select>
为什么要使用Integer?因为当我把s q l语句放到p g s q l数据库执行时,如果查不到数据,那么p g s q l返回nu l l。此时需要使用java.lang.Integer作为返回类型,不可以使用resultType="int"
,否则会报错。报错信息参考文章 mapper method attempted to return null from a method with a primitive return type (int)
resultType="int" 使用代码块
dao代码:
int count(); 查询用户的总量
sql:
<select id = "count" resultType="int">
select count(1) from user;
</select>
为什么可以使用int?因为当我把s q l语句放到p g s q l数据库执行时,如果查不到数据,那么p g s q l返回0。此时可以使用int作为返回类型,也可以使用java.lang.Integer作为返回值