关于hibernate入门 小整理(一)

要部署hibernate首先要配置好xml,一般命名为hibernate.cfg.xml,接下来贴段链接mysql的配置:

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) 
        <property name="connection.pool_size">1</property>   hibernate 自带链接池-->

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management 
        <property name="current_session_context_class">thread</property>-->

        <!-- Disable the second-level cache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <mapping resource="com/sfwl/hibernate/model/Employee.hbm.xml" />
        <mapping class="com.sfwl.hibernate.model.Student"/>
    </session-factory>

</hibernate-configuration>

解释几个名词:dialect 方言(即指定使用的SQL语言,oracle和mysql是不一样的)

       show_sql (show的是sql语句)

       ddl(数据库模式定义语言 指定建表策略

mapping resource 是映射 实体类的配置文件  使用xml配置使用到。
<hibernate-mapping package="com.sfwl.hibernate.model">
<class name="Employee" >
<id name="id" ></id>
<property name="sex"></property>
<property name="age"></property>
<property name="habit"></property>
<property name="nativeplace"></property>
<property name="name"></property>
<property name="descript"></property>
</class>
</hibernate-mapping>

配置好类名,主键,各个属性即可。

mapping class 是直接映射到类  使用annotationp配置。
在类上@entity,在get方法上@id 就完成了配置。

接下来写测试类:
    Student ss=new Student();
            ss.setId();
            ss.setName("");
            ss.setSex("");
            
            Configuration cfg = new AnnotationConfiguration();
//            解析配置文件
            SessionFactory sf = cfg.configure().buildSessionFactory();          
            Session session =sf.openSession();
            session.beginTransaction();
            session.save(ss);
            session.getTransaction().commit();
            session.close();
            sf.close();

主要流程是:解析配置文件->创建会话->开始事务->操作->提交->关闭;

创建会话会花大量的时间,这边可以优化下使用单例模式:

private static SessionFactory sf = null;
    @BeforeClass
    public static void beforclass() {
        sf = new AnnotationConfiguration().configure().buildSessionFactory();
    }

    @Test
    public void test() {
        Student ss = new Student();
        ss.setId();
        ss.setName("");
        ss.setSex("");
        Session session = sf.openSession();
        session.beginTransaction();
        session.save(ss);
        session.getTransaction().commit();
        session.close();
    }
    @AfterClass
    public void afterclass(){
        sf.close();
    }

此文章为个人入门自学个人理解整理,如有错误大家笑笑指点下哈~




原文地址:https://www.cnblogs.com/weiwoduhigh/p/3152028.html