2013-04-09 71 views
1

[春季MVC +休眠]插入记录冬眠

@Controller

@Controller 
public class COAMaintenanceController { 

protected static Logger log = Logger 
     .getLogger(COAMaintenanceController.class); 

@Resource(name = "COAMaintenanceService") 
private COAMaintenanceService coaMaintenanceService; 

@RequestMapping(value = "/addCoaMaintenance", method = RequestMethod.POST) 
public @ResponseBody 
JsonResponse addCoaCategory(@RequestParam("mainAccount") long mainAccount, 
     @RequestParam("subAccount") long subAccount, 
     @RequestParam("accountName") String accountName, 
     @RequestParam("coaCategoryId") long coaCategoryId, 
     @RequestParam("postingType") int postingType, 
     @RequestParam("typicalBalance") int typicalBalance, 
     @RequestParam("isActive") int isActive) { 

    Date sysdate = null; 
    JsonResponse response = null; 

    try { 

     sysdate = new Date(); 
     response = new JsonResponse(); 

     COAMaintenanceModel coaMaintenanceModel = new COAMaintenanceModel(
       mainAccount, subAccount, accountName, coaCategoryId, 
       postingType, typicalBalance, isActive, 
       GetSessionValue.getSysUserId(), 
       GetSessionValue.getSysUserIp(), sysdate, 0); 

     coaMaintenanceService.AddCOAMaintenance(coaMaintenanceModel); 

     response.setStatus("Success"); 


    } catch (Exception ex) { 
     log.error("Exception.." + ex); 
     response.setStatus("Fail"); 

    } 

    return response; 

} 

} 

@Service

@Service("COAMaintenanceService") 
@Transactional 
public class COAMaintenanceService { 


@Resource(name="sessionFactory") 
private SessionFactory sessionFactory; 



public void AddCOAMaintenance(COAMaintenanceModel obj) { 

Session session = sessionFactory.getCurrentSession(); 
session.save(obj); 


} 

} 

控制器我写lo可以多次输入记录,但以下不起作用,它只插入一条记录。

for(int i=0; i<50; i++){ 
    coaMaintenanceService.AddCOAMaintenance(coaMaintenanceModel); 
} 

如何在上面的场景中输入多条记录!

回答

1

现在只有一个coaMaintenanceModel,你应该把它放在一个数组中,然后循环array.length。顺便说一句,你不需要循环与一个成员的阵列

+0

好的。但我的情况是不同的。你能寄给我你的电子邮件ID吗?我会给你举个例子,我停下来!我的电子邮件= [email protected] – 2013-04-10 10:22:24

+0

当然,[email protected] – 2013-04-11 03:11:02