映射文件配置,核心文件配置,c3p0配置


    映射文档:(在hibernate-core-5.0.7.Final.jar--org.hibernate--hibernate-configuration-3.0.dtd/hibernate-mapping-3.0.dtd可以移入约束声明)
        <?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="" table="">
                <id name="" column=""></id>
                <property name="" column="">
                <property name="" column="">
                <property name="" column="">
                <property name="" column="">
                <property name="" column="">
                <property name="" column="">
            </class>
        </hibernate-mapping>
        
    核心配置:
        <?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>
                <!-- 数据库连接信息的配置 -->
                <session-factory>
                         <!-- 类似于连接池  必填5个-->
                        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernates</property>
                        <property name="hibernate.connection.username">root</property>    
                        <property name="hibernate.connection.password">1234</property>    
                        <!-- 方言 : 让hibernate准备的匹配不同数据库的不同sql语句(例如:分页)  mysql limit  sqlserver top  分页    -->
                        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>    
                        
                        <!-- 可选的配置 -->
                        <!-- 在控制台显示hibernate生成的sql语句 -->    
                        <property name="hibernate.show_sql">true</property>    
                        <property name="hibernate.format_sql">true</property>    
                        
                        <!-- 让hibernate帮助我们是否生成对应表 -->
                        <!--
                            create: 每次都创建一个新表  :如果没有表创建新表,如果有表删除之后创建新表
                            create-drop: 每次都创建一个新表(如果没有表创建新表,如果有表删除之后创建新表),用完就删
                                                            用于测试
                                                            
                            update(工作中固定的):没有表创建表,有表使用表
                            
                            validate: 不会创建表,校验你现有的表
                         -->
                        <property name="hibernate.hbm2ddl.auto">update</property>
                        
                        <!-- 让hibernate使用c3p0连接池 -->
                        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
                        
                        
                        
                        <!-- 引入映射文件路径 -->    
                        <mapping resource="cn/itcast/domain/Customer.hbm.xml"/>
                </session-factory>
        </hibernate-configuration>
        
    使用:
        // 加载核心配置文件
        Configuration conf = new Configuration();
        conf.configure();
        // 创建sessionFactory --类似于连接池
        SessionFactory factory = conf.buildSessionFactory();
        // 生成session ---类似于数据库的连接(Connection)
        Session session = factory.openSession();
        // hibernate3.x和5.x的区别    hibernate3.x它的提交全部都必须得是手动提交
        // 开启事务
        //Transaction tx = session.beginTransaction();
        
        // hibernate的增操作
        Customer ct = new Customer();
        ct.setCust_name("jack");
        
         // hibernate的保存方法会有返回值,返回是对应的主键ID ctrl+2 L
        Serializable i = session.save(ct);
        System.out.println(i);
    
        // 手动提交
        //tx.commit();
        
        // 释放资源
        session.close();
        factory.close();
        
    c3p0配置:
        导包:hibernate-release-5.0.7.Final\lib\optional\c3p0
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

posted @ 2017-07-29 11:10  缺氧的鱼520  阅读(304)  评论(0编辑  收藏  举报