mybaits中设置的返回值类型为boolean类型,当查询的结果大于1时返回True而不是false

在Mybatis项目中,遇到一个需要返回布尔值来确定某条记录是否存在的需求。由于查询的结果可能是0、1或者>1,就想确认一下,当resultType为boolean类型时,Mybatis是怎么转换的。查询了一下博客,一些博客说:当记录数行数是1时返回true,行数为0时返回false, 如果查询到多条记录(大于1),返回的却是false。这时我就产生了疑问,自己验证了一下发现,其实当记录数大于1时,返回的是true,而不是false。所以,凡事我们都要自己验证一下,可能版本不同,结果也不同。多动手实践才是真理。


以下是我的验证:

数据库User表信息:

mybaits中设置的返回值类型为boolean类型,当查询的结果大于1时返回True而不是false_第1张图片

查询语句:

  
    

    

    
    @Test
    public void test(){

        boolean exist = userMapper.exist(12);
        System.out.println("exist:  "+exist);

        int userNumbers = userMapper.getUserNumbers(12);
        System.out.println("userNumbers: "+userNumbers);

        List list = userMapper.getUserInfo(12);
        list.forEach(user-> System.out.println(user));

        boolean existAge100 = userMapper.exist(100);
        System.out.println("existAge100:  "+existAge100);
    }

结果:

10:36:01.492 [main] DEBUG c.q.juint.test.dao.UserMapper.exist - ==>  Preparing: SELECT count(1) FROM user WHERE age = ?; 
10:36:01.511 [main] DEBUG c.q.juint.test.dao.UserMapper.exist - ==> Parameters: 12(Integer)
10:36:01.527 [main] DEBUG c.q.juint.test.dao.UserMapper.exist - <==      Total: 1
10:36:01.530 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c1fca1e]
exist:  true
10:36:01.530 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c1fca1e] from current transaction
10:36:01.530 [main] DEBUG c.q.j.t.d.UserMapper.getUserNumbers - ==>  Preparing: SELECT count(1) FROM user WHERE age = ?; 
10:36:01.530 [main] DEBUG c.q.j.t.d.UserMapper.getUserNumbers - ==> Parameters: 12(Integer)
10:36:01.531 [main] DEBUG c.q.j.t.d.UserMapper.getUserNumbers - <==      Total: 1
10:36:01.531 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c1fca1e]
userNumbers: 3
10:36:01.531 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c1fca1e] from current transaction
10:36:01.531 [main] DEBUG c.q.j.t.dao.UserMapper.getUserInfo - ==>  Preparing: SELECT `id` as id, `age` as age, `name` as name FROM user WHERE age = ?; 
10:36:01.531 [main] DEBUG c.q.j.t.dao.UserMapper.getUserInfo - ==> Parameters: 12(Integer)
10:36:01.535 [main] DEBUG c.q.j.t.dao.UserMapper.getUserInfo - <==      Total: 3
10:36:01.535 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c1fca1e]
id:2  age:12  name:小花
id:3  age:12  name:小兰
id:7  age:12  name:大师兄
10:36:01.535 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c1fca1e] from current transaction
10:36:01.535 [main] DEBUG c.q.juint.test.dao.UserMapper.exist - ==>  Preparing: SELECT count(1) FROM user WHERE age = ?; 
10:36:01.536 [main] DEBUG c.q.juint.test.dao.UserMapper.exist - ==> Parameters: 100(Integer)
10:36:01.536 [main] DEBUG c.q.juint.test.dao.UserMapper.exist - <==      Total: 1
10:36:01.537 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c1fca1e]
existAge100:  false
10:36:01.537 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c1fca1e]
10:36:01.537 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c1fca1e

总结:

通过结果。我们可知,当记录数行数是1时返回true,行数为0时返回false, 如果查询到多条记录(大于1),返回true。

你可能感兴趣的:(mybatis)