mybatis 查询之神坑
生活随笔
收集整理的這篇文章主要介紹了
mybatis 查询之神坑
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
先看一個示例:
數據表數據:
mybatis類和查詢語句:
1. 當UserInfoMap中所有字段(包含association)都為NULL的話,getUserInfo的返回結果是個null,即使查詢的記錄存在!運行結果如下:
2019-06-26 10:50:00.800 DEBUG 95032 --- [ main] com.zcm.mybatis.dao.UserDao.getUserInfo : ==> Preparing: select name,phone,info_id from d_user where id = ? 2019-06-26 10:50:00.854 DEBUG 95032 --- [ main] com.zcm.mybatis.dao.UserDao.getUserInfo : ==> Parameters: 1(Integer) 2019-06-26 10:50:00.954 DEBUG 95032 --- [ main] c.z.m.d.m.InfoMapper.selectByPrimaryKey : ====> Preparing: select id, card_id, birth, weight, height from d_info where id = ? 2019-06-26 10:50:00.955 DEBUG 95032 --- [ main] c.z.m.d.m.InfoMapper.selectByPrimaryKey : ====> Parameters: 0(Integer) 2019-06-26 10:50:00.956 DEBUG 95032 --- [ main] c.z.m.d.m.InfoMapper.selectByPrimaryKey : <==== Total: 0 2019-06-26 10:50:00.957 DEBUG 95032 --- [ main] com.zcm.mybatis.dao.UserDao.getUserInfo : <== Total: 1 null2. 當UserInfoMap中有字段不為NULL的時候,getUserInfo返回的結果才不為null(前提查詢的記錄存在),如d_user表id=1中,為phone添加值"x",如下圖:
這時再運行測試程序,結果如下:
2019-06-26 11:01:20.794 DEBUG 95861 --- [ main] com.zcm.mybatis.dao.UserDao.getUserInfo : ==> Preparing: select name,phone,info_id from d_user where id = ? 2019-06-26 11:01:20.856 DEBUG 95861 --- [ main] com.zcm.mybatis.dao.UserDao.getUserInfo : ==> Parameters: 1(Integer) 2019-06-26 11:01:20.965 DEBUG 95861 --- [ main] c.z.m.d.m.InfoMapper.selectByPrimaryKey : ====> Preparing: select id, card_id, birth, weight, height from d_info where id = ? 2019-06-26 11:01:20.966 DEBUG 95861 --- [ main] c.z.m.d.m.InfoMapper.selectByPrimaryKey : ====> Parameters: 0(Integer) 2019-06-26 11:01:20.967 DEBUG 95861 --- [ main] c.z.m.d.m.InfoMapper.selectByPrimaryKey : <==== Total: 0 2019-06-26 11:01:20.968 DEBUG 95861 --- [ main] com.zcm.mybatis.dao.UserDao.getUserInfo : <== Total: 1 {"phone":"x"}所以綜合本測試案例,得出的最終結果是:
1. 記錄存在時,不一定返回的對象一定不為null, 還要看查詢的字段是否全部為NULL,或者說是否有不為NULL的字段!
2. 這個測試結果跟是否有關聯查詢無關!
?
總結
以上是生活随笔為你收集整理的mybatis 查询之神坑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机丢失vcomp110.dll,ms
- 下一篇: 无线投屏视频经过服务器吗,无线投屏方案