Hibernate相关配置文件、映射文件案例、工具类、总结相关
2017-10-21 10:58
387 查看
配置文件:
映射文件案例
工具类
总结相关:
<?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 -- 如果有表,使用原来的表.同时校验映射文件与表中字段是否一致如果不一致就会报错.
相关文章推荐
- javaEE 用户、部门、角色、权限实体的关系设计与hibernate映射配置文件关系总结
- SpringMVC_v02中的总结(web项目、java项目中引入Config.properties,加载hibernate映射文件的配置的区别)
- Hibernate 配置文件与映射文件 总结
- hibernate 各种映射的配置文件总结
- 配置Middlegen-Hibernate和hibernate-extensions以产生hbm映射文件和POJO
- Hibernate配置文件和映射元素解释
- Hibernate配置文件中映射元素详解
- 使用Spring的mappingDirectoryLocations来配置Hibernate映射文件
- Hibernate配置文件中映射元素详解
- 使用Spring的mappingDirectoryLocations来配置Hibernate映射文件
- Hibernate配置(一):用MiddleGen生成映射文件和用hbm2java生成pojo zz
- Hibernate配置文件和映射元素详解
- 使用MiddleGen 产生hibernate的数据库表映射文件-JSP教程,数据库相关
- Hibernate配置文件和映射元素详解
- 配置Middlegen-Hibernate和hibernate-extensions以产生hbm映射文件和POJO (转)
- 使用Spring的mappingDirectoryLocations来配置Hibernate映射文件
- Hibernate配置文件中映射元素详解
- hibernate的映射文件配置问题
- Spring 整合 Hibernate 的一处简化引入PO映射文件配置
- Hibernate配置文件中映射元素详解