1,xml配置版
步骤一:导入相关jar包
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.6.Final</version>
</dependency>
</pre><pre name="code" class="html"><dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
步骤二:配置文件hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<span style="white-space:pre"> </span><session-factory>
<span style="white-space:pre"> </span><!-- 数据库连接设置 -->
<span style="white-space:pre"> </span><property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<span style="white-space:pre"> </span><property name="connection.url">jdbc:mysql://localhost/3306/数据库名</property>
<span style="white-space:pre"> </span><property name="connection.username">用户名</property>
<span style="white-space:pre"> </span><property name="connection.password">密码</property>
<span style="white-space:pre"> </span><!-- 数据库连接池 -->
<span style="white-space:pre"> </span><property name="connection.pool_size">1</property>
<span style="white-space:pre"> </span><!-- 数据库方言 -->
<span style="white-space:pre"> </span><property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<span style="white-space:pre"> </span><!-- 将hibernate绑定到当前线程 -->
<span style="white-space:pre"> </span><property name="current_session_context_class">thread</property>
<span style="white-space:pre"> </span><!-- hibernate二级缓存 -->
<span style="white-space:pre"> </span><property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<span style="white-space:pre"> </span><!-- 控制台是否展示sql语句 -->
<span style="white-space:pre"> </span><property name="show_sql">true</property>
<span style="white-space:pre"> </span><!-- 删除并且再次创建数据库表 -->
<span style="white-space:pre"> </span><property name="hbm2ddl.auto">update</property>
<span style="white-space:pre"> </span></session-factory>
</hibernate-configuration>
步骤三:创建实体类及映射文件
public class Event {
private Integer id;
private String name;
public void setId(Integer id) {
this.id = id;
}
public Integer getId(){
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Event" table="event(表名)">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="event_name" />
</property>
</class>
</hibernate-mapping>
</xml>
<mapping resource="com/.../entity/Event.hbm.xml" />
<pre name="code" class="html">步骤五:测试
<span style="font-family: Arial, Helvetica, sans-serif;">public class EventTest {</span>
<pre name="code" class="java"> public static void main(String[] args) {
Event event = new Event();
event.setName("hibernate");
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure("config/hibernate.cfg.xml").buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(event);
session.getTransaction().commin();
session.close();
sf.close();
}
}