Hibernate 第一个hibernate程序

来源:互联网 发布:手机抢号软件 编辑:程序博客网 时间:2024/05/29 17:04

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>

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

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

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

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

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

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

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

    </session-factory>

</hibernate-configuration>

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

Pojo对象

publicclass User {

   privateintid;

   private Stringname;

   private Stringpwd;

   }

User表:user

id

name

pwd

 

 

 

e)        编辑*.hbm.xml文件

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

放在pojo类所在的包下

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

<hibernate-mapping>

   <classname="cn.siggy.pojo.User"table="user">

      <idname="id">

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

         <generatorclass="native"></generator>

      </id>

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

      <propertyname="name"/> 

      <propertyname="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中是这种写法

           //SessionFactorysf = 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();

    }

 

原创粉丝点击