簡體   English   中英

使用Spring MVC + Hibernate的for循環插入記錄

[英]insert record using for loop with spring mvc + hibernate

[Spring MVC + Hibernate]

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


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



public void AddCOAMaintenance(COAMaintenanceModel obj) {

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


}

}

Controller中,我編寫循環以多次輸入記錄,但是以下操作不起作用,它僅插入一條記錄。

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

如何在上述場景中輸入多個記錄!

目前只有一個coaMaintenanceModel,您應該將其放在一個數組中,並按array.length循環。 順便說一句,您不需要用一個成員循環數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM