通用个数据库的类

闲来无事,自己整理了各个数据库连接,为了以后方便查看

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

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值