我正在使用 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;
}
}