Spring-47 Adding an Update Method to the DAO
public boolean update(Offer offer){BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(offer); return jdbc.update("update offers set name=:name,text=:text,email=:email where id =:id",params) == 1;}
Offer offer = new Offer(2,"nfd","[email protected]","iwtkurf"); if(offersDao.update(offer)){ System.out.println("Object update"); }else{ System.out.println("Cannot update object"); }
Spring-48 Batch Updates:Prepared Statements
public int[] create(List<Offer> offers){ SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(offers.toArray()); return jdbc.batchUpdate("insert into offers(name,text,email) values (:name, :text, :email)", params); }
List<Offer> offers1=new ArrayList<Offer>(); offers1.add(new Offer("Dave","[email protected]","Cash of software")); offers1.add(new Offer("sergio","[email protected]","Cash of hardware"); int[] rvals = offersDao.create(offers1); for(int value: rvals){ System.out.println("Updated"+ value+" rows."); }
Spring-49 Transactions
Add bean "DataSourceTransactionManager"--->set ref "dataSource"
check "tx" in Namespace
In "tx" ----->insert "tx:annotationdriven element"
标记为@Transactional时,两个sql,任意一个失败,两个都不执行。
时间: 2024-10-01 03:00:13