Hibernate学习2 — 第一个Hibernate程序

1.    第一个Hibernate项目

1.1. 前期工作

我们首先需要建立一个Java项目,加入如下jar

       * HIBERNATE_HOME/hibernate3.jar

       * HIBERNATE_HOME/lib/*.jar

       * 数据库驱动

复制hibernate.cfg.xmllog4j.properties到项目的src目录下,这两个文件位于

       * HIBERNATE_HOME/etc/

1.2. 建立JavaBean对象(实体类)

Javabean对象是用来映射数据库的一条记录。例如:

package po;

//对应数据库T_CUSTOMER中的一条记录,JavaBean规范编写

public class Customer {

       private String account;

       private String password;

       private String cname;

       private double cbalance;

       public Customer(){}

       public Customer(String account,String password,String cname,double cbalance){

              this.account = account;

              this.password = password;

              this.cname = cname;

              this.cbalance = cbalance;

       }

       public String getAccount() {

              return account;

       }

       public void setAccount(String account) {

              this.account = account;

       }

       public double getCbalance() {

              return cbalance;

       }

       public void setCbalance(double cbalance) {

              this.cbalance = cbalance;

       }

       public String getCname() {

              return cname;

       }

       public void setCname(String cname) {

              this.cname = cname;

       }

       public String getPassword() {

              return password;

       }

       public void setPassword(String password) {

              this.password = password;

       }

}

1.3. 建立类与表的映射文件(.hbm.xml

<?xml version=”1.0″?>

<!DOCTYPE hibernate-mapping PUBLIC

    “-//Hibernate/Hibernate Mapping DTD 3.0//EN”

    “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>

<hibernate-mapping>

       <class name=”po.Customer” table=”T_CUSTOMER”>       <!– 类和表对应 –>

              <id name=”account” column=”ACCOUNT”>                            <!– 主键 –>

                     <!– 主键生成策略,assigned表示由用户赋值 –>

<generator class=”assigned”/>                   

              </id>

              <property name=”password” column=”PASSWORD”/>     

              <property name=”cname”      column=”CNAME”/>    

              <property name=”cbalance”   column=”CBALANCE”/>             

       </class>

</hibernate-mapping>

 

1.4. 创建hibernate配置文件(hibernate.cfg.xml)

hibernate.cfg.xmlhibernate的配置信息,为了方便跟踪sql执行,在hibernate.cfg.xml文件中加入<property name=”hibernate.show_sql”>true</property>,例如:

<?xml version=’1.0′ encoding=’UTF-8′?>

<!DOCTYPE hibernate-configuration PUBLIC

          “-//Hibernate/Hibernate Configuration DTD 3.0//EN”

          “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>

<hibernate-configuration>

<session-factory>

       <property name=”connection.username”>scott</property>

       <property name=”connection.url”>jdbc:odbc:School</property>

       <property name=”dialect”>

              org.hibernate.dialect.Oracle9Dialect

       </property>

       <property name=”connection.password”>tiger</property>

       <property name=”connection.driver_class”>

              sun.jdbc.odbc.JdbcOdbcDriver

       </property>

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

       <mapping resource=”po/Customer.hbm.xml” />

</session-factory>

</hibernate-configuration>

1.5. 编写hbm2ddl工具类

编写hbm2ddl工具类,将实体类生成数据库表,代码如下:

public class ExportDB {

    public static void main(String[] args) {

       //读取hibernate.cfg.xml文件

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

       SchemaExport export = new SchemaExport(cfg);

       export.create(true, true);

    }

}

 

1.6. CRUD操作

1.6.1.添加

//第一步,读取Hibernate配置文件

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

//第二步,Hibernate操作要使用Session来完成,SessionSessionFactory管理,

//生成SessionFactory

SessionFactory sf = conf.buildSessionFactory();

//第三步:打开Session,Session的事务不是自动提交的

Session session = sf.openSession();

Customer cus = new Customer(“222″,”222″,”张三“,4000);

Transaction tran = session.beginTransaction();

session.save(cus);

tran.commit();

1.6.2.删除

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

SessionFactory sf = conf.buildSessionFactory();

Session session = sf.openSession();

 

Customer cus = new Customer();

session.load(cus, “111”);

 

Transaction tran = session.beginTransaction();

session.delete(cus);

tran.commit();

1.6.3.更新

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

              SessionFactory sf = conf.buildSessionFactory();

              Session session = sf.openSession();

             

              Customer cus = new Customer();

              session.load(cus, “111”);

             

              cus.setCbalance(cus.getCbalance() + 1000);

              Transaction tran = session.beginTransaction();

              session.update(cus);

              tran.commit();

1.6.4.查询

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

              SessionFactory sf = conf.buildSessionFactory();

              Session session = sf.openSession();

              Customer cus = new Customer();

              session.load(cus, “111”);

              System.out.println(cus.getAccount());

              System.out.println(cus.getPassword());

              System.out.println(cus.getCname());

              System.out.println(cus.getCbalance());

发表评论

电子邮件地址不会被公开。 必填项已用*标注

昵称 *