简述下问题:
1、前台页面有个多选框,后台定义了JavaBean,多选框用String数组来接,生成get和set,然后在action中可以获取到参数,但如何通过dao插入到数据库? 我用的是hibernate。for (int i = 0; i < xxxl.length; i++) 
{
    stu.setXxxl(xxxl);
}
这样插入进去是一个数组,在数据库中也是。。如何获取到数组中的值然后插入? 主要JavaBean中定义的是数组,这里set的也必须是数组,如何把值取出来然后插入? 冰天雪地裸体跪求。。

解决方案 »

  1.   

    action中的相关代码;String[] xxxl = (String[])request.getParameterValues("xxxl"); dao中的相关代码for (int i = 0; i < xxxl.length; i++) 

        stu.setXxxl(xxxl); 
    } session.save(stu);
    ...
    ...
      

  2.   


    //添加功能
    public ActionForward insertPayoff(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
    PayOffForm payoffForm=(PayOffForm)form;
    try {
    String payoffIds=(String)request.getSession().getAttribute("payoffIds");
    System.out.println(payoffIds);
    String payoffRegisterTime=request.getParameter("payoff.payoffRegisterTime"); 
    String payoffRegistrar=request.getParameter("payoff.payoffRegistrar");

    String [] fileInfoId=request.getParameterValues("fileInfoId");
    String [] payId=request.getParameterValues("payId");

    String [] payoffBounty=request.getParameterValues("payoffBounty");
    String [] payoffSaleroom=request.getParameterValues("payoffSaleroom");
    String [] payoffDeduct=request.getParameterValues("payoffDeduct");
    String [] payCount=request.getParameterValues("payCount");
    HttpSession session=request.getSession();

    for(int i=0;i<fileInfoId.length;i++){
    Payoff payoff=new Payoff();
    FileInfo file=new FileInfo();
    Pay pay=new Pay();


    file.setFileInfoId(Integer.parseInt(fileInfoId[i]));
    payoff.setFileInfo(file);
    pay.setPayId(Integer.parseInt(payId[i]));
    payoff.setPay(pay);
    payoff.setPayoffState(0);
    payoff.setPayoffRegistrar(payoffRegistrar);
    payoff.setPayoffRegisterTime(payoffRegisterTime);
    payoff.setPayoffIds(payoffIds);
    System.out.println(payoffBounty[i]);
    payoff.setPayoffBounty(Double.parseDouble(payoffBounty[i]));
    payoff.setPayoffSaleroom(Double.parseDouble(payoffSaleroom[i]));
    payoff.setPayoffDeduct(Double.parseDouble(payoffDeduct[i]));


    Double a=Double.parseDouble(payoffBounty[i]);
    Double b=Double.parseDouble(payoffSaleroom[i]);
    Double c=Double.parseDouble(payoffDeduct[i]);
    Double d=Double.parseDouble(payCount[i]);

    Double sum=a+b-c+d;
    System.out.println(sum);
    payoff.setPayoffCount(sum);

    payoffService.insertPayoff(payoff);

    }
    return mapping.findForward("insert");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return mapping.findForward("error");
    }
    }
    给你个批量添加的例子  自己看
      

  3.   

    顶下,楼上的朋友不好意思,我试了试不行。。stu.setXxxl(xxxl)是一个数组.不会报错。 但把循环出来的值放进去就不行了,例如stu.setXxxl(xxxl[i])... 
      

  4.   

    你直接在循环调用session.save(stu); 这样应该就可以插入多条数据了
      

  5.   

    把 session.save(stu); 放到for语句吧
      

  6.   


    for (int i = 0; i < xxxl.length; i++) {
    Student stu = new Student();
    stu.setUsername(username);
    stu.setGender(gender);
    stu.setXxkc(xxkc);
    stu.setAge(age);
    stu.setMinzu(minzu);
    stu.setWhcd(whcd);
    stu.setFather(father);
    stu.setMother(mother);
    stu.setHuji(huji);
    stu.setTel(tel);
    stu.setAddress(address);
    stu.setFamily(family);
    stu.setCode(code);
    stu.setIdno(idno);
    stu.setXxxl(xxxl);
    stu.setBz(bz);
    session.save(stu);
    }在循环里面插入也不行。。出来还是一个对象。。
    [Ljava.lang.String;@1c3432a 
      

  7.   

    你去找一找hibernate批量插入数据!
      

  8.   

    我只要求插入一条数据就可以了。。数组插入后一直为一个对象。是不是需要把JavaBean里面的 string [] xxxl改为 String xxxl ? 这样应该就不能获取多个值了。。数组通过循环也无法赋值赋值后set的是一个数组。。 郁闷