方式1、经过hibernate.cfg.xml文件配置
1. hibernate.cfg.xml
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
test
123
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@127.0.0.1:1521:tran
true
true
true
org.hibernate.dialect.OracleDialect
GBK
5
20
1800
50
update
auto
false
false
2. 应用代码
package com.lxh.transaction3;
import java.io.File;
import org.hibernate.cfg.Configuration;
public class HibernateTransactionTest {
// path
private static String path = HibernateTransactionTest.class.getResource("")
.getPath().toString();
// hibernate.cfg.xml文件方式获取
public static Configuration getConfigurationByXML() {
// 加载配置文件
Configuration config = new Configuration();
Configuration cfg = config.configure(new File(path
+ "hibernate.cfg.xml"));
return cfg;
}
public static void main(String[] args) {
// Configuration
Configuration cfg = HibernateTransactionTest.getConfigurationByXML();
System.out.println("****cfg="+cfg+"****\n****SessionFactory="+cfg.buildSessionFactory()+"****");
}
} 3. 测试结果
方式2、经过hibernate.properties文件配置
1. hibernate.properties
hibernate.connection.username=test
hibernate.connection.password=123
hibernate.connection.url=jdbc:oracle:thin:@127.0.0.1:1521:tran
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.connection.characterEncodin=GBK
hibernate.connection.release_mode=auto
hibernate.transaction.auto_close_session=false
hibernate.connection.autocommit=false
c3p0.min_size=5
c3p0.max_size=20
c3p0.timeout=1800
c3p0.max_statements=50 2. 应用代码
package com.lxh.transaction3;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.hibernate.cfg.Configuration;
public class HibernateTransactionTest {
// path
private static String path = HibernateTransactionTest.class.getResource("")
.getPath().toString();
// properties文件方式获取
public static Configuration getConfigurationByProp() {
// hibernate.properties
Properties prop = new Properties();
try {
prop.load(new FileReader(new File(path + "hibernate.properties")));
} catch (IOException e) {
System.out.println("加载hibernate配置文件失败:\t" + e.getMessage());
}
// 加载配置文件
Configuration config = new Configuration();
Configuration cfg = config.setProperties(prop);
cfg.addClass(BankAccount.class);// 很是重要----自动搜索BankAccount.hbm.xml
return cfg;
}
public static void main(String[] args) {
// Configuration
Configuration cfg = HibernateTransactionTest.getConfigurationByProp();
System.out.println("****cfg="+cfg+"****\n****SessionFactory="+cfg.buildSessionFactory()+"****");
}
} 3. 测试结果
补充:BankAccount.hbm.xml
/p>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
说明;
1. 须要引入数据库驱动包以及hibernate相关jar;
2. 使用properties文件配置hibernate时,应该采用cfg.addClass(BankAccount.class);自动加载XXX.hbm.xm文件;
3. XXX.hbm.xml文件配置时特别注意正确书写表名和对应POJO的路径。
参考:
1. hibernate使用Properties文件方式配置 http://blog.csdn.net/xiazdong/article/details/7562765
2. hibernate.cfg.xml配置 http://www.blogjava.net/baoyaer/articles/172642.html
3. hibernate.cfg.xml在hibernate与spring中配置的区别 http://kewen1989.iteye.com/blog/1747156
4. hibernate配置文件中有关事务的配置说明 http://www.blogjava.net/freeman1984/archive/2011/08/04/355808.html
5. hibernate配置参数解释 http://www.cnblogs.com/elleniou/archive/2012/12/01/2797546.html
6. hibernate展现SQL语句 http://www.mkyong.com/hibernate/hibernate-display-generated-sql-to-console-show_sql-format_sql-and-use_sql_comments/
遇到的问题:
hibernate能显示执行的SQL语句,可是不能执行格式化。原先配置以下图所示:
解决方法:去掉空格便可。