解決Mybatis報錯:org.apache.ibatis.reflection.ReflectionException: There is no getter for property named問題
錯誤提示
服務(wù)器處理發(fā)生異常:
nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘userTaskqueryDTO’ in ‘class com.lz.platform.trauma.api.interfaces.dto.task.UserTaskqueryDTO’
解決方式
可能錯誤原因一
解決方法一:
DTO沒有寫getter/setter方法,需要添加上。SpringBoot在DTO上加@Data
@Data是一個Lombok提供的注解,可以自動為Java類生成一些常用的方法,包括getter、setter、toString、equals、hashCode等方法,從而簡化Java類的編寫。
使用@Data注解可以讓代碼更加簡潔,提高開發(fā)效率。
@Data @ApiModel(value = "模糊查詢列表") public class UserTaskqueryDTO implements Serializable { private String idno; private String Code; private String keyWord; }
可能錯誤原因二
解決方法二:
DAO文件上沒有加上注入方法@Repository
DAO文件上需要加上@Repository
@Repository是Spring框架中的一個注解,用于標識一個類作為數(shù)據(jù)訪問對象(DAO)的組件。它的作用是將DAO層的Bean標記為Spring容器中的Bean,并自動執(zhí)行Bean的注冊、依賴注入等操作。
可能錯誤原因三
解決方法三:
JAVA中Mapper文件不識別#{dto.propertyName}語法,將dto.去掉
Mapper文件中方法DTO注入不能帶上DTO名稱。
錯誤Mapper:
<select id="queryUserTraumaTaskList" resultType="com.lz.task.TraumaTask" parameterType="com.lz.task.UserTaskqueryDTO"> select * from task t where 1=1 <if test=" userTaskqueryDTO.no != null and userTaskqueryDTO.no != ''"> and t.NO = #{userTaskqueryDTO.no} </if> <if test="userTaskqueryDTO.keyWord != null and userTaskqueryDTO.keyWord !=''"> and ( t.STERNO like concat( concat('%',#{userTaskqueryDTO.keyWord}),'%') or t.NAME like concat( concat('%',#{userTaskqueryDTO.keyWord}),'%') or t.IClass like concat( concat('%',#{userTaskqueryDTO.keyWord}),'%')) </if> order by t.date desc </select>
按照上邊的寫法會出現(xiàn)報錯:
服務(wù)器處理發(fā)生異常:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘userTaskqueryDTO’ in ‘class com.lz.platform.trauma.api.interfaces.dto.task.UserTaskqueryDTO’
下邊的寫法才是正確的寫法:
把DTO去掉之后,報錯就消失了,加了DTO系統(tǒng)讀取不到,去掉之后句好了
<select id="queryUserTraumaTaskList" resultType="com.lz.task.TraumaTask" parameterType="com.lz.task.UserTaskqueryDTO"> select * from task t where 1=1 <if test=" no != null and no != ''"> and t.NO = #{no} </if> <if test="keyWord != null and keyWord !=''"> and ( t.STERNO like concat( concat('%',#{keyWord}),'%') or t.NAME like concat( concat('%',#{keyWord}),'%') or t.IClass like concat( concat('%',#{keyWord}),'%')) </if> order by t.date desc </select>
錯誤原因
在Mapper文件中,使用DTO(Data Transfer Object)來傳遞數(shù)據(jù),可以將數(shù)據(jù)從Java代碼傳輸?shù)綌?shù)據(jù)庫,也可以將數(shù)據(jù)從數(shù)據(jù)庫傳輸?shù)絁ava代碼。
通常情況下,我們將DTO中的屬性名與數(shù)據(jù)庫表的列名進行對應(yīng),以便在Mapper文件中使用這些屬性來構(gòu)建SQL語句。
- 在使用DTO的屬性名填充Mapper文件數(shù)據(jù)時,不需要在屬性名前面添加DTO名稱。這是因為DTO通常是一個Java類,而不是一個數(shù)據(jù)庫表,它的屬性名稱是在Java類中定義的,而不是在數(shù)據(jù)庫表中定義的。
- 在Mapper文件中,我們使用#{propertyName}來引用DTO的屬性。如果我們在屬性名前面添加了DTO名稱,例如#{dto.propertyName},則會導致語法錯誤,因為Mapper文件不能識別這個語法。
因此,在使用DTO的屬性名填充Mapper文件數(shù)據(jù)時,應(yīng)該只使用屬性名本身,而不需要添加DTO名稱。
例如,如果DTO中有一個屬性名為id,我們可以在Mapper文件中使用#{id}來引用這個屬性,而不需要使用#{dto.id}。
可能錯誤原因四
解決方法四:
sql語句中的屬性名稱,跟接收,或者入?yún)⒌膶傩悦Q不一致
例如:我傳進來的參數(shù)名稱為tmID,但是我sql語句里邊寫的確是tmhosID
把屬性名稱改為一致就可以了
注意:
- 出參和入?yún)⒌念愋筒灰e了
- 出參是一個list的話,resultType放的是list參數(shù)的類型。
- 例如:出參List《TraumaTask》 ,resultType放的就是TraumaTask路徑
- 出參List《String 》,resultType放的就是java,util.lang
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 解決mybatis generator MySQL自增ID出現(xiàn)重復問題MySQLIntegrityConstraintViolationException
- MyBatis嵌套查詢collection報錯:org.apache.ibatis.exceptions.TooManyResultsException
- 解決springboot3:mybatis-plus依賴錯誤:org.springframework.beans.factory.UnsatisfiedDependencyException
- 解決Mybatis出現(xiàn)報錯Error querying database.Cause: java.lang.IndexOutOfBoundsException: Index 9 out of
- 解決mybatis plus報錯com.microsoft.sqlserver.jdbc.SQLServerException:必須執(zhí)行該語句才能獲得結(jié)果
- 關(guān)于MyBatisSystemException異常產(chǎn)生的原因及解決過程
相關(guān)文章
java.io.File的renameTo方法移動文件失敗的解決方案
這篇文章主要介紹了java.io.File的renameTo方法移動文件失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07ByteArrayOutputStream與InputStream互相轉(zhuǎn)換方式
這篇文章主要介紹了ByteArrayOutputStream與InputStream互相轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot項目統(tǒng)一枚舉轉(zhuǎn)換實踐過程
文章介紹了在Spring Boot項目中統(tǒng)一枚舉轉(zhuǎn)換的實踐,通過使用自定義的父枚舉接口和AttributeConverter、ConverterFactory、JsonSerializer和JsonDeserializer等工具,實現(xiàn)了枚舉與數(shù)據(jù)庫、請求參數(shù)、響應(yīng)參數(shù)和消息參數(shù)之間的自動轉(zhuǎn)換2024-12-12