1. 导入jar包
在lib->required文件夹下
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
slf4j-api-1.6.11.jar
还有一个核心包
hibernate3.jar
2. 导入mysql驱动包
mysql-connector-java-5.1.6-bin.jar
3. 新建User类,包com.test.po
public class User{
private Integer userId;
private String username;
private String password;
private Character sex;
private Date birthday;
private Float salary;
}
4. 新建User.hbm.xml映射文件(描述po类和数据库表的映射关系,最好和类名一致,放在同一个包下)
<?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的映射配置-->
<hibernate-mapping>
<!--描述类和表的映射关系-->
<class name="com.test.po.User" table="t_user" schema="h3db">
<!--对象OID和表中主键的对应关系-->
<id name="userId" column="user_id">
<!--主键生成策略-->
<generator class="native"></generator>
</id>
<!--类中属性和表中字段的对应关系-->
<property name="username" column="username"></property>
<property name="password" column="password"></property>
<property name="sex" column="sex"></property>
<property name="birthday" column="birthday"></property>
<property name="salary" column="salary"></property>
</class>
</hibernate-mapping>
5. 编写Hibernate核心配置文件: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核心配置-->
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/h3db</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- jdbc连接池配置:c3p0 -->
<property name="connection.pool_size">1</property>
<!-- SQL 方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 将session关联交给当前线程
<property name="current_session_context_class">thread</property>
-->
<!-- 二级缓存配置
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
-->
<!-- 是否显示sql语句到控制台 -->
<property name="show_sql">true</property>
<!-- 数据库表的生成方式:
create:无论表是否存在,每次执行都会重新创建表结构(会清空所有数据)
update:如果表已存在执行更新,如果表不存在创建(不会删除已有数据)
-->
<property name="hbm2ddl.auto">update</property>
<!--引入映射文件-->
<mapping resource="com/test/po/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6.编写HibernateUtil类
public class H3Util{
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory(){
try{
//读取类加载路径文件hibernate.cfg.xml文件创建sessionFactory对象
return new Configuration().configure().buildSessionFactory();
}catch(Throwable e){
System.err.pringln("Initial SessionFactory creation failed." + e);
throw new ExceptionInInitializerError(e);
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
- 新建测试类,JUnit类测试
- Hibernate常见错误:
注意:使用hibernate对数据库的操作,必须基于事务
1、Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListeners
导入:hibernate-jpa-2.0-api-1.0.1.Final.jar