zbb20170218_hibernate

Posted Daryll

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zbb20170218_hibernate相关的知识,希望对你有一定的参考价值。

1、结构图

2、class文件

Event.java

package com.zbb.entity;

import java.util.Date;

public class Event {
    private Long id;

    private String title;
    private Date date;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }

}

Test.java

package com.zbb.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class Test {
    public static void main(String[] args) {
        creteTable();
    }
    public static void creteTable(){
        new SchemaExport(getConfiguration()).create(true, true);
    }
    public static Session getSession(){
        return getSessionFactory().openSession();
    }
    public static SessionFactory getSessionFactory(){
        return getConfiguration().buildSessionFactory();
    }
    public static Configuration getConfiguration(){
        return new Configuration().configure();
    }
}

3、配置文件

Event.hbm.xml

<?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>
    <class name="com.zbb.entity.Event">
        <id name="id" column="e_id"></id>
        <property name="title" column="e_titme"></property>
        <property name="date" type="timestamp" column="e_date"></property>
    </class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version=\'1.0\' encoding=\'UTF-8\'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>

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

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- 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>
    <property name="format_sql">true</property>
    <mapping resource="com/zbb/entity/Event.hbm.xml" />

</session-factory>

</hibernate-configuration>

4、执行结果

 

以上是关于zbb20170218_hibernate的主要内容,如果未能解决你的问题,请参考以下文章

zbb20170216_spring_ioc

zbb20170824 oracle scp

zbb20170216_spring_aop

zbb20170301_好看的404和500页面

总结(20170218~20170310)

zbb20170227_spring_quartz