使用mybatis根据主键id获取信息,偶尔查出的数据为NULL,解决根据主键id获取null值,MyBatis-plus的根据id获取信息返回nulll可以参考
作者:互联网
业务场景:项目使用了PageHelper和MyBatis,在根据id获取信息时,有时候返回的是null值,报空指针异常。
产生问题的原因
PageHelper默认会在sql 上添加一个Count,会导致执行效率变慢和异常
下图是自己写的SQL
空指针异常后的sql
解决方式:重写PageHelper的Count
示例:
dao层
AlumniUser findOnlyByAluserId(AlumniUser alumniUser);
//重写count
long findOnlyByAluserId_COUNT(AlumniUser alumniUser);
xml
<select id="findOnlyByAluserId" parameterType="mobile.bean.AlumniUser" resultMap="BaseResultMap">
select aluser.alumniuser_id,aluser.`name`,aluser.mobile_no,aluser.email,aluser.head_img,gar.grade_name as gradeName,ma.major_name as majorName
from alumniuser aluser
left join educationinfo educ on educ.alumniuser_id=aluser.alumniuser_id
left join grade gar on educ.grade_id=gar.grade_id
left join workinfo woinfo on woinfo.alumniuser_id=aluser.alumniuser_id
left join major ma on educ.major_id=ma.major_id
where aluser.alumniuser_id=#{alumniuserId}
group by aluser.alumniuser_id
</select>
<select id="findOnlyByAluserId_COUNT" parameterType="mobile.bean.AlumniUser" resultType="long">
select COUNT(1)
from alumniuser aluser
left join educationinfo educ on educ.alumniuser_id=aluser.alumniuser_id
left join grade gar on educ.grade_id=gar.grade_id
left join workinfo woinfo on woinfo.alumniuser_id=aluser.alumniuser_id
left join major ma on educ.major_id=ma.major_id
where aluser.alumniuser_id=#{alumniuserId}
group by aluser.alumniuser_id
</select>
重写PageHelper的参考链接:https://blog.csdn.net/weixin_43917392/article/details/115511352
标签:alumniuser,获取信息,join,aluser,主键,educ,id,left 来源: https://blog.csdn.net/LOVE320722/article/details/120460122