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、执行结果