标签在Mybatis的xml配置中使用集合,主要是用到了foreach动态语句。 foreach的参数: foreach元素的属性主要有 item,index,collection,open,separator,close。 item表示集合中每个元素进行迭代时的别名. index指 定一个名字,用于表示在迭代过程当中,每次迭代到的位置(不知道具体的用处). open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号做为分隔 符. close表示以什么结束git
在使用mybatis中使用 foreach 出现以下问题github
org.apache.ibatis.reflection.ReflectionException:There is no getter for property named '__frch_item_0' in 'TestEntity'
foreach 代码以下:apache
<foreach collection="companys" index="index" item="item" open=" (" close=") " separator=","> #{item} </foreach>
com.github.miemiedev.mybatis.paginator.domain.PageBounds; 查询一:this.getSqlSession().selectList(toMybatisStatement("selectByConditions"), params, bounds); 查询二:this.getSqlSession().selectList(toMybatisStatement("selectByConditions"), params);
使用第一种带第三方分页插件查询 报错:数组
org.apache.ibatis.reflection.ReflectionException:There is no getter for property named '__frch_item_0' in 'TestEntity'
使用第二种方式查询 可以正常使用。安全
若是想要使用第一种查询方式时 可使用一下两种解决方法:mybatis
解决方案1:parameterType 参数类型将 TestEntity 修改为 map 解决方案2:将 #{item} 修改为 ${item} 若是是字符串则修改为 '${item}'
默认状况下,使用#{}语法,MyBatis会产生PreparedStatement语句中,而且安全的设置PreparedStatement参数,这个过程当中MyBatis会进行必要的安全检查和转义。 示例1:dom
执行SQL:Select * from company where id = #{id} 参数:id =>10 解析后执行的SQL:Select * from emp where id= ? 执行SQL:Select * from company where id= ${id } 参数:id 传入值为:10 解析后执行的SQL:Select * from company where id=10 使用foreach循环的时候 companys 这边传入的是string 数组 使用foreach循环时 默认会 将item 转成 __frch_item_0 ... 1.咱们使用#{} 获取参数 则变成 __frch_item_0=>10... Select * from emp where id in(?,?,?) 2.咱们使用${} 获取参数 则变成 10 传入数据中 .. Select * from emp where id in(10,20,30) 则能够直接使用
因此在使用分页查询的时候使用第二种解决方案是可行的。那是什么缘由致使了使用#{}这种获取参数数据的方式会错,而当使用map做为参数时#{}又是可用的。 分析数据:在使用#{}获取参数时,mybatis会将获取到的数据存入一个map中, 例如:性能
{id="10",param1 = "test",_fors_item_0=591,_fors_item_1=592 }
其中 低于param1为正常获取 数据,forsitem0,forsitem1为循环时获取的数据, 同时在map还会有其余的参数,其中包括 传入类型 parameterType所带入的对象(TestEntity)。this
使用PageBounds 的时候会将mybaits 的数据嵌入在PageBounds里面, 我的分析在PageBounds 回填参数的时候作了类型的判断, 判断传入的parameterType是不是map类型,若是是则从map中获取数据,若是不是则将map映射成TestEntity实体,然而在实体中不存在forsitem_0属性,因此就提示错误:插件
`org.apache.ibatis.reflection.ReflectionException:There is no getter for property named '__frch_item_0' in 'TestEntity'`
使用${}方式会引起SQL注入的问题、同时也会影响SQL语句的预编译,因此从安全性和性能的角度出发,能使用#{}的状况下就不要使用${},