mybatis inserts list data into the database

Prerequisite

In the business needs of the last few days, I encountered inserting list data into the database, or querying the data in the list. For queries, my first idea was to write a simple SQL statement and execute SQL in a loop Statement, but this will consume a lot of money, and cannot be used in this way, so you can only write the loop in the sql statement. Naturally, I thought of using dynamic sql and foreach in mybatis.

Specific code explanation

InventoryDetail

@Table(name = "t_inventory_detail")
public class InventoryDetail {
    
    
    /**
     * 主键ID
     */
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    /**
     * 盘点头表ID
     */
    @Column(name = "inventory_header_id")
    private Integer inventoryHeaderId;

    /**
     * 盘点履历表ID
     */
    @Column(name = "inventory_resume_id")
    private Integer inventoryResumeId;

    /**
     * 商品ID
     */
    @Column(name = "product_id")
    private Integer productId;

    /**
     * 在库数量
     */
    @Column(name = "stock_quantity")
    private Integer stockQuantity;

    /**
     * 在库单价
     */
    @Column(name = "stock_price")
    private Long stockPrice;

    /**
     * 创建时间
     */
    @Column(name = "create_date")
    private Date createDate;

    /**
     * 创建人ID
     */
    @Column(name = "create_user_id")
    private Integer createUserId;

    /**
     * getter 和 setter 方法
     */
}

mapper

public interface InventoryDetailMapper {
    
    
    /**
     * 插人数据到盘点详情表
     *
     */
    @Insert("<script> " +
            "insert into t_inventory_detail (inventory_header_id, product_id, stock_quantity, stock_price, create_date, create_user_id)" +
            " values " +
            " <foreach collection='list' item='inventoryDetail' separator=','>" +
            " (#{inventoryDetail.inventoryHeaderId}," +
            "  #{inventoryDetail.productId}," +
            "  #{inventoryDetail.stockQuantity}," +
            "  #{inventoryDetail.stockPrice}," +
            "  #{inventoryDetail.createDate}," +
            "  #{inventoryDetail.createUserId})" +
            "</foreach> " +
            "</script>")
    Integer insertInventoryDetail(@Param("list") List<InventoryDetail>  list);
}

This is sql written in comments, you can also change it to write in xml files. The sentences are similar. as follows:

<insert id="insertInventoryDetail" parameterType="java.util.List" useGeneratedKeys="false">
    			insert into t_inventory_detail
    			( inventory_header_id, product_id, stock_quantity, stock_price, create_date, create_user_id)
    			values
    			<foreach collection="list" item="inventoryDetail" separator=",">
    				(
    					#{
    
    inventoryDetail.inventoryHeaderId},
    					#{
    
    inventoryDetail.productId},
    					#{
    
    inventoryDetail.stockQuantity},
    					#{
    
    inventoryDetail.stockPrice},
    					#{
    
    inventoryDetail.createDate},
    					#{
    
    inventoryDetail.createUserId}}
    				)
    		     </foreach>		
    </insert>    
</mapper>

Note: The conditions in foreach correspond to your sql statement.

summary

Query and insert are almost the same. Just watch the sentence and rewrite it.

Guess you like

Origin blog.csdn.net/hello_cmy/article/details/108074097