简体   繁体   English

如何使用mybatis的foreach标签映射实体类型的参数?

[英]How can I map a parameter of entity type with foreach tag of mybatis?

There is an attribute of list type named 'carType' in an entity, the entity is like below: 实体中有一个名为“ carType”的列表类型的属性,该实体如下所示:

public class Car{
     private List carType;
     private String carName;

     public void setCarType(List carType){
         this.carType = carType;
     }

     public List getCarType(){
         return carType;
     }

}

I assigned it a value before calling the DAO interface , just like : 我在调用DAO接口之前为其分配了一个值,就像:

Car car = new Car();
car.setCarType = [1,2,3];
List list = Dao.car(Car car);

Then I tried to map the parameter with foreach tag of mybatis: 然后,我尝试使用mybatis的foreach标签映射参数:

  <select id='car'>
         select car_name, car_type from tb_car
         where car_type in
      <foreach item="item" collection="carType" separator="," open="(" close=")" index="">
         #{item}
      </foreach>
  </select>

Moreover, I hope the result is as follows: 此外,我希望结果如下:

select car_name, car_type from tb_car
where car_type in (1, 2, 3)

But the following error occurred after debugging: 但是调试后发生以下错误:

org.mybatis.spring.MyBatisSystemException: nested exception is 
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: 
invalid comparison: java.util.ArrayList and java.lang.String
### Cause: java.lang.IllegalArgumentException: invalid comparison: 
java.util.ArrayList and java.lang.String

I knew that I can fix it with parameter of Hashmap type, but could you tell me how to correct my code in this way ? 我知道我可以使用Hashmap类型的参数修复它,但是您能告诉我如何以这种方式纠正代码吗?

Is parameter a List type? 参数是列表类型吗? try <select id='car' parameterType="java.util.List"> 尝试<select id='car' parameterType="java.util.List">
Or Car type? 还是Car类型? try <select id='car' parameterType="your.class.package.Car"> 尝试<select id='car' parameterType="your.class.package.Car">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM