方法一:
<!-- 根據hid,hanme,grade,模糊查詢醫院信息-->
方法一:
List<Hospital> getHospitalLike(@Param("selectword") String selectword);
<select id="getHospitalLike" resultType="com.hand.hand.domain.Hospital">
SELECT *
FROM hospital
where hid=cast(#{selectword} as signed INTEGER ) OR hname like concat('%',#{selectword},'%')
OR grade like concat('%',#{selectword},'%')
</select>
where hid=cast(#{selectword} as signed INTEGER ) hid為Integer類型,而參數selectword為string類型,所以用cast將string轉化為Integer
where hname like concat('%',#{selectword},'%') 字符串拼接,將%和selectword拼接成%selectword%
方法二:動態sql中的bind
List<Hospital> getHospitalLike(@Param("selectword") String selectword);
<select id="getHospitalLike" resultType="com.hand.hand.domain.Hospital">
<bind name="bindselectword" value="'%'+selectword+'%'"></bind>
SELECT *
FROM hospital
<if test="selectword!=null">
where hid=cast(#{selectword} as signed INTEGER ) OR hname like #{bindselectword}
OR grade like #{bindselectword}
</if>
</select>
方法三:
在service層直接拼接字符串,xml直接使用轉入的參數
此方法測試出錯(參考博客后,測試出錯):
<!--where hid like '%'||#{selectword}||'%' or hname like '%'||#{selectword}||'%' or grade like '%'||#{selectword}||'%'-->
其他拼接法,可參考:https://www.cnblogs.com/dushan/p/4766954.html