On the way learning spring 6

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","nfd@gmail.com","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","dave@caveofprogramming","Cash of software"));
offers1.add(new Offer("sergio","sergio@caveofprogramming","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,任意一个失败,两个都不执行。

 

posted on 2015-10-30 00:37  jobfinder  阅读(137)  评论(0编辑  收藏  举报