当前位置: 首页 > 知识库问答 >
问题:

我如何将实体类型的参数与mybatis的foreach标记映射?

柯星华
2023-03-14

在一个实体中有一个名为'car type'的列表类型属性,该实体如下所示:

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

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

     public List getCarType(){
         return carType;
     }

}

我在调用DAO接口之前给它赋了一个值,就像:

Car car = new Car();
car.setCarType = [1,2,3];
List list = Dao.car(Car car);
  <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>
select car_name, car_type from tb_car
where car_type in (1, 2, 3)
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

共有1个答案

孙承
2023-03-14

参数是列表类型吗?尝试