1.新建web工程,将hibernate部署在eclipse的工程里
2.将相关jar放入lib文件夹中
3.编写hibernate核心文件
<?xml version='1.0' encoding='utf-8'?>
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ License: GNU
Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt
file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接设置 -->
<!--数据库驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--数据库连接 -->
<property name="connection.url">jdbc:mysql://localhost:3306/ssdb</property>
<!--数据库用户名 -->
<property name="connection.username">root</property>
<!--数据库密码 -->
<property name="connection.password">xiaomingjun945</property>
<!-- JDBC连接池最大连接数量(使用内置的) -->
<property name="connection.pool_size">1</property>
<!-- 指定与Hibernate进行交互的特定SQL方言。 -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!--禁用二级缓存 -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- 将所有执行的SQL都打印出来 -->
<property name="show_sql">true</property>
<!-- 在启动时删除并重新创建数据库模式 -->
<property name="hbm2ddl.auto">create</property>
<!--将持久类的映射配置文件添加到配置中 -->
<mapping resource="com/entity/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
4.编写entity实体类
package com.entity;
public class Person {
private Long ID;
private String name;
private String sex;
public Long getID() {
return ID;
}
public void setID(Long iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(Long iD, String name, String sex) {
super();
ID = iD;
this.name = name;
this.sex = sex;
}
}
5.编写Person实体类的配置文件
<?xml version="1.0"?>
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ License: GNU
Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt
file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--package属性对应于我们java类所在的包名 -->
<hibernate-mapping package="com.entity">
<!--name对应的Java类的类名(不需要加上包名),table对应数据库与之对应的表名 -->
<class name="Person" table="person">
<!-- <id/>标签代主键列,name对应Java类中属性名,column对应数据库中列名(此属性不填写默认Java类中属性名) -->
<id name="ID" column="id">
<!--generator通知Hibernate用于为该实体生成主键值的策略,class值"increment":用于为long, short或者int类型生成唯一标识 -->
<generator class="increment" /><!--详细了解generator可去百度科普 -->
</id>
<!--property对应除主键外的其他列,其中可以用type属性来设置该列的数据存储类型,如果在映射中未指定type属性, -->
<!--则Hibernate将尝试自动确定正确的转换和映射类型,方法是使用Java反射来确定已声明属性的Java类型,并为该Java类型使用默认映射类型。 -->
<property name="name" column="name" /><!--想了解详细type属性值的可以去百度科普一下,这里我就不一一列出了 -->
<property name="sex" column="sex" />
</class>
</hibernate-mapping>
6.以上hibernate的开发环境基本配置完毕,下面编写测试类测试以上环境配置是否成功
package com.entity;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class Main {
private SessionFactory sessionFactory;
protected void initDataBase() throws Exception {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
try {
setSessionFactory(new MetadataSources(registry).buildMetadata().buildSessionFactory());
} catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
Main main = new Main();
main.initDataBase();
} catch (Exception e) {
e.printStackTrace();
}
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
7.运行成功,控制台打印信息
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
Hibernate: drop table if exists person
五月 08, 2018 12:36:42 上午 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@9635fa] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: create table person (id bigint not null, name varchar(255), sex varchar(255), primary key (id)) engine=InnoDB
五月 08, 2018 12:36:42 上午 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection