初识hibernate

这是建立第一个简单的hibernate项目,参考极客学院《Hibernate 框架入门》免费视频,

一,环境配置

1.用的是在hibernate官网下载的hibernate-release-4.3.11.Final包,
主要的jar包放在“\hibernate jar 包\hibernate-release-4.3.11.Final\lib\required”文件夹下,
建立项目需要把这个里面的全部包导入,还有需要连接相应数据库的jar包,
2.需要在所在项目src目录下复制这些文件“hibernate jar 包\hibernate-release-4.3.11.Final\project\etc” 目录下的
log4j.properties文件和hibernate.cfg.xml
3,注意在hibernate.cfg.xml配置数据库参数时,里面的参数其实在\hibernate-release-4.3.11.Final\project\etc目录下的hibernate.properties文件中有,有疑问可以查看,

二,编写代码

hibernate.cfg.xml
配置连接数据库的各种操作

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<!-- 
    <session-factory name="foo">
        <property name="show_sql">true</property>
        <mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/>
        <class-cache
            class="org.hibernate.test.legacy.Simple"
            region="Simple"
            usage="read-write"/>
             -->

    <!--指定连接数据库用的驱动-->
    <session-factory>
            <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>


            <!--指定连接数据库的路径-->           
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>

            <!--指定连接数据库的用户名-->          
            <property name="connection.username">scott</property>

            <!--指定连接数据库的密码-->
            <property name="connection.password">sinstar</property>     


            <!--指定数据库使用的SQL方言-->            
            <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>

            <!--当show_sql属性为true时表示在程序运行时在控制台输出SQL语句,默认为false,建议在调试程序时设为
            true,发布程序之前再改为false,因为输出SQL语句会影响程序的运行速度-->

            <property name="show_sql">true</property>

            <!-- 注意这个包是用的“/”,而且需要完整的路径名 -->         
            <mapping resource="com/bright/bean/Bookbean.hbm.xml"/><!--指定持久化类映射文件-->     

    </session-factory>  
</hibernate-configuration>

Bookbean.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="com.bright.bean.Bookbean" table="mybook">
        <id name="id" column="b_id">

            <!-- 这个属性就是设置自增长序列,在bean 里面的id都不用直接给值,空的传过去就行,非常好用 -->
            <generator class="native"></generator>
        </id>
        <property name="bname" column="b_name"></property>
        <property name="price" column="b_price"></property>
    </class>

</hibernate-mapping>

Bookbean.java
通用格式就行

package com.bright.bean;

public class Bookbean {
    private int id;
    private String bname;
    private String price;
    public Bookbean() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Bookbean(int id, String bname, String price) {

        this.id = id;
        this.bname = bname;
        this.price = price;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getBname() {
        return bname;
    }
    public void setBname(String bname) {
        this.bname = bname;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }   

}

这里面包含主要的操作代码,
Booktest.java

package com.bright.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import com.bright.bean.Bookbean;

public class Booktest {

    public void createTable(){
        Configuration cfg=new Configuration().configure();

        SchemaExport se=new SchemaExport(cfg);

        se.create(true,true);

    }

    public void add(){

        /*
         * configurationo 类负责管理hibernate的配置信息,并根据配置信息启动hibernate
         * hibernate的配置有两种方法
         * 1.属性文件
         * 2.xml文件   一般企业级开发都是用这种,所以也比较推荐这种方法,即hibernate.cfg.xml文件
         */
        Configuration cfg=new Configuration().configure();

        //在hibernate4.0以后,这个方法已经过时
        //SessionFactory factory=cfg.buildSessionFactory();

        //需要用到下面的方法来获得factory

        StandardServiceRegistryBuilder ssrb=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());

        ServiceRegistry service=ssrb.build();
        SessionFactory factory=cfg.buildSessionFactory(service);

        //应用程序从SessionFactory中获取session对象,记得最后一定要session.close();

        //这里的session与以前的http session没有任何关系
        Session session=factory.openSession();

        //获取事务对象,开启事务
        Transaction tx=session.beginTransaction();

        Bookbean bb=new Bookbean();

        /*
         * 由于在Bookbean.hbm.xml文件里面定义了
         * 
         * <id name="id" column="b_id">         
         *  <!-- 这个属性就是设置自增长序列,在bean 里面的id都不用直接给值,空的传过去就行,非常好用 -->
         *  <generator class="native"></generator>
         * </id>
         * 
         * 自增长属性,所以id都不用赋值
         */

        //bb.setId(11);
        bb.setBname("sinstar");
        bb.setPrice("12");

        //session中的所有方法都会抛出一个hibernateException异常,所以建议放到try catch中操作

        try {
            session.save(bb);
            tx.commit();    //事务提交  , hibernate中的事务不是自动提交的

        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();  //如果发生异常,则让事务回滚!
        }finally{
            session.close();
        }       
    }   

}

然后随便写一个main方法运行就行了,

一个生成XXX.hbm.xml的步骤
这里写图片描述

项目的文件如图,
其中的log4j.properties文件暂时没动

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值