mybatis中传入String类型参数的问题
生活随笔
收集整理的這篇文章主要介紹了
mybatis中传入String类型参数的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 出現的問題
需求是想寫一個按公司名字查詢公司列表的功能,最開始的代碼如下
Dao層接口如下:
mybatis的xml代碼:
<select id="findCompanyNameList" parameterType="java.lang.String" resultType="com.pds.modules.sys.entity.Office">SELECT id,name FROM sys_office where o.del_flag = '1'<if test="name!= null and name!= ''">AND name LIKE concat('%',#{name},'%')</if> </select>這樣寫會報錯,大體意思是name沒有Getter方法
2. 解決辦法
2.1 解決辦法1
在接口參數里加上mybatis中的@param注解
@MyBatisDao public interface OfficeDao extends TreeDao<Office> {List<Office> findCompanyNameList(@Param("name")String name); } <select id="findCompanyNameList" parameterType="java.lang.String" resultType="com.pds.modules.sys.entity.Office">SELECT id,name FROM sys_office where o.del_flag = '1'<if test="name!= null and name!= ''">AND name LIKE concat('%',#{name},'%')</if> </select>2.2 解決辦法2
在xml的if里用"_parameter" 代表參數
<select id="findCompanyNameList" parameterType="java.lang.String" resultType="com.pds.modules.sys.entity.Office">SELECT id,name FROM sys_office where o.del_flag = '1'<if test="_parameter!= null and _parameter!= ''">AND name LIKE concat('%',#{name},'%')</if> </select>2.3 兩種方法區別
可以看出,_parameter不能區分多個參數,而@param能。所以@param能傳多個這樣的參數
總結
以上是生活随笔為你收集整理的mybatis中传入String类型参数的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: APICloud中app如何在手机端测试
- 下一篇: 重新理解@Resource注解