循环插入数据被覆盖,循环内怎么new对象

@Override
public int add(String tpid, String tsid) {
    OrgQuestions questions = new OrgQuestions();
    questions.setOqId(CommonClass.UUIDGenerated());
    String s = tsid;
    List<String> topicstpres = Arrays.asList(s.split(","));
    List<OrgTopicstore> topicstoreList = orgTopicstoreMapper.selectTsIdList(topicstpres);
    for (OrgTopicstore topicstore : topicstoreList) {

        questions.setOqAnswer(topicstore.getTsOpanswer());
        questions.setOqTsname(topicstore.getTsName());
        questions.setOqType(topicstore.getTsType());
        questions.setOqScore(topicstore.getTsScore());
    }
    int result = 0;
    int exist = orgQuestionsMapper.queryExist(questions.getOqId());
    if (exist > 0) {
        return -1;
    }
    LoginSessionModel loginSessionModel = LoginSessionInfo.getSessionModel(redisUtil);
    questions.setOqTpid(tpid);
    questions.setOqCreatetime(new Date());
    questions.setOqCreateuser(loginSessionModel.getLoginUserName());
    int rs = orgQuestionsMapper.insert(questions);
    if (rs > 0) {
        result = 1;
    }
    return result;
}

 

把new 对象和 insert都放在for循环内部

正确代码:

@Override
    public int add(String tpid, String tsid) {
        int result = 0;
        String s = tsid;
        List<String> topicstpres = Arrays.asList(s.split(","));
        List<OrgTopicstore> topicstoreList = orgTopicstoreMapper.selectTsIdList(topicstpres);
        for (OrgTopicstore topicstore : topicstoreList) {
            OrgQuestions questions = new OrgQuestions();
            questions.setOqAnswer(topicstore.getTsOpanswer());
            questions.setOqTsname(topicstore.getTsName());
            questions.setOqType(topicstore.getTsType());
            questions.setOqScore(topicstore.getTsScore());
            int exist = orgQuestionsMapper.queryExist(questions.getOqId());
            if (exist > 0) {
                return -1;
            }
            questions.setOqId(CommonClass.UUIDGenerated());
            LoginSessionModel loginSessionModel = LoginSessionInfo.getSessionModel(redisUtil);
            questions.setOqTpid(tpid);
            questions.setOqCreatetime(new Date());
            questions.setOqCreateuser(loginSessionModel.getLoginUserName());
            int rs = orgQuestionsMapper.insert(questions);
            if (rs > 0) {
                result = 1;
            }
        }
        return result;
    }