昨天领导让我做一个查询功能,涉及到多个表单的查询。发现以前做的都是单表查询。一开始按单表查询的样式写,一直报错数据库报错,以下是原代码:
<select id="findCityIdByUsername" resultType="com.indechy.nyj.bean.BusinessSwitch">
SELECT
t_user_enterprises.competentAuthority t_user_enterprises.competent_authority,
t_user_enterprises.username,
t_user_enterprises.leadingLevel t_user_enterprises.leading_level,
batch.bacth,
history.timeOfSubmission history.time_of_submission
FROM
history,t_user_enterprises,batch
WHERE
history.eid=t_user_enterprises.id
AND
history.batch_id=batch.id
AND
history.time_of_submission like CONCAT(CONCAT('%',#{age} ,'%'))
AND
history.record_type like CONCAT(CONCAT('%',#{recordType} ,'%'))
AND
batch.bacth like CONCAT(CONCAT('%',#{bacth} ,'%'))
AND
t_user_enterprises.competent_authority like CONCAT(CONCAT('%',#{competentAuthority},'%'))
AND
t_user_enterprises.leading_level like CONCAT(CONCAT('%',#{leadingLevel} ,'%'))
AND
t_user_enterprises.username like CONCAT(CONCAT('%',#{username} ,'%'))
AND
history.time_of_submission>= #{startTimeOfSubmission}
AND
history.time_of_submission<= #{endTimeOfSubmission}
GROUP BY
t_user_enterprises.username
</select>
这样写首先在后面的 history.time_of_submission<= #{endTimeOfSubmission}中<=号就会报错,原因是xml语法中大于小于分别需要用>和<代替。其次这样一直报错 TODO DOT t_user_enterprises。
原因就是数据库mapper的映射文件中出错
后经过检查发现select里面多表查询的别名不需要加表明,因为其是对用bean类中的变量,并不对应数据库中的,只需要把前面的“表名.”去掉即可查询。
修改后代码为:
<select id="findCityIdByUsername" resultType="com.indechy.nyj.bean.BusinessSwitch">
SELECT
t_user_enterprises.competent_authority competentAuthority,
t_user_enterprises.username username,
t_user_enterprises.leading_level leadingLevel,
batch.bacth bacth,
history.time_of_submission timeofsubmission
FROM
history,t_user_enterprises,batch
WHERE
history.eid=t_user_enterprises.id
AND
history.batch_id=batch.id
AND
history.time_of_submission like CONCAT(CONCAT('%',#{age} ,'%'))
AND
history.record_type like CONCAT(CONCAT('%',#{recordType} ,'%'))
AND
batch.bacth like CONCAT(CONCAT('%',#{bacth} ,'%'))
AND
t_user_enterprises.competent_authority like CONCAT(CONCAT('%',#{competentAuthority},'%'))
AND
t_user_enterprises.leading_level like CONCAT(CONCAT('%',#{leadingLevel} ,'%'))
AND
t_user_enterprises.username like CONCAT(CONCAT('%',#{username} ,'%'))
AND
history.time_of_submission > #{startTimeOfSubmission}
and
history.time_of_submission <= #{endTimeOfSubmission}
GROUP BY
t_user_enterprises.username
</select>