• 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.Finalprojectetchibernate.cfg.xml 放入到项目中src下:

        修改hibernate.cfg.xml文件如下:  

     1  <!--以MySQL为例:-->
     2  <hibernate-configuration>
     3      <session-factory>
     4          <!-- 配置数据库连接信息 -->
     5                  <!--数据库驱动-->
     6          <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
     7                  <!--数据库地址-->
     8          <property name="connection.url">jdbc:mysql://localhost:3306/hibernate4</property>
     9                  <!--数据库用户名-->
    10          <property name="connection.username">root</property>
    11                  <!--数据库密码-->
    12          <property name="connection.password">root</property>
    13          <!-- 数据库方言 -->
    14          <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    15      </session-factory>
    16  </hibernate-configuration>
    17  <!-- 这些配置都可以在hibernate.properties文件中找到 -->

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

        Pojo对象:

    1 public class User {
    2     private int id;
    3     private String name;
    4     private String pwd;
    5         //省略setter、getter
    6 }

     User表:user

    id

    name

    pwd

     

      e) 编辑*.hbm.xml文件

        文件名一般为pojo类的名称User.hbm.xml,放在pojo类所在的包下,头文件可以在project下查找,也可拷贝。然后将*.hbm.xml配置文件加入到hibernate.cfg.xml中.

     1 <hibernate-mapping>
     2     <class name="cn.siggy.pojo.User" table="user">
     3         <id name="id">
     4             <!-- 主键生成策略 -->
     5             <generator class="native"></generator>
     6         </id>
     7         <!-- 实体类的属性 -->
     8         <property name="name"/>    
     9         <property name="pwd"/>    
    10     </class>
    11 </hibernate-mapping>

      f) 测试:将*.hbm.xml配置文件加入到hibernate.cfg.xml中。hibernate.cfg.xml中点击Session Factory,在Mappings中add即可。   

     1 public static void main(String[] args) {
     2         //1.新建Configuration对象
     3         Configuration cfg = new Configuration().configure();
     4         //2.通过Configuration创建SessionFactory对象
     5             //在hibernate3.x中是这种写法
     6             //SessionFactory sf = cfg.buildSessionFactory();
     7         //hibernate4.3之前~hibernate4.0
     8 //        ServiceRegistry sr = new ServiceRegistryBuilder()
     9 //                            .applySettings(cfg.getProperties())
    10 //                            .buildServiceRegistry();
    11         //hibernate4.3
    12         ServiceRegistry registry = new StandardServiceRegistryBuilder()
    13                               .applySettings(cfg.getProperties())
    14                               .build();
    15         SessionFactory sf = cfg.buildSessionFactory(registry);
    16         //3.通过SessionFactory得到Session
    17         Session session = sf.openSession();
    18         //4.通过session对象 得到Transaction对象
    19         //开启事务
    20         Transaction tx = session.beginTransaction();
    21         //5.保存数据
    22         User user = new User();
    23         user.setName("张三疯");
    24         user.setPwd("1111");
    25         session.save(user);
    26         //6.提交事务
    27         tx.commit();
    28         //7.关闭session
    29         session.close();
    30     }
  • 相关阅读:
    个人阅读笔记05—数据流图
    JVM(三)程序计数器【PC寄存器】
    JVM(三)运行时数据区的概述,线程概述
    JVM(二)表示两个Class对象是否为同一个类;Java程序对类的使用
    我的面试经之JVM(二)双亲委派机制
    我的面试经之JVM(二) ClassLoader补充【获取类的加载器的方式】
    我的面试经之JVM(二)类加载器的分类
    我的面试经之JVM(二)类加载器子系统ClassLoader类的加载过程
    Python小白要了解的模块大概用途(随学习进度更新),import xxxx
    Python常见的模块(知乎转载)
  • 原文地址:https://www.cnblogs.com/jiangjianzhu/p/5534618.html
Copyright © 2020-2023  润新知