配置文件:
<?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>
<!-- 记住:先配置SessionFactory标签,一个数据库对应一个SessionFactory标签 -->
<session-factory>
<!-- 必须要配置的参数有5个,4大参数,数据库的方言 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选配置 -->
<!-- 显示SQL语句,在控制台显示 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 生成数据库的表结构
update:如果没有表结构,创建表结构。如果存在,不会创建,添加数据
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 开启绑定本地的session -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 配置C3P0的连接池 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 映射配置文件,需要引入映射的配置文件 -->
<mapping resource="com/itheima/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
映射文件案例
<?xml version="1.0" encoding="UTF-8"?>
<!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="com.itheima.domain.Customer" table="cst_customer">
<id name="cust_id" column="cust_id">
<generator class="native"/>
</id>
<property name="cust_name" column="cust_name"/>
<property name="cust_user_id" column="cust_user_id"/>
<property name="cust_create_id" column="cust_create_id"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_linkman" column="cust_linkman"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</class>
</hibernate-mapping>
工具类
package cn.crm.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* <p>
* Title: HibernateUtil
* </p>
* <p>
* Description:session工具类
* </p>
* <p>
* Company: www.itcast.com
* </p>
*
*/
public class HibernateUtil {
// 会话工厂,以单例方式管理
private static SessionFactory sessionFactory;
// ThreadLocal存储session
private static ThreadLocal<Session> session = new ThreadLocal<Session>();
// 以单例方式管理sessionFactory
static {
try {
sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
throw new HibernateException("初始化会话工厂失败!");
}
}
//得到一个单例的会话工厂
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
//获取一个新session
public static Session openSession(){
return sessionFactory.openSession();
}
//获取当前与线程绑定的session,如果获取不到则创建一个新session并与当前线程绑定
// public static Session getCurrentSession() throws HibernateException {
// //获取当前线程绑定的session
// Session s = (Session) session.get();
// if (s == null) {
// //创建一个新session
// s = sessionFactory.openSession();
// //新session并与当前线程绑定
// session.set(s);
// }
// return s;
// }
public static Session getCurrentSession() throws HibernateException {
return sessionFactory.getCurrentSession();
}
//关闭当前线程绑定的session
// public static void closeSession() throws HibernateException {
// //获取当前线程绑定的session
// Session s = (Session) session.get();
// if (s != null){
// //关闭session
// s.close();
// }
// session.set(null);
// }
public static void closeSession() throws HibernateException {
sessionFactory.getCurrentSession().close();
}
}
总结相关:
1.导入lib下required(必要)的包
2.导入log4j下的包(框架日志包)
3.导入数据库驱动包
4.配置文件约束:<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> (在hibernate-core下核心文件第一个包下的hibernate-mapping-3.0)
5.核心配置(可选)
* hibernate.show_sql -- 显示SQL
<property name="hibernate.show_sql">true</property>
* hibernate.format_sql -- 格式化SQL
<property name="hibernate.format_sql">true</property>
* hibernate.hbm2ddl.auto -- 通过映射转成DDL语句(以下值)
。。。
* create -- 每次都会创建一个新的表.---测试的时候
* create-drop -- 每次都会创建一个新的表,当执行结束之后,将创建的这个表删除.---测试的时候
* update -- 如果有表,使用原来的表.没有表,创建一个新的表.同时更新表结构.
* validate -- 如果有表,使用原来的表.同时校验映射文件与表中字段是否一致如果不一致就会报错.