闲来无事,自己整理了各个数据库连接,为了以后方便查看
1.工具类
package com.util.db;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class NewHibernateUtil {
private static SessionFactory msSessionFactory;
private static SessionFactory pgSessionFactory;
private static SessionFactory oracleSessionFactory;
private static SessionFactory mysqlSessionFactory;
@SuppressWarnings("deprecation")
public static SessionFactory getSessionFactory(String type) {
if (SqlType.MS.equals(type)) {
if (msSessionFactory==null) {
msSessionFactory = new AnnotationConfiguration().configure("hibernate.ms.cfg.xml").buildSessionFactory();
}
return msSessionFactory;
} else if (SqlType.PG.equals(type)) {
if (pgSessionFactory==null) {
pgSessionFactory = new AnnotationConfiguration().configure("hibernate.pg.cfg.xml").buildSessionFactory();
}
return pgSessionFactory;
} else if (SqlType.ORACLE.equals(type)) {
if (oracleSessionFactory==null) {
oracleSessionFactory = new AnnotationConfiguration().configure("hibernate.oracle.cfg.xml").buildSessionFactory();
}
return oracleSessionFactory;
} else if (SqlType.MYSQL.equals(type)) {
if (mysqlSessionFactory==null) {
mysqlSessionFactory = new AnnotationConfiguration().configure("hibernate.mysql.cfg.xml").buildSessionFactory();
}
return mysqlSessionFactory;
}
return null;
}
}
2.枚举类
package com.util.db;
public enum SqlType {
MS,PG,ORACLE,MYSQL
}
3.配置文件
hibernate.ms.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://10.0.1.80\dbo:1433;databaseName=thams</property>
<property name="hibernate.connection.username">thams</property>
<property name="hibernate.connection.password">ams2000</property>
</session-factory>
</hibernate-configuration>
hibernate.mysql.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/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://10.0.4.44:3306/mysql</property>
<property name="hibernate.connection.username">dev</property>
<property name="hibernate.connection.password">dev</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
hibernate.oracle.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@10.0.13.124:1521:oracle</property>
<property name="hibernate.connection.username">dev</property>
<property name="hibernate.connection.password">dev</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
hibernate.pg.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://10.0.13.187:5432/ams</property>
<property name="hibernate.connection.username">dev</property>
<property name="hibernate.connection.password">dev</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
调用的时候记得导入相对应的jar