0

我正在使用 Spring-boot 和 Oracle,我想使用NamedParameterJdbcTemplate insertbatch方法将新记录插入到我的数据库表中。

但我object(ProductMarket)object(Market)我需要的价值(getMarket().getId())

我怎么知道SqlParameterSource从内部获取市场ID Object

package x.dao.productMarket;

import java.util.List;

import x.model.base.ProductMarket;

public interface IProductMarketDao {

    public static final String INSERT_PRODUCT_MARKETS =
        " INSERT " +
        " INTO PRODUCT_MARKETS" +
        " (" +
        "    product_id ," +
        "    market_id" +
        " )" +
        " VALUES" +
        " (" +
        "    :productId ," +
        "    :marketId " +
        " ) ";


    void insert(List < ProductMarket > productMarkets);

}



package x.dao.productMarket;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import x.model.base.ProductMarket;

@
Repository
public class ProductMarketDao implements IProductMarketDao {

    @
    Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @
    Override
    public void insert(List < ProductMarket > productMarkets) {
        SqlParameterSource[] batch =
            SqlParameterSourceUtils.createBatch(productMarkets.toArray());
        namedParameterJdbcTemplate.batchUpdate(INSERT_PRODUCT_MARKETS,
            batch);
    }
}

package x.model.base;

public class ProductMarket implements Serializable {

    private static final long serialVersionUID = 421870753189756693 L;

    private long productId;
    private Market market;

    /**
     * @return the productId
     */
    public long getProductId() {
            return productId;
        }
        /**
         * @param productId the productId to set
         */
    public void setProductId(long productId) {
        this.productId = productId;
    }


    public Market getMarket() {
        return market;
    }
    public void setMarket(Market market) {
        this.market = market;
    }

}
4

1 回答 1

0

假设市场的定义如下:

class Market {
    private final int id;
    private final String name;

    public Market(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

然后您可以按如下方式更改您的 SQL 模板:

static final String INSERT_PRODUCT_MARKETS =
        " INSERT " +
                " INTO product_markets" +
                " (" +
                "    product_id ," +
                "    market_id" +
                " )" +
                " VALUES" +
                " (" +
                "    :productId ," +
                "    :market.id " +
                " ) ";

请注意我如何使用路径来表示表单:market.id中的命名参数,其中market字段的名称是 inProductMarket并且id是字段的名称 in Market

一旦以这种方式定义,NamedParameterJdbcTemplate将能够取消引用相应的值并将其用作查询的参数。

于 2017-08-16T07:38:08.113 回答