zoukankan      html  css  js  c++  java
  • 我的第一个Hibernate程序

    今天开始学习Hibernate,开启我的第一个Hibernate程序 

    1.方法一:myeclipse自动生成

    首先,建立一个数据库student

    第二步,在student数据库中建立一张表student

    第三步,打开 myeclipse10,打开MyEclipse Hibernate preperctive

    第四步,在DB Browser中新建一个新的DB连接

    第五步,新建一个新的mysql的连接

     切换至Myeclipse java prespective

    第六步,新建一个Hibernate_001的项目,并加入myeclipse对于Hibernate的支持

     

    第七步,再次切换到Hibernate prespective中,使用Hibernate reverse engineering

     第八步,

    切换至java prespective,新建一个test.java,在test.java中输入如下的代码:

    //此代码功能是向数据库中插入一个新建的stu的对象

    public static void main(Strring[]args)

    {

    Student stu=new Student();

    stu.setSno(6);

    stu.setSname("f");

    stu.setSgrade(12);

    Configuration cfg=new Configuration().configure();

    SessionFactory sc=cfg.buildSessionFactory();


    Session se=sc.openSession();

    Transaction tx=se.beginTransaction();

     

    se.save(stu);

    tx.commit();

    System.out.println("success!");

    }

    第九步,执行java代码结束后,检查数据库,在数据库中查询select * from student;

     

    Hibernate操作成功。

    2.方法二:手工配置

    同样使用Student数据库

    第一步,建立一个Hibernate_002程序

    2.导入jar包

    3.第三步,在src下新建一个hibernate.cfg.xml,并在此文件中输入以下内容,配置数据库连接的基本信息

    第四步 在src下新建一个Student.hbm.xml文件,并在Student.hbm.xml中配置student表的基本属性

     第五步,创建一个Student.java类,Student类放在com.zk.Student中

     最后一步,新建一个test文件,并输入下列代码,向数据库中插入新的数据

    数据插入成功

  • 相关阅读:
    洛谷 P2700 逐个击破
    洛谷 P1503 鬼子进村
    洛谷 P1556 幸福的路
    洛谷 P1490 买蛋糕
    洛谷 P2507 [SCOI2008]配对
    code vs 3305 水果姐逛水果街Ⅱ
    通过idea远程调试
    【Cocos2d-x JavaScript Binding】
    ☀【SeaJS】SeaJS Grunt构建
    -_-#【Better Code】throttle / debounce
  • 原文地址:https://www.cnblogs.com/longlyseul/p/9822811.html
Copyright © 2011-2022 走看看