1

[Spring MVC + 休眠]

@控制器

@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("COAMaintenanceService")
@Transactional
public class COAMaintenanceService {


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



public void AddCOAMaintenance(COAMaintenanceModel obj) {

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


}

}

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

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

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

4

1 回答 1

1

此时只有一个coaMaintenanceModel,你应该把它放在一个数组中,并按array.length循环。顺便说一句,您不需要用一个成员循环数组

于 2013-04-10T02:24:40.920 回答