在eclipse中配置hibernate

来源:互联网 发布:vue.js省市区三级联动 编辑:程序博客网 时间:2024/05/18 00:25

我们在实际开发的过程中经常会用到hibernate,不过我们使用的往往是已经配置好的环境,一般只知道调用save(Object o)之类的方法,那hibernate一开始是怎么配置起来的呢,下面我就试着从新建一个工程到配置hibernate再到最后能把数据保存到数据库(oracle10g)为例梳理一下。

1、我们需要下载hibernate所用到的jar包,具体可以去这里下载,关于数据库的jar版本是ojdbc-14.jar网上很容易找到

2、我们新建一个简单的java工程HibernateApplication,然后再工程下面新建一个lib包,这个lib包与src目录同级,完了之后添加相关的依赖

3、新建一个实体类

/** * Tparam entity. *  * @author MyEclipse Persistence Tools */public class Tparam implements java.io.Serializable {// Fieldsprivate Long fcode;// 代号private String fremark;// 参数含义说明private String fvalue;// 参数值private Long fparamtype;// 参数类型 0:TEdit 1:TCheckBox 2:TRadioBox 3:TcomboBoxprivate String fparamitems;// 参数取值列表(ParamType=2,3)private String fcontent;// 参数说明private Long fxz;// 0通用参数 1深圳专用 2东莞专用//查询参数private String p_fcode;private String p_fvalue;// Constructors/** default constructor */public Tparam() {}/** minimal constructor */public Tparam(Long fcode) {this.fcode = fcode;}/** full constructor */public Tparam(Long fcode, String fremark, String fvalue, Long fparamtype,String fparamitems, String fcontent, Long fxz) {this.fcode = fcode;this.fremark = fremark;this.fvalue = fvalue;this.fparamtype = fparamtype;this.fparamitems = fparamitems;this.fcontent = fcontent;this.fxz = fxz;}// Property accessorspublic Long getFcode() {return this.fcode;}public String getP_fvalue() {return p_fvalue;}public void setP_fvalue(String p_fvalue) {this.p_fvalue = p_fvalue;}public String getP_fcode() {return p_fcode;}public void setP_fcode(String p_fcode) {this.p_fcode = p_fcode;}public void setFcode(Long fcode) {this.fcode = fcode;}public String getFremark() {return this.fremark;}public void setFremark(String fremark) {this.fremark = fremark;}public String getFvalue() {return this.fvalue;}public void setFvalue(String fvalue) {this.fvalue = fvalue;}public Long getFparamtype() {return this.fparamtype;}public void setFparamtype(Long fparamtype) {this.fparamtype = fparamtype;}public String getFparamitems() {return this.fparamitems;}public void setFparamitems(String fparamitems) {this.fparamitems = fparamitems;}public String getFcontent() {return this.fcontent;}public void setFcontent(String fcontent) {this.fcontent = fcontent;}public Long getFxz() {return this.fxz;}public void setFxz(Long fxz) {this.fxz = fxz;}}

4、新建一个Tparam.hbm.xml文件:

<?xml version="1.0"?>              <!DOCTYPE hibernate-mapping PUBLIC              "-//Hibernate/Hibernate Mapping DTD//EN"              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><hibernate-mapping><class name="Tparam" table="TPARAM" schema="ZY"><id name="fcode" type="java.lang.Long"><column name="FCODE" precision="5" scale="0" /><generator class="assigned" /></id><property name="fremark" column="FREMARK" type="string" /><property name="fvalue" column="FVALUE" type="string" /><property name="fparamtype" column="FPARAMTYPE" type="long" /><property name="fparamitems" column="FPARAMITEMS" type="string" /><property name="fcontent" column="FCONTENT" type="string" /><property name="fxz" column="FXZ" type="long" /></class></hibernate-mapping>

5、新建一个hibernate.cfg.xml文件:

<?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>                  <!-- ________________ To be Edited _________________ -->                  <property name="connection.url">jdbc\:oracle\:thin\:@172.31.0.203\:1521\:ztsysyp</property>                  <property name="connection.username">HISBASE</property>                  <property name="connection.password">0CB75C052C31C5E7</property>                  <!-- _____________ End of To be Edited ______________ -->                                <property name="connection.driver_class">                          oracle.jdbc.driver.OracleDriver                  </property>                  <property name="dialect">                     org.hibernate.dialect.OracleDialect                  </property>                  <property name="current_session_context_class">thread</property>                                <!-- _________ Defining the Mapping Files ___________ -->                  <mapping resource="Tparam.hbm.xml" />              </session-factory>              </hibernate-configuration>

6、新建一个测试类:

import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class  SimpleTest{public static void main(String[] args) {SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();Session session = sessionFactory.getCurrentSession();Transaction tx = session.beginTransaction();Tparam param = new Tparam();param.setFcode(12345678l);param.setFremark("hibernate test");session.save(param);tx.commit();}}



0 0
原创粉丝点击