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);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41534115/article/details/84070407