困扰了我一天的问题终于解决了 但是还是没想明白为什么。。
我的需求:分页(模糊/条件)查询教师列表
定义了两个接口:一个查询 一个计算数量,如下:
我的XML:
<select id="pageQuery" parameterType="cn.duofen.produce.entity.Teacher" resultMap="baseMap">
SELECT
t.id, t.school_id, t.class_id, t.name, t.phone, t.grade_id,
t.subject, t.role_id, t.status, t.create_time,
g.school_name, g.name as gradeName, g.grade_in, g.grade_out,
c.name as className
FROM teacher AS t
LEFT JOIN grade as g
ON t.grade_id = g.grade_id
LEFT JOIN clazz AS c
ON FIND_IN_SET(c.class_id, t.class_id)
<where>
<if test="teacher.gradeId != null">
and t.grade_id = #{teacher.gradeId}
</if>
<if test="teacher.schoolId != null">
and t.school_id = #{teacher.schoolId}
</if>
<if test="teacher.subject != null">
and t.subject = #{teacher.subject}
</if>
<if test="teacher.name != null">
and t.name like concat('%', #{teacher.name}, '%')
</if>
<if test="teacher.classId != null">
and FIND_IN_SET(#{teacher.classId}, t.class_id)
</if>
</where>
limit #{page},#{size}
</select>
<select id="getTchCount" parameterType="cn.duofen.produce.entity.Teacher" resultType="java.lang.Integer">
SELECT
count(*)
FROM teacher AS t
<where>
<if test="teacher.gradeId != null">
and t.grade_id = #{teacher.gradeId}
</if>
<if test="teacher.schoolId != null">
and t.school_id = #{teacher.schoolId}
</if>
<if test="teacher.subject != null">
and t.subject = #{teacher.subject}
</if>
<if test="teacher.name != null">
and t.name like concat('%', #{teacher.name}, '%')
</if>
<if test="teacher.classId != null">
and FIND_IN_SET(#{teacher.classId}, t.class_id)
</if>
</where>
</select>
然而,运行时,可以看出查询可以正常执行,而计算数量总是报错:
There is no getter for property named 'teacher' in 'class cn.duofen.produce.entity.Teacher'
我懵了真的!!
两个xml写的几乎一模一样啊,就是getTchCount没有连接其他的表而已,因为真的不需要。但是无论怎么改,就是一直报错。
然后我直接把select的直接复制到下面,出了id和count(*)跟select不一样,其他都一模一样了,我想着这下该行了吧,结果还是报错。。。
就是select没有毛病 而getTchCount报错:no getter 吧啦吧啦。。。。
经过我的一点点排查,我终于找到了解决办法,如下:
我也不明白为什么 select中不加parameterType可以正常运行,但getTchCount不行,而且getTchCount加了parameterType之后就不再需要用teacher.属性了,如果用teacher.gradeId等,就会报:There is no getter for property named 'teacher' in 'class cn.duofen.produce.entity.Teacher'
虽然我真的不知道是为什么,但是问题也总算是解决了。
最后,如果不需要用teacher去点了,也可以不用@Param注解了
不懂 真的不懂。。。。