mybaits中設定的返回值型別為boolean型別,當查詢的結果大於1時返回True而不是false
阿新 • • 發佈:2018-12-20
在Mybatis專案中,遇到一個需要返回布林值來確定某條記錄是否存在的需求。由於查詢的結果可能是0、1或者>1,就想確認一下,當resultType為boolean型別時,Mybatis是怎麼轉換的。查詢了一下部落格,一些部落格說:當記錄數行數是1時返回true,行數為0時返回false, 如果查詢到多條記錄(大於1),返回的卻是false。這時我就產生了疑問,自己驗證了一下發現,其實當記錄數大於1時,返回的是true,而不是false。所以,凡事我們都要自己驗證一下,可能版本不同,結果也不同。多動手實踐才是真理。
以下是我的驗證:
資料庫User表資訊:
查詢語句:
<select id="exist" parameterType="Integer" resultType="java.lang.Boolean"> SELECT count(1) FROM user WHERE age = #{age}; </select> <select id="getUserNumbers" parameterType="Integer" resultType="java.lang.Integer"> SELECT count(1) FROM user WHERE age = #{age}; </select> <select id="getUserInfo" parameterType="Integer" resultType="com.qiqi.juint.test.model.vo.UserVO"> SELECT `id` as id, `age` as age, `name` as name FROM user WHERE age = #{age}; </select>
@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<UserVO> 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 [ [email protected]]
exist: true
10:36:01.530 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [[email protected]] 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 [[email protected]]
userNumbers: 3
10:36:01.531 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Fetched SqlSession [[email protected]] 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 [[email protected]]
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 [[email protected]] 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 [[email protected]]
existAge100: false
10:36:01.537 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization deregistering SqlSession [[email protected]]
10:36:01.537 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [[email protected]
總結:
通過結果。我們可知,當記錄數行數是1時返回true,行數為0時返回false, 如果查詢到多條記錄(大於1),返回true。