首先dao类名字是WebContentDao,以下为配置的oracle序列
<bean id="increWC" class="org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer">
<property name="incrementerName" value="s_webcontent"/> <!-- ①指定序列名 -->
<property name="dataSource" ref="dataSource"/>
<!-- ②设置数据源 -->
</bean>
以下为WebContentDao类的代码
@Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
final Logger logger = LoggerFactory.getLogger(JdbcWebContentDao.class);
@Autowired
@Qualifier("increWC")
private DataFieldMaxValueIncrementer increWC;
@SuppressWarnings("unchecked")
public int save(final WebContent wc){
final StringReader sr = new StringReader(wc.getContent());
final int length = wc.getContent().getBytes().length;
jdbcTemplate.execute(
"insert into webcontent(id,content,title) values (?,?,?)",
new AbstractLobCreatingPreparedStatementCallback(new DefaultLobHandler()) { @Override
int id = increWC.nextIntValue();
wc.setId(id); //由于要返回id,使用参数实体来保存id
prep.setInt(1, id);
lobCreator.setClobAsCharacterStream(prep, 2, sr, length);
prep.setString(3, wc.getTitle());
//lobCreator.setBlobAsBinaryStream(ps, 3, blobIs, (int)blobIn.length());
}
});
sr.close();
return wc.getId();
}
由于Connection的ThreadLocal实现,两个属性JdbcTemplate和DataFieldMaxValueIncrementer都是取到同线程的Connection对象,所以不存在并发问题!