使用hibernate 调用存储过程。



  1. public




    class


    StuInfo {



  2. private




    int


    id;



  3. private


    String stuName;



  4. private


    String stuNo;



  5. private




    int


    stuAge;



  6. private


    String stuId;



  7. private


    String stuSeat;



  8. private


    String stuAddress;


  9. }

对应的数据库表:



  1. if exists(


    select


    *


    from


    sysobjects


    where




    name


    =


    ‘stuInfo’


    )



  2. drop




    table


    stuInfo



  3. create




    table


    stuInfo /*创建学员信息表**/


  4. (

  5. stuName

    varchar


    (20)


    not




    null


    ,


    — 姓名,非空




  6. stuNo

    char


    (6)


    not




    null


    ,


    — 学号,非空




  7. stuAge

    int




    not




    null


    ,


    — 年齡,int 默认为4个长度




  8. stuId

    numeric


    (18,0),


  9. stuSeat

    smallint


    ,


    — 坐位车,使用自增




  10. stuAddress text

    — 住址 可以为空




  11. )


  12. — 给stuInfo添加一列





  13. alter




    table


    stuInfo


    add


    id


    int


    identity(1,1)


    primary




    key


    ;




创建存储过程:



  1. — 存储过程




  2. if exists(

    select




    name




    from


    sysobjects


    where




    name


    =


    ‘proc_stuInfo’




    and


    type=


    ‘p’


    )



  3. drop


    proc proc_stuInfo


  4. go


  5. create


    proc proc_stuInfo



  6. as





  7. select


    *


    from


    stuInfo


  8. go


  9. — 调用存储过程





  10. exec


    proc_stuInfo;

在hibernate 中调用存储过程的几种方法。

第一种:命名查询



  1. <


    sql-query




    name


    =


    “getStuInfo”




    callable


    =


    “true”


    >





  2. <


    return




    alias


    =


    “stuInfo”




    class


    =


    “com.bosssoft.domain.StuInfo”


    >





  3. <


    return-property




    name


    =


    “id”




    column


    =


    “id”




    />





  4. <


    return-property




    name


    =


    “stuName”




    column


    =


    “stuName”




    />





  5. <


    return-property




    name


    =


    “stuAge”




    column


    =


    “stuAge”




    />





  6. <


    return-property




    name


    =


    “stuNo”




    column


    =


    “stuNo”


    />





  7. <


    return-property




    name


    =


    “stuSeat”




    column


    =


    “stuSeat”




    />





  8. <


    return-property




    name


    =


    “stuAddress”




    column


    =


    “stuAddress”


    />





  9. <


    return-property




    name


    =


    “stuId”




    column


    =


    “stuId”


    />





  10. </


    return


    >




  11. {call proc_stuInfo()}


  12. </


    sql-query


    >






  1. List li=session.getNamedQuery(


    “getStuInfo”


    ).list();


  2. System.out.println(li.get(

    0


    ));




第二种:类似于jdbc



  1. System.out.println(


    “jdbc 调用————-”


    );


  2. Connection conn = session.connection();

  3. ResultSet rs =

    null


    ;


  4. CallableStatement call;


  5. try


    {


  6. call = conn.prepareCall(

    “{Call proc_stuInfo()}”


    );


  7. rs = call.executeQuery();


  8. while


    (rs.next()){


  9. System.out.println(rs.getString(

    1


    ));


  10. System.out.println(rs.getString(

    2


    ));


  11. System.out.println(rs.getString(

    3


    ));


  12. System.out.println(rs.getString(

    4


    ));


  13. System.out.println(rs.getString(

    5


    ));


  14. System.out.println(rs.getString(

    6


    ));


  15. System.out.println(rs.getString(

    7


    ));


  16. System.out.println(

    “——————”


    );


  17. }

  18. }

    catch


    (SQLException e) {


  19. e.printStackTrace();

  20. }






第三种:最简单的一种



  1. SQLQuery query =  session.createSQLQuery(


    “{call proc_stuInfo()}”


    ).addEntity(StuInfo.


    class


    );


  2. List list =query.list();

  3. System.out.println(list.get(

    0


    ));




注:在第三种调用时,一定要加上addEntity();否则没有数据返回。

hibenate 调用带参的存储程



  1. — 带参数据的存储过程




  2. if exists(

    select




    name




    from


    sysobjects


    where




    name


    =


    ‘proc_find_stu’




    and


    type=


    ‘p’


    )



  3. drop


    proc  proc_find_stu


  4. go


  5. create


    proc  proc_find_stu(@startId


    int


    ,@endId


    int


    )



  6. as





  7. select


    *


    from


    stuInfo


    where


    id


    between


    @startId


    and


    @endId;


  8. go


  9. exec


    proc_find_stu 1,4;




  1. /**hibernate 调用带参的存储过程*/





  2. @SuppressWarnings


    (


    “unchecked”


    )



  3. @Test





  4. public




    void


    msTest2(){


  5. SessionFactory sf = SessionFactoyUtil.getSessionFactory();

  6. Session session = sf.openSession();

  7. SQLQuery query = session.createSQLQuery(

    “{CALL proc_find_stu(?,?)}”


    ).addEntity(StuInfo.


    class


    );


  8. query.setLong(

    0


    ,


    2


    );


  9. query.setLong(

    1


    ,


    4


    );


  10. List<StuInfo> list =query.list();


  11. for


    (


    int


    i =


    0


    ;i<list.size();i++){


  12. System.out.println(list.get(i));

  13. }

  14. }



版权声明:本文为ly199108171231原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ly199108171231/article/details/54092376