MyBatis測(cè)試報(bào)錯(cuò):Cannot?determine?value?type?from?string?'xxx'的解決辦法
關(guān)于[Cannot determine value type from string ‘xxx’]的問(wèn)題
1、產(chǎn)生
實(shí)體類Student中屬性有id,name,email,age,未使用無(wú)參構(gòu)造器
在mapper.xml中只查詢name,email,age
測(cè)試時(shí)報(bào)錯(cuò)
Cannot determine value type from string '張三'
2、解決
實(shí)體類Student中添加無(wú)參構(gòu)造器
得到結(jié)果
Student{id=null, name='張三', email='zhangsan@126.com', age=22}
3、探究
跟蹤源碼
org.apache.ibatis.executor.resultset.DefaultResultSetHandler
這個(gè)類有個(gè)方法createResultObject
private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix) throws SQLException { final Class<?> resultType = resultMap.getType(); final MetaClass metaType = MetaClass.forClass(resultType, reflectorFactory); final List<ResultMapping> constructorMappings = resultMap.getConstructorResultMappings(); if (hasTypeHandlerForResultObject(rsw, resultType)) { return createPrimitiveResultObject(rsw, resultMap, columnPrefix); } else if (!constructorMappings.isEmpty()) { return createParameterizedResultObject(rsw, resultType, constructorMappings, constructorArgTypes, constructorArgs, columnPrefix); } else if (resultType.isInterface() || metaType.hasDefaultConstructor()) { return objectFactory.create(resultType); } else if (shouldApplyAutomaticMappings(resultMap, false)) { return createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs); } throw new ExecutorException("Do not know how to create an instance of " + resultType); }
這個(gè)方法是根據(jù)結(jié)果集返回值的類型創(chuàng)建出相應(yīng)的bean字段對(duì)象
- 當(dāng)實(shí)體使用無(wú)參構(gòu)造器時(shí)
mybatis會(huì)調(diào)用createResultObject方法中objectFactory.create(resultType),使用無(wú)參構(gòu)造器創(chuàng)建對(duì)象
private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) { try { Constructor<T> constructor; if (constructorArgTypes == null || constructorArgs == null) { constructor = type.getDeclaredConstructor(); try { return constructor.newInstance(); } catch (IllegalAccessException e) { if (Reflector.canControlMemberAccessible()) { constructor.setAccessible(true); return constructor.newInstance(); } else { throw e; } } } constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[0])); try { return constructor.newInstance(constructorArgs.toArray(new Object[0])); } catch (IllegalAccessException e) { if (Reflector.canControlMemberAccessible()) { constructor.setAccessible(true); return constructor.newInstance(constructorArgs.toArray(new Object[0])); } else { throw e; } } } catch (Exception e) { String argTypes = Optional.ofNullable(constructorArgTypes).orElseGet(Collections::emptyList) .stream().map(Class::getSimpleName).collect(Collectors.joining(",")); String argValues = Optional.ofNullable(constructorArgs).orElseGet(Collections::emptyList) .stream().map(String::valueOf).collect(Collectors.joining(",")); throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e); } }
當(dāng)實(shí)體使用有參構(gòu)造參數(shù)
mybatis會(huì)調(diào)用createResultObject方法中createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs);
private Object createUsingConstructor(ResultSetWrapper rsw, Class<?> resultType, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, Constructor<?> constructor) throws SQLException { boolean foundValues = false; for (int i = 0; i < constructor.getParameterTypes().length; i++) { Class<?> parameterType = constructor.getParameterTypes()[i]; String columnName = rsw.getColumnNames().get(i); TypeHandler<?> typeHandler = rsw.getTypeHandler(parameterType, columnName); Object value = typeHandler.getResult(rsw.getResultSet(), columnName); constructorArgTypes.add(parameterType); constructorArgs.add(value); foundValues = value != null || foundValues; } return foundValues ? objectFactory.create(resultType, constructorArgTypes, constructorArgs) : null; }
? 這個(gè)代碼片段里面有個(gè)TypeHandler,這個(gè)是mybatis的類型處理器,用來(lái)處理 JavaType 與 JdbcType 之間的轉(zhuǎn)換。
由代碼我們看出,當(dāng)實(shí)體使用有參構(gòu)造函數(shù)時(shí),會(huì)遍歷有參構(gòu)造參數(shù)個(gè)數(shù),根據(jù)有參構(gòu)造參數(shù)下標(biāo) 查找相應(yīng)的數(shù)據(jù)庫(kù)字段名稱,根據(jù)有參構(gòu)造字段類型以及數(shù)據(jù)庫(kù)字段名稱找類型處理器。然后使用TypeHandler來(lái)處理JavaType 與 JdbcType 之間的轉(zhuǎn)換。當(dāng)轉(zhuǎn)換異常,就會(huì)報(bào)錯(cuò)。
4、總結(jié)
解決Cannot determine value type from string 'xxx’的方法有2種
- 實(shí)體加無(wú)參構(gòu)造參數(shù)
- mapper.xml中查詢的數(shù)據(jù)庫(kù)字段屬性的類型要和有參構(gòu)造器的字段類型一一匹配;查詢字段的個(gè)數(shù)要和有參構(gòu)造器個(gè)數(shù)一樣
到此這篇關(guān)于MyBatis測(cè)試報(bào)錯(cuò):Cannot determine value type from string 'xxx'解決的文章就介紹到這了,更多相關(guān)Cannot determine value type from string內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于mybatis中test條件中單引號(hào)雙引號(hào)的問(wèn)題
這篇文章主要介紹了基于mybatis中test條件中單引號(hào)雙引號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Scala之文件讀取、寫(xiě)入、控制臺(tái)操作的方法示例
這篇文章主要介紹了Scala之文件讀取、寫(xiě)入、控制臺(tái)操作的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Spring?Cloud?Gateway?遠(yuǎn)程代碼執(zhí)行漏洞(CVE-2022-22947)的過(guò)程解析
Spring?Cloud?Gateway?是基于?Spring?Framework?和?Spring?Boot?構(gòu)建的?API?網(wǎng)關(guān),它旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單、有效、統(tǒng)一的?API?路由管理方式,這篇文章主要介紹了Spring?Cloud?Gateway?遠(yuǎn)程代碼執(zhí)行漏洞(CVE-2022-22947),需要的朋友可以參考下2022-08-08JavaWeb連接數(shù)據(jù)庫(kù)MySQL的操作技巧
數(shù)據(jù)庫(kù)是編程中重要的一部分,它囊括了數(shù)據(jù)操作,數(shù)據(jù)持久化等各方面。在每一門(mén)編程語(yǔ)言中都占有相當(dāng)大的比例。本次,小編以MySQL為例,使用mvc編程思想,給大家講解下javaweb對(duì)數(shù)據(jù)庫(kù)的操作2017-02-02Spring事務(wù)失效場(chǎng)景原理及解決方案
這篇文章主要介紹了Spring事務(wù)失效場(chǎng)景原理及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09spring-boot中的SPI機(jī)制實(shí)例講解
這篇文章主要介紹了spring-boot中的SPI機(jī)制實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07spring webflux自定義netty 參數(shù)解析
這篇文章主要介紹了spring webflux自定義netty 參數(shù)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09springboot定時(shí)任務(wù)備份mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
為了防止數(shù)據(jù)庫(kù)被清庫(kù)或者誤刪數(shù)據(jù)庫(kù)的情況,所以需要定時(shí)將mysql數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行備份,本文主要介紹了springboot定時(shí)任務(wù)備份mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03