1. 程式人生 > >hibernate測試

hibernate測試

如果不懂怎麼建立hibernate,請看:https://blog.csdn.net/qq_41534115/article/details/84068293

hibernate.cfg.xml配置

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/school1</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">970817</property>

        <!-- <property name="connection.username"/> -->
        <!-- <property name="connection.password"/> -->
        <!--方言-->
        <!--myisam不支援事務  支援全文索引-->
        <!--innodb支援事務  不支援全文索引-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!--<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>-->
        <!--<property name="hibernate.dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>-->
        <!--建立臨時表-->
        <property name="hibernate.hbm2ddl.auto">create-drop</property>
        <!--通過配置檔案建立相應的表結構-->
        <!--<property name="hibernate.hbm2ddl.auto">create</property>-->
        <!--通過配置檔案更新表結構-->
        <!--<property name="hibernate.hbm2ddl.auto">update</property>-->
        <!--在執行之前驗證表結構和類結構是否匹配-->
        <!--<property name="hibernate.hbm2ddl.auto">validate</property>-->
        <!--顯示sql-->
        <property name="hibernate.show_sql">true</property>
        <!--格式化sql-->
        <property name="hibernate.format_sql">true</property>
        <mapping resource="Course.hbm.xml"/>
        <mapping resource="Grade.hbm.xml"/>
        <mapping resource="Teacher.hbm.xml"/>
        <mapping resource="User.hbm.xml"/>
        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>
HibernateUtil類
package com.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    static SessionFactory factory=null;
    static {
        //建立配置檔案
        Configuration config=new Configuration();
        //讀取配置檔案
        config.configure("/hibernate.cfg.xml");
        //建立sessionFactory
        factory=config.buildSessionFactory();

    }
    //獲取session
    public static Session getCurrentSession(){

        Session session=factory.openSession();
        return session;
    }
    //關閉session
    public static void closeSession(Session session){
        if (session!=null){
            session.close();
        }
    }
}

Test類

package com.jie;

import com.jie.domain.User;
import com.util.HibernateUtil;
import org.hibernate.Session;

import java.util.List;

public class Test {
    public static void main(String[]args){
        Session session= HibernateUtil.getCurrentSession();
        //查詢全部資料
        List list=session.createQuery("from User").list();
        System.out.println(list);
        //查詢一個數據
        /*
        get 和 load 的區別
        1.get直接查詢資料庫得到當前物件
        2.load(懶載入)方法先建立一個代理物件,該代理物件只有主鍵欄位有值,其他欄位沒有值,當使用非主鍵欄位時,才會查詢資料
         */

        User user1=session.get(User.class,1);
        System.out.println(user1);
        User user2=session.load(User.class,1);
        HibernateUtil.closeSession(session);
    }
}