hibernate開發流程

來源:互聯網
上載者:User

標籤:map   set   pack   img   images   classpath   分享   not   ber   

 

 

開發流程,注意:每個hibernate版本在整合的時候是不太一樣的。本次使用的是hibernate-distribution-3.6.10.Final-dist

一、開發流程

1)在資料庫中建立表,代碼如下:

CREATE TABLE USER(id INT PRIMARY KEY AUTO_INCREMENT,uName VARCHAR(50) NOT NULL,uGender CHAR(2),uBirthday DATE);

 

2)在工程中建立實體類

public class User {    private int id;    private String uName;    private String uGender;    private Date uBirthday;        public User() {        // TODO Auto-generated constructor stub    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getuName() {        return uName;    }    public void setuName(String uName) {        this.uName = uName;    }    public String getuGender() {        return uGender;    }    public void setuGender(String uGender) {        this.uGender = uGender;    }    public Date getuBirthday() {        return uBirthday;    }    public void setuBirthday(Date uBirthday) {        this.uBirthday = uBirthday;    }

 

3)建立對象和表的對應檔,*.hbm.xml。User.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping     package="com.huitong.entity">        <class name="User" table="user">                <id name="id" column="id">            <generator class="native"/>        </id>                <property name="uName" column="uName"></property>        <property name="uGender" column="uGender"></property>        <property name="uBirthday" column="uBirthday"></property>    </class></hibernate-mapping>

 

4)hibernate的設定檔,放在src/目錄下。hibernate.cfg.xml.

包含兩部分

  • 資料庫連接配置
  • 載入對應檔
<!DOCTYPE hibernate-configuration PUBLIC    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- 設定資料庫串連 -->        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="hibernate.connection.url">jdbc:mysql:///day17?useSSL=true</property>        <property name="hibernate.connection.username">root</property>        <property name="hibernate.connection.password">root</property>        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>                <property name="hibernate.show_sql">true</property>                <!-- 載入對應檔 -->        <mapping resource="com/huitong/entity/User.hbm.xml"/>    </session-factory></hibernate-configuration>

小技巧:在myeclipse中要將多個jar檔案配置到classpath中:

<1>首先點中第一個jar包

<2>shift+點擊最後一個jar檔案,選中所有jar檔案

 

 5)簡單測試

    public void fun1(){        User user = new User();        user.setuName("good");        user.setuBirthday(new Date());                Configuration configuration = new Configuration();        configuration.configure();                SessionFactory sf = configuration.buildSessionFactory();        Session session = sf.openSession();                Transaction bt = session.beginTransaction();        session.save(user);        bt.commit();                session.close();        sf.close();            }

 

hibernate開發流程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.