简体   繁体   中英

how to insert query in mybatis with parameter list?

Can any one convert the Sample i found in https://localcoder.org/mybatis-insert-list-values solution 3. There is no explanation what happen and im having hard time to change it like the sample and Im new to mybatis. This is the solution if @Insert has a parameter List.

// The actual Query i made

@Insert("INSERT INTO tb_client(client_guest_id, client_guest_name, client_guest_email,purchase_item_cart) " +
              " VALUES (#{client_guest_id}, #{client_guest_name}, #{client_guest_email},#{purchase_item_cart} )")

  @Options(useGeneratedKeys = true, keyColumn = "client_guest_id", keyProperty = "client_guest_id")
    public int insert(Client clientGuestRequest);
    }

This is the sample Solution 3 https://localcoder.org/mybatis-insert-list-values

//Sample

//I want to like this because i have list

@Insert({
        "<script>",
        "INSERT INTO your_database_name.your_table_name",
            "(column1_int, column2_str, column3_date, column4_time)",
        "VALUES" +  
            "<foreach item='each_item_name' collection='theCollection' open='' separator=',' close=''>" +
                "(" +
                    "#{each_item_name.column1,jdbcType=INTEGER},",
                    "#{each_item_name.column2,jdbcType=VARCHAR},",
                    "(SELECT SOME_DB_FUNCTION(#{each_item_name.column3,jdbcType=DATE})),",
                    "#{each_item_name.period.start,jdbcType=TIME}" +
                ")" +
            "</foreach>",
    "</script>"})
    void insertBatchSomething(@Param("theCollection") List<Something> theCollection);
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductEntity {
    
    private Integer purchase_item;
    private String productname;
    private String productbrand;
    private Double productprice;
    private String productdescription;
    private Integer productquantity;
    private Date  productexpirationdate;
    
    
   }

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Client {
    
    private Integer client_guest_id;

    private String client_guest_name;
    private String client_guest_email;
    private List<Integer> purchase_item_cart;
    
    
    }
//this is error

org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation.  Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'purchase_item_cart'. It was either not specified and/or could not be found for the javaType (java.util.List) : jdbcType (null) combination.

I just change the tb_client will this do?

CREATE TABLE `tb_client` (
  `client_guest_id` int NOT NULL AUTO_INCREMENT,
  `client_guest_email` varchar(255) DEFAULT NULL,
  `client_guest_name` varchar(255) DEFAULT NULL,
  `purchase_item_cart` int DEFAULT NULL,
  PRIMARY KEY (`client_guest_id`),
  KEY `purchase_idx` (`purchase_item_cart`),
  CONSTRAINT `purchase` FOREIGN KEY (`purchase_item_cart`) REFERENCES `tb_product` (`purchase_item`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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