美文网首页
Hibernate系列之(0)初始化配置

Hibernate系列之(0)初始化配置

作者: Ethan_Walker | 来源:发表于2017-08-21 21:56 被阅读7次
    1. 官方下载 http://hibernate.org/orm/ 相关jar 包

    2. 解压之后 将 lib/ required / 下面的所有jar 包复制到 工程的 /WEB-INF/lib 下(确保lib已被设置成类库 library files),导入数据库驱动包 mysql-connection.jar

    3. 创建数据库summer, 表可以暂时不用创建, hibernate 能够自动生成

    4. 创建与表对应的 POJO 类User

      public class User {
          private Integer id;
          private String username;
          private String password;
        ...getter/setter 方法
      }
      
    5. 在User.java 同一个包下创建映射文件 User.hbm.xml, 注意:名字的前缀和类名相同
      首先添加dtd 头,不然没有提示:打开 hibernate-core.jar 下 org.hibernate/hibernate-mapping-3.0.dtd

    image.png

    复制阴影部分到 User.hbm.xml上,然后开始配置

    1. User.hbm.xml

      <?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.example.domain.User" table="t_user">
                  <id name="id" column="id">  
                      <generator class="native" />
                  </id>
                  <property name="username" column="username" type="string"/>
                  <property name="password" column="password" />
            </class>
        </hibernate-mapping>  
      
        <!-- class标签:用于映射类与表的关系 name :类的全路径  table:表名称 -->
        <!-- 使用id的标签 配置唯一属性 -->
        <!-- 在<id>标签中配置一个主键的生成策略. -->
        <!-- property标签:映射类中的普通属性 name:类中的属性名称, column:表中字段名称 -->
        <!-- 
              type:三种写法
                  * Java类型        :java.lang.String
                  * Hibernate类型   :string
                  * SQL类型     :不能直接使用type属性,需要子标签<column>
                      <property name=”name”>
                          * <column name="name" sql-type="varchar(20)"/>
                      </property>
           -->
      
      
    2. 配置核心配置文件
      在src下创建 hibernate.cfg.xml, 打开 hibernate-core.jar 下 org.hibernate/hibernate-configuration-3.0.dtd

    image.png

    复制阴影部分到 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-configuration>
        <session-factory>
            <!-- 必须去配置的属性 -->
           <!-- 配置数据库连接的基本信息: -->
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql:///summer</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property>
    
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>      <!-- Hibernate的方言 ,生成底层SQL不同的 -->
    
            <!--可选属性-->
            <property name="hibernate.format_sql">true</property>   <!-- 显示SQL -->
            <property name="hibernate.show_sql">true</property> <!-- 格式化SQL -->
            <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- hbm:映射 to DDL: create drop alter, 不需要创建表,在加载配置hbm文件的时候,自动创建表 -->
    
            <!-- 通知Hibernate加载那些映射文件 -->
          <mapping resource="cn/itcast/hibernate3/demo1/Customer.hbm.xml" />
    
        </session-factory>
    </hibernate-configuration>
    
    
    1. 编写测试:
    @Test
        // 向数据库中插入一条记录
        public void demo1(){
            // 1.Hiberante框架加载核心配置文件(有数据库连接信息)
            Configuration configuration = new Configuration().configure();
            // 2.创建一个SessionFactory.(获得Session--相当连接对象)
            SessionFactory sessionFactory = configuration.buildSessionFactory();
            // 3.获得Session对象.
            Session session = sessionFactory.openSession();
            // 4.默认的情况下,事务是不自动提交.
            Transaction tx = session.beginTransaction();
            // 5.业务逻辑操作
            
            // 向数据库中插入一条记录:
            Customer customer = new Customer();
            customer.setName("任童");
            customer.setAge(28);
            
            session.save(customer);
            
            // 6.事务提交
            tx.commit();
            // 7.释放资源
            session.close();
            sessionFactory.close();
        }
    

    扩展: 用C3P0替代Hibernate 内置数据库连接池

    1. 在下载解压后的jar 包中找到\hibernate-release-5.2.10.Final\lib\optional\c3p0,复制三个jar包到 web\WEB-INF\lib下

    2. 在核心配置文件中添加配置
      注意: property 标签必须在 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>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql:///summer</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property>
    
            <property name="hibernate.current_session_context_class">thread</property>
    
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
            <property name="hibernate.format_sql">true</property>
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.hbm2ddl.auto">update</property>
            <!--在连接池中可用的数据库连接的最少数目 -->
            <property name="hibernate.c3p0.min_size">5</property>
            <!--在连接池中所有数据库连接的最大数目  -->
            <property name=" hibernate.c3p0.max_size">20</property>
            <!--设定数据库连接的过期时间,以秒为单位,
            如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
            <property name=" hibernate.c3p0.timeout">120</property>
            <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
            <property name=" hibernate.c3p0.idle_test_period">3000</property>
            
            <mapping resource="com/example/domain/User.hbm.xml" />
    
        </session-factory>
    </hibernate-configuration>
    
    
          
    

    相关文章

      网友评论

          本文标题:Hibernate系列之(0)初始化配置

          本文链接:https://www.haomeiwen.com/subject/tcnhdxtx.html