mybatis中传入String类型参数的问题
生活随笔
收集整理的這篇文章主要介紹了
mybatis中传入String类型参数的问题
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 出現(xiàn)的問題
需求是想寫一個(gè)按公司名字查詢公司列表的功能,最開始的代碼如下
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>這樣寫會(huì)報(bào)錯(cuò),大體意思是name沒有Getter方法
2. 解決辦法
2.1 解決辦法1
在接口參數(shù)里加上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" 代表參數(shù)
<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 兩種方法區(qū)別
可以看出,_parameter不能區(qū)分多個(gè)參數(shù),而@param能。所以@param能傳多個(gè)這樣的參數(shù)
總結(jié)
以上是生活随笔為你收集整理的mybatis中传入String类型参数的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: APICloud中app如何在手机端测试
- 下一篇: 重新理解@Resource注解