简体   繁体   English

使用Spring MVC + Hibernate的for循环插入记录

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

[Spring MVC + Hibernate] [Spring MVC + Hibernate]

@Controller @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);


}

}

In Controller I write loop to enter record more than one time, but the following is not working , it insert only one record. Controller中,我编写循环以多次输入记录,但是以下操作不起作用,它仅插入一条记录。

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

How to enter the multiple records in the above scenerio! 如何在上述场景中输入多个记录!

There is only one coaMaintenanceModel at this time, you should put it in an array, and loop by array.length. 目前只有一个coaMaintenanceModel,您应该将其放在一个数组中,并按array.length循环。 by the way, you don't need to loop an array with one member 顺便说一句,您不需要用一个成员循环数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM