gnalog
gnalog

Reputation: 5

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:

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 :

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:

  <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 ?

Upvotes: 0

Views: 700

Answers (1)

liuchu
liuchu

Reputation: 60

Is parameter a List type? try <select id='car' parameterType="java.util.List">
Or Car type? try <select id='car' parameterType="your.class.package.Car">

Upvotes: 1

Related Questions