日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

mybatis中传入String类型参数的问题

發布時間:2025/3/20 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis中传入String类型参数的问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 出現的問題

需求是想寫一個按公司名字查詢公司列表的功能,最開始的代碼如下
Dao層接口如下:

@MyBatisDao public interface OfficeDao extends TreeDao<Office> {List<Office> findCompanyNameList(String name); }

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类型参数的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。