gpt4 book ai didi

java - 使用 for 循环和 spring mvc + hibernate 插入记录

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:02 25 4
gpt4 key购买 nike

[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

@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进行循环。顺便说一句,您不需要用一个成员循环数组

关于java - 使用 for 循环和 spring mvc + hibernate 插入记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15907898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com