用hibernate.properties代替hibernate.cfg.xml配置常用的屬性


      我們使用hibernate時經常在hibernate.cfg.xml文件中配置數據庫連接的相關屬性,是否顯示sql語句,數據庫的方言等,這些配置其實也可以在.properties文件中配置。現在我把這把文件的名字起為:hibernate.properties.

思路:寫一個domian對象,以及這個domain對象映射到數據庫中的.hbm.xml文件。和一個測試類(這個測試類是更新數據庫中的一條數據)

以及hibernate.properties文件(這個文件是放在src的下面即在classPath的根目錄下)

一:domain Person的代碼是:

package com.qls.domain;

import java.util.Date;

/**
 * Created by 秦林森 on 2017/5/21.
 */
public class Person {
    private Integer id;
    private String  name;
    private Date enterCampusDate;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getEnterCampusDate() {
        return enterCampusDate;
    }

    public void setEnterCampusDate(Date enterCampusDate) {
        this.enterCampusDate = enterCampusDate;
    }
}

二:Person.hbm.xml文件的代碼如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.qls.domain">
    <class name="Person" table="person">
<id name="id" column="person_id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="enterCampusDate" type="timestamp"/>
        </class>
        </hibernate-mapping>

 

三:hibernate.properties文件的代碼如下:

#the row end do not add semicolon;
hibernate.show_sql=true
hibernate.connection.driver_class=oracle.jdbc.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:orcl
hibernate.connection.username=scott
hibernate.connection.password=a123456
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.connection.pool_size=10

四:測試類

package com.qls.test;

import com.qls.domain.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.ImprovedNamingStrategy;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by ${秦林森} on 2017/5/22.
 */
public class Test12 {
    public static void main(String[] args) {
        Configuration configuration = new Configuration().addResource("/com/qls/domain/Person.hbm.xml");
       InputStream in = Test12.class.getClassLoader().getResourceAsStream("hibernate.properties"); Properties properties = new Properties(); try { properties.load(in); } catch (IOException e) { e.printStackTrace(); } configuration.setProperties(properties);
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();//一次會話
        Transaction tx = session.beginTransaction();//開啟事務
        Person person=session.get(Person.class,24);
        person.setName("人民萬歲");
        session.update(person);
        tx.commit();//提交事務。
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM