MyBatis測試報錯:Cannot?determine?value?type?from?string?'xxx'的解決辦法
關于[Cannot determine value type from string ‘xxx’]的問題
1、產(chǎn)生
實體類Student中屬性有id,name,email,age,未使用無參構造器
在mapper.xml中只查詢name,email,age
測試時報錯
Cannot determine value type from string '張三'
2、解決
實體類Student中添加無參構造器
得到結果
Student{id=null, name='張三', email='zhangsan@126.com', age=22}
3、探究
跟蹤源碼
org.apache.ibatis.executor.resultset.DefaultResultSetHandler
這個類有個方法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); }
這個方法是根據(jù)結果集返回值的類型創(chuàng)建出相應的bean字段對象
- 當實體使用無參構造器時
mybatis會調(diào)用createResultObject方法中objectFactory.create(resultType),使用無參構造器創(chuàng)建對象
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); } }
當實體使用有參構造參數(shù)
mybatis會調(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; }
? 這個代碼片段里面有個TypeHandler,這個是mybatis的類型處理器,用來處理 JavaType 與 JdbcType 之間的轉換。
由代碼我們看出,當實體使用有參構造函數(shù)時,會遍歷有參構造參數(shù)個數(shù),根據(jù)有參構造參數(shù)下標 查找相應的數(shù)據(jù)庫字段名稱,根據(jù)有參構造字段類型以及數(shù)據(jù)庫字段名稱找類型處理器。然后使用TypeHandler來處理JavaType 與 JdbcType 之間的轉換。當轉換異常,就會報錯。
4、總結
解決Cannot determine value type from string 'xxx’的方法有2種
- 實體加無參構造參數(shù)
- mapper.xml中查詢的數(shù)據(jù)庫字段屬性的類型要和有參構造器的字段類型一一匹配;查詢字段的個數(shù)要和有參構造器個數(shù)一樣
到此這篇關于MyBatis測試報錯:Cannot determine value type from string 'xxx'解決的文章就介紹到這了,更多相關Cannot determine value type from string內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring?Cloud?Gateway?遠程代碼執(zhí)行漏洞(CVE-2022-22947)的過程解析
Spring?Cloud?Gateway?是基于?Spring?Framework?和?Spring?Boot?構建的?API?網(wǎng)關,它旨在為微服務架構提供一種簡單、有效、統(tǒng)一的?API?路由管理方式,這篇文章主要介紹了Spring?Cloud?Gateway?遠程代碼執(zhí)行漏洞(CVE-2022-22947),需要的朋友可以參考下2022-08-08JavaWeb連接數(shù)據(jù)庫MySQL的操作技巧
數(shù)據(jù)庫是編程中重要的一部分,它囊括了數(shù)據(jù)操作,數(shù)據(jù)持久化等各方面。在每一門編程語言中都占有相當大的比例。本次,小編以MySQL為例,使用mvc編程思想,給大家講解下javaweb對數(shù)據(jù)庫的操作2017-02-02spring webflux自定義netty 參數(shù)解析
這篇文章主要介紹了spring webflux自定義netty 參數(shù)解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09springboot定時任務備份mysql數(shù)據(jù)庫的實現(xiàn)示例
為了防止數(shù)據(jù)庫被清庫或者誤刪數(shù)據(jù)庫的情況,所以需要定時將mysql數(shù)據(jù)庫中的數(shù)據(jù)進行備份,本文主要介紹了springboot定時任務備份mysql數(shù)據(jù)庫的實現(xiàn)示例,需要的朋友們下面隨著小編來一起學習學習吧2024-03-03