abel533 / Mapper

Mybatis Common Mapper - Easy to use
https://mybatis.io
MIT License
7.34k stars 1.63k forks source link

求教:example 动态sql 查询抛异常 。感谢:) #382

Open guan25 opened 6 years ago

guan25 commented 6 years ago

Error querying database. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_listItem_3'. It was either not specified and/or could not be found for the javaType ([Ljava.lang.Integer;) : jdbcType (null) combination.

Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_listItem_3'. It was either not specified and/or could not be found for the javaType ([Ljava.lang.Integer;) : jdbcType (null) combination.] with root cause

java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_listItem_3'. It was either not specified and/or could not be found for the javaType ([Ljava.lang.Integer;) : jdbcType (null) combination. at org.apache.ibatis.mapping.ParameterMapping$Builder.validate(ParameterMapping.java:119) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.mapping.ParameterMapping$Builder.build(ParameterMapping.java:104) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.buildParameterMapping(SqlSourceBuilder.java:123) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.builder.SqlSourceBuilder$ParameterMappingTokenHandler.handleToken(SqlSourceBuilder.java:67) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.parsing.GenericTokenParser.parse(GenericTokenParser.java:78) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.builder.SqlSourceBuilder.parse(SqlSourceBuilder.java:45) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:44) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:292) ~[mybatis-3.4.6.jar:3.4.6] at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:83) ~[pagehelper-5.1.4.jar:na] at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) ~[mybatis-3.4.6.jar:3.4.6] at com.sun.proxy.$Proxy176.query(Unknown Source) ~[na:na] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) ~[mybatis-3.4.6.jar:3.4.6] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121] at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) ~[mybatis-spring-1.3.2.jar:1.3.2] at com.sun.proxy.$Proxy113.selectList(Unknown Source) ~[na:na] at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) ~[mybatis-spring-1.3.2.jar:1.3.2] at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76) ~[mybatis-3.4.6.jar:3.4.6] at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) ~[mybatis-3.4.6.jar:3.4.6] at com.sun.proxy.$Proxy122.selectByExample(Unknown Source) ~[na:na]

代码如下:=================================

for(){ ..... //拼接example,动态sql switch (type) { case EQURE: criteria.andEqualTo(columnName, proValue); break; case NOTEQURE: criteria.andNotEqualTo(columnName, proValue); break; case LIKE: if (proValue instanceof String) { criteria.andLike(columnName, "%" + proValue + "%"); } break; case RANGE:

                    Column ca = (Column) poClass.getDeclaredField(proName).getAnnotation(Column.class);
                    //获取表字段名
                    String cn = Optional.ofNullable(ca).map(va -> va.name()).orElse(proName);

                    if (proValue instanceof String) {
                        StringBuilder stringBuilder=new StringBuilder(cn);
                        if(((String) proValue).indexOf(",")!=-1) {
                            String[] s = ((String) proValue).split(",");
                            if (StringUtils.isEmpty(s[0]) && StringUtils.isNotEmpty(s[1])) {
                                stringBuilder.append("<=").append(s[1]);
                            } else if (StringUtils.isNotEmpty(s[0]) && s.length == 1) {
                                stringBuilder.append(">=").append(s[0]);
                            } else {
                                stringBuilder.append(" between ").append(s[0]).append(" and ").append(s[1]);
                            }
                        }
                        criteria.andCondition(stringBuilder.toString());
                    }
                    /*  此处与通用mapper的类型不匹配
                    if (proValue instanceof String) {
                        String[] s = ((String) proValue).split(",");
                        if (StringUtils.isEmpty(s[0]) && StringUtils.isNotEmpty(s[1])) {
                            criteria.andLessThanOrEqualTo(columnName, s[1]);
                            criteria.andCondition()
                        } else if (StringUtils.isNotEmpty(s[0]) && s.length == 1) {
                            criteria.andGreaterThanOrEqualTo(columnName, s[0]);
                        } else {
                            criteria.andBetween(columnName, s[0], s[1]);
                        }
                    }
                    */
                    break;
                case LIST:
                    if (proValue.getClass().isArray()) {
                        criteria.andIn(columnName, Arrays.asList(proValue));
                    } else if (proValue instanceof List) {
                        criteria.andIn(columnName, (List) proValue);
                    }
                    break;

... }

..... if (order.getOrderType().equals(FilterWrapper.ORDER_TYPE_ASC)) { if (Optional.ofNullable(orderBy).isPresent()) { orderBy = orderBy.orderBy(orderName).asc(); } else { orderBy = example.orderBy(orderName).asc(); }

                } else {
                    if (Optional.ofNullable(orderBy).isPresent()) {
                        orderBy = orderBy.orderBy(orderName).desc();
                    } else {
                        orderBy = example.orderBy(orderName).desc();
                    }
                }

..... return secondHouseMapper.selectByExample(example);

guan25 commented 6 years ago

追踪到mybatis里面:发现 TypeHandler 为空,对应的过滤条件是List类型(采用通用mapper generator生成的配置) --- 应该如何修改 image

abel533 commented 6 years ago

proValue 是什么值的时候出的问题?

guan25 commented 6 years ago

@abel533 是数组的时候出问题,如果是List就不会有问题,难道用Arrays.asList 引起的,但array并没有使用基本类型 谢谢

fallsea commented 4 years ago

有解决方案吗,同样遇到此问题

fallsea commented 4 years ago

我这边测试,只要第一次没有执行Example.Criteria 组装的条件,后面都可以查询成功,没有异常,第一次执行有条件,就可能会失败,不是必然的 @abel533

fallsea commented 4 years ago

已经解决了,参数只传example对象就没问题,传list,有时会有问题,有时正常

wysnxzm commented 3 years ago

WeekendSqls使用andIn()查询存在相同问题,更换为Example andIn()后正常

Error querying database. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_listItem_4'. It was either not specified and/or could not be found for the javaType / jdbcType combination specified.

Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_listItem_4'. It was either not specified and/or could not be found for the javaType / jdbcType combination specified.

wysnxzm commented 3 years ago

WeekendSqls使用andIn()查询存在相同问题,更换为Example andIn()后正常

Error querying database. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_listItem_4'. It was either not specified and/or could not be found for the javaType / jdbcType combination specified.

Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_listItem_4'. It was either not specified and/or could not be found for the javaType / jdbcType combination specified.

请教@abel533 后,我的问题已解决 是由于int[]被识别为类而不是数组导致的问题,改为Integer[]后解决