Hibernate 第一个hibernate程序

Hibernate第一个程序

1、  下载资源:www.hibernate.org

2、  资源介绍hibernate-release-4.3.10.Final

a)        Documentation  相关文档

b)        Lib 相关jar包

c)        Project相关资源文件,模板文件,源码等

3、  搭建hibernate环境:

a)        新建一个java项目

b)        导入相关jar包

antlr-2.7.7.jar

dom4j-1.6.1.jar

hibernate-commons-annotations-4.0.5.Final.jar

hibernate-core-4.3.10.Final.jar

hibernate-jpa-2.1-api-1.0.0.Final.jar

jandex-1.1.0.Final.jar

javassist-3.18.1-GA.jar

jboss-logging-3.1.3.GA.jar

jboss-logging-annotations-1.2.0.Beta1.jar

jboss-transaction-api_1.2_spec-1.0.0.Final.jar

mysql-connector-java-5.1.20-bin.jar

c)        编写配置文件hibernate.cfg.xml文件

hibernate-release-4.3.10.Final\project\etc\hibernate.cfg.xml

放入到项目中src下:

<hibernate-configuration>

    <session-factory>

       <!-- 配置数据库连接信息 -->

       <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

       <property name="connection.url">jdbc:mysql://localhost:3306/hibernate4</property>

       <property name="connection.username">root</property>

       <property name="connection.password">root</property>

       <!-- 数据库方言 -->

       <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

    </session-factory>

</hibernate-configuration>

d)        创建数据库表,以及对应的pojo对象

Pojo对象

publicclass User {

   privateintid;

   private String name;

   private String pwd;

   }

User表:user

id

name

pwd

 

 

 

e)        编辑*.hbm.xml文件

文件名一般为pojo类的名称User.hbm.xml

放在pojo类所在的包下

头文件可以在project下查找,也可拷贝。

<hibernate-mapping>

   <class name="cn.siggy.pojo.User" table="user">

      <id name="id">

         <!-- 主键生成策略 -->

         <generator class="native"></generator>

      </id>

      <!-- 实体类的属性 -->

      <property name="name"/> 

      <property name="pwd"/>  

   </class>

</hibernate-mapping>

 

f)         测试:将*.hbm.xml配置文件加入到hibernate.cfg.xml中

publicstaticvoid main(String[] args) {

       //1.新建Configuration对象

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

       //2.通过Configuration创建SessionFactory对象

           //hibernate3.x中是这种写法

           //SessionFactory sf = cfg.buildSessionFactory();

       //hibernate4.3之前~hibernate4.0

//     ServiceRegistry sr = new ServiceRegistryBuilder()

//                       .applySettings(cfg.getProperties())

//                       .buildServiceRegistry();

       //hibernate4.3

       ServiceRegistry registry = new StandardServiceRegistryBuilder()

                         .applySettings(cfg.getProperties())

                         .build();

       SessionFactory sf = cfg.buildSessionFactory(registry);

       //3.通过SessionFactory得到Session

       Session session = sf.openSession();

       //4.通过session对象得到Transaction对象

       //开启事务

       Transaction tx = session.beginTransaction();

       //5.保存数据

       User user = new User();

       user.setName("张三疯");

       user.setPwd("1111");

       session.save(user);

       //6.提交事务

       tx.commit();

       //7.关闭session

       session.close();

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胖墩有点瘦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值