返回boolean的mybatis查询


注意:返回数量为0时函数返回值为false,返回数量为非零值时为true。

Java函数:

boolean hasSameServiceCode(@Param("oldDepotCd") String oldDepotCd,@Param("newDepotCd") String newDepotCd);

XML代码:

    <select id="hasSameServiceCode" resultType="java.lang.Boolean">
        select (case when count(*)=1 then 1 else 0 end) as cnt from (
            select distinct s.service_code 
            from depot d inner join service_carrier_mapping s
            on d.carrier_code=s.handling_carrier_code
            where exists (select null from DEPOT where depot_code=#{oldDepotCd})
            and exists (select null from DEPOT where depot_code=#{newDepotCd})
            and d.depot_code in (#{oldDepotCd},#{newDepotCd}) )
    </select>

取值:

cnt=1,返回true

cnt=0,返回false

注意,如果直接写select count(*) from XXX,因为会有0,1, >1三种情况,如果是0,返回值是false,这个没有争议;如果是1,返回值为true;如果是>1,返回值也为true。非零值都是true,这个是我实测了的,并不像网文 https://blog.csdn.net/qq_39039017/article/details/80700002 所说的只有1返回true,0和大于1的值都返回false。

像我上面的业务,只能在返回1时返回真,返回0或者其它值都得返回假,这就需要用case语句来转化一下。

--2020-04-07--

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。