hibernate第一個程序

hibernate配置文件:hibernate.cfg.xmljava

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
mysql

<hibernate-configuration>
    <session-factory name="foo">
        <!-- 配置數據庫信息 --> 
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql:///hibernate_20120328</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
sql

        <!-- 其餘配置 -->
        <property name="hibernate.show_sql">true</property><!--顯示sql默認是false不顯示SQL語句-->
        <property name="hibernate.format_sql">false</property>
 
       <!-- 
            create:先刪除,再建立
            update:若是表不存在就建立,不同就更新,同樣就什麼都不作。
            create-drop:初始化時建立表,SessionFactory執行close()時刪除表。
            validate:驗證表結構是否一致,若是不一致,就拋異常。
         -->

        <property name="hbm2ddl.auto">update</property>
    
    
        
<!-- 導入映射文件 -->
        <mapping resource="cn/itcast/a_helloworld/User.hbm.xml"/>數據庫

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

映射文件:User.hbm.xmlapp

<?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 package="cn.itcast.a_helloworld">
  
 <!--name:實體類名,table:數據庫數據表-->
    <class name="User" table="t_user">
        <id name="id" type="int" column="id">
            <generator class="native"/>
<!--native:自增加-->
        </id>
      
 <!--column:列名-->
        <property name="name" type="string" column="name" length="20"/>
    </class>
    
</hibernate-mapping>
ide

User.javaui

package cn.itcast.a_helloworld;
/**
 * 實體
 */
public class User {
    private int id;
    private String name;
this

    public int getId() {
        return id;
    }
url

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "[User: id=" + id + ", name=" + name + "]";
    }

}

App.java:

package cn.itcast.a_helloworld;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class App {

    private static SessionFactory sessionFactory;

    static {
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml"); // 讀取指定的主配置文件
        sessionFactory = cfg.buildSessionFactory(); // 根據生成Session工廠
    }

    @Test
    public void testSave() throws Exception {
        User user = new User();
        user.setName("張三");

        // 保存
        Session session = sessionFactory.openSession(); // 打開一個新的Session
        Transaction tx = session.beginTransaction(); // 開始事務

        session.save(user);

        tx.commit(); // 提交事務
        session.close(); // 關閉Session,釋放資源
    }

    @Test
    public void testGet() throws Exception {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        User user = (User) session.get(User.class, 1); // 獲取
        System.out.println(user);

        tx.commit();         session.close();     } }  

相關文章
相關標籤/搜索