如何在mybatis中插入查询参数列表?



任何人都可以转换在https://localcoder.org/mybatis-insert-list-values解决方案3中找到的样本吗?没有解释发生了什么,我很难像样本一样改变它,我是新来的。这是@Insert有参数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);
}

这是示例解决方案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.

我只是改变tb_client,这会做吗?

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

最新更新