【发布时间】:2018-02-18 16:16:26
【问题描述】:
在我的 Spring Batch 应用程序中,我正在读取、处理然后尝试使用 stored procedure 将 ItemWriter 写入数据库:
下面是我的 CSV 文件的样子,让我们说出我想要读取、处理和写入的内容:
Cob Date;Customer Code;Identifer1;Identifier2;Price
20180123;ABC LTD;BFSTACK;1231.CZ;102.00
我的ItemWriter:
@Slf4j
public class MyDBWriter implements ItemWriter<Entity> {
private final EntityDAO scpDao;
public MyWriter(EntityDAO scpDao) {
this.scpDao = scpDao;
}
@Override
public void write(List<? extends Entity> items) {
items.forEach(scpDao::insertData);
}
}
我的 DAO 实现:
@Repository
public class EntityDAOImpl implements EntityDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
private SimpleJdbcCall simpleJdbcCall = null;
@PostConstruct
private void prepareStoredProcedure() {
simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName("loadPrice");
//declare params
}
@Override
public void insertData(Entity scp) {
Map<String, Object> inParams = new HashMap<>();
inParams.put("Identifier1", scp.getIdentifier1());
inParams.put("Identifier2", scp.getIdentifier1());
inParams.put("ClosingPrice", scp.getClosingPrice());
inParams.put("DownloadDate", scp.getDownloadDate());
simpleJdbcCall.execute(inParams);
}
}
我用来更新的存储过程如下:
ALTER PROCEDURE [dbo].[loadPrice]
@Identifier1 VARCHAR(50),
@Identifier1 VARCHAR(50),
@ClosingPrice decimal(28,4),
@DownloadDate datetime
AS
SET NOCOUNT ON;
UPDATE p
SET ClosingPrice = @ClosingPrice,
from Prices p
join Instrument s on s.SecurityID = p.SecurityID
WHERE convert(date, @DownloadDate) = convert(date, DownloadDate)
and s.Identifier1 = @Identifier1
if @@ROWCOUNT = 0
INSERT INTO dbo.Prices
(
sec.SecurityID
, ClosingPrice
, DownloadDate
)
select sec.SecurityID
, @ClosingPrice
, LEFT(CONVERT(VARCHAR, @DownloadDate, 112), 8)
from dbo.Instrument sec
WHERE sec.Identifier1 = @Identifier1
如果我有这个设置,我的要求之一是如果我无法使用@Identifier1 更新/插入数据库,即没有与Identifier1 匹配的SecurityID,我需要然后更新/插入
使用Identifier2。如果您愿意,可以进行二级比赛。
如何在我的 DAO insertData() 中执行此操作?它是业务逻辑,更喜欢使用 java 代码而不是存储过程,但我很想看看你的例子是如何实现的。
如何返回更新/插入行的结果并决定是否使用第二个标识符更新/插入?
【问题讨论】:
标签: java sql spring spring-batch microservices