Hibernate入門一

一:Hibernate是什麼java

  Hibernate是用於訪問數據庫的一個框架,很是高級是徹底面向對象mysql

二:Hibernate框架的搭建sql

  1.導包:數據庫

  2.建立實體類(持久化類)session

    

public class Customer {
	private Long cust_id;
	private String cust_name;
	private String cust_source;
	private String cust_industry;
	private String cust_level;
	private String cust_phone;
	private String cust_mobile;
	public Long getCust_id() {
		return cust_id;
	}
	public void setCust_id(Long cust_id) {
		this.cust_id = cust_id;
	}
	public String getCust_name() {
		return cust_name;
	}
	public void setCust_name(String cust_name) {
		this.cust_name = cust_name;
	}
	public String getCust_source() {
		return cust_source;
	}
	public void setCust_source(String cust_source) {
		this.cust_source = cust_source;
	}
	public String getCust_industry() {
		return cust_industry;
	}
	public void setCust_industry(String cust_industry) {
		this.cust_industry = cust_industry;
	}
	public String getCust_level() {
		return cust_level;
	}
	public void setCust_level(String cust_level) {
		this.cust_level = cust_level;
	}
	public String getCust_phone() {
		return cust_phone;
	}
	public void setCust_phone(String cust_phone) {
		this.cust_phone = cust_phone;
	}
	public String getCust_mobile() {
		return cust_mobile;
	}
	public void setCust_mobile(String cust_mobile) {
		this.cust_mobile = cust_mobile;
	}
	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
				+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
				+ ", cust_mobile=" + cust_mobile + "]";
	}
	
}

  3.建立映射文件app

    a.引入約束  hibernate-mapping-3.0.dtd框架

    b.書寫配置文件  三個標籤牢記 class id propertydom

    

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
   <!-- 配置表與實體對象的關係 -->
   <!-- package屬性:填寫一個包名.在元素內部凡是須要書寫完整類名的屬性,能夠直接寫簡答類名了. -->
<hibernate-mapping package="cn.itheima.domain" >
    <!-- 
        class元素: 配置實體與表的對應關係的
            name: 完整類名
            table:數據庫表名
     -->
    <class name="Customer" table="cst_customer" >
        <!-- id元素:配置主鍵映射的屬性
                name: 填寫主鍵對應屬性名
                column(可選): 填寫表中的主鍵列名.默認值:列名會默認使用屬性名
                type(可選):填寫列(屬性)的類型.hibernate會自動檢測實體的屬性類型.
                        每一個類型有三種填法: java類型|hibernate類型|數據庫類型
                not-null(可選):配置該屬性(列)是否不能爲空. 默認值:false
                length(可選):配置數據庫中列的長度. 默認值:使用數據庫類型的最大長度
         -->
        <id name="cust_id"  >
            <!-- generator:主鍵生成策略(明天講) -->
            <generator class="native"></generator>
        </id>
        <!-- property元素:除id以外的普通屬性映射
                name: 填寫屬性名
                column(可選): 填寫列名
                type(可選):填寫列(屬性)的類型.hibernate會自動檢測實體的屬性類型.
                        每一個類型有三種填法: java類型|hibernate類型|數據庫類型
                not-null(可選):配置該屬性(列)是否不能爲空. 默認值:false
                length(可選):配置數據庫中列的長度. 默認值:使用數據庫類型的最大長度
         -->
        <property name="cust_name" column="cust_name" >
            <!--  <column name="cust_name" sql-type="varchar" ></column> -->
        </property>
        <property name="cust_source" column="cust_source" ></property>
        <property name="cust_industry" column="cust_industry" ></property>
        <property name="cust_level" column="cust_level" ></property>
        <property name="cust_linkman" column="cust_linkman" ></property>
        <property name="cust_phone" column="cust_phone" ></property>
        <property name="cust_mobile" column="cust_mobile" ></property>
    </class>
</hibernate-mapping>

  4.建立核心配置文件(hibernate.cfg.xml)ide

<?xml version="1.0" encoding="UTF-8"?>
<!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>
         <!-- 數據庫url -->
        <property name="hibernate.connection.url">jdbc:mysql:///crm_hibernate</property>
         <!-- 數據庫鏈接用戶名 -->
        <property name="hibernate.connection.username">root</property>
         <!-- 數據庫鏈接密碼 -->
        <property name="hibernate.connection.password">root</property>
        <!-- 數據庫方言
            不一樣的數據庫中,sql語法略有區別. 指定方言能夠讓hibernate框架在生成sql語句時.針對數據庫的方言生成.
            sql99標準: DDL 定義語言  庫表的增刪改查
                      DCL 控制語言  事務 權限
                      DML 操縱語言  增刪改查
            注意: MYSQL在選擇方言時,請選擇最短的方言.
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        
        <!-- #hibernate.show_sql true 
             #hibernate.format_sql true
        -->
        <!-- 將hibernate生成的sql語句打印到控制檯 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 將hibernate生成的sql語句格式化(語法縮進) -->
        <property name="hibernate.format_sql">true</property>
        <!-- 
        ## auto schema export  自動導出表結構. 自動建表
        #hibernate.hbm2ddl.auto create        自動建表.每次框架運行都會建立新的表.之前表將會被覆蓋,表數據會丟失.(開發環境中測試使用)
        #hibernate.hbm2ddl.auto create-drop 自動建表.每次框架運行結束都會將全部表刪除.(開發環境中測試使用)
        #hibernate.hbm2ddl.auto update(推薦使用) 自動生成表.若是已經存在不會再生成.若是表有變更.自動更新表(不會刪除任何數據).
        #hibernate.hbm2ddl.auto validate    校驗.不自動生成表.每次啓動會校驗數據庫中表是否正確.校驗失敗.
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 引入orm元數據
            路徑書寫: 填寫src下的路徑
         -->
        <mapping resource="cn/itcast/domain/ Customer.hbm.xml" />
        
    </session-factory>
</hibernate-configuration>

  5.編寫測試代碼測試

public class HibernateTest01 {
    @Test
    public void test01() {    
        //1.加載配置文件
        Configuration cf = new Configuration().configure();
        //2.建立一個SessionFactory
        SessionFactory sessionFactory = cf.buildSessionFactory();
        //3.建立Session對象 Session對象相似Connection
        Session session = sessionFactory.openSession();
        //4.開啓事務
        Transaction transaction = session.beginTransaction();
        //5.執行相關操做
        Customer customer = new Customer();
        customer.setCust_name("王小智");
        customer.setCust_level("18");
        session.save(customer);
        //6.事務提交
        transaction.commit();
        //7.釋放資源
        session.close();
    }
    
    
}
相關文章
相關標籤/搜索