[英]how to insert query in mybatis with parameter list?
任何人都可以轉換我在https://localcoder.org/mybatis-insert-list-values解決方案 3 中找到的示例。沒有解釋發生了什么,我很難像示例一樣更改它,而且我是 mybatis 的新手。 如果@Insert 有一個參數列表,這就是解決方案。
// 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.