hibernate动态读取配置及创建数据源
来源:互联网 发布:高中编程 编辑:程序博客网 时间:2024/05/01 09:02
public static Configuration initDBConfig(String dialect, String driver, String url, String user, String pwd){Configuration cfg = new Configuration().configure("/hibernate.cfg.xml");cfg.setProperty("hibernate.dialect", dialect);cfg.setProperty("hibernate.connection.driver_class", driver);cfg.setProperty("hibernate.connection.url", url);cfg.setProperty("hibernate.connection.username", user);cfg.setProperty("hibernate.connection.password", pwd);cfg.setProperty("hibernate.c3p0.max_size", DBConstant.getDBConf().getPoolMaxSize());cfg.setProperty("hibernate.c3p0.min_size", DBConstant.getDBConf().getPoolMinSize());cfg.setProperty("hibernate.c3p0.max_statements", DBConstant.getDBConf().getMaxStatements());return cfg;}
通过上面的函数,数据库的配置信息就无需使用hibernate.cfg.xml写死的配置,可以从其他的渠道进行读取,而无需改变的信息则可以默认使用hibernate.cfg.xml的配置。
public static SessionFactory initSessionFactory(Configuration cfg) throws SmbDaoException{try{StandardServiceRegistryBuilder ssrbuilder = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties());ServiceRegistry service = ssrbuilder.build();SessionFactory sessionFactory=cfg.buildSessionFactory(service);LOG.info("init hibernate success");return sessionFactory;}catch(Exception e){throw new SmbDaoException(e.getMessage(), e); }}第二个函数接收第一个函数的返回作为参数,这样就可以根据不同的配置,创建不同的数据源了。
阅读全文
0 0
- hibernate动态读取配置及创建数据源
- spring + hibernate动态数据源配置
- spring + hibernate动态数据源配置
- hibernate动态数据源配置(注解)
- Hibernate配置及创建
- Tomcat6配置C3P0数据源及Hibernate整合
- EXTJS4.2数据源配置及创建注意事项
- hibernate 动态切换数据源
- Hibernate动态数据源
- 跨db的动态数据源的transaction- 1动态读取数据源取代配置文件中一一配置
- hibernate + DB2 数据源配置及对象映射配置
- ODBC动态创建数据源
- hibernate配置两个数据源
- hibernate 数据源配置
- Hibernate配置JNDI数据源
- hibernate配置数据源
- hibernate数据源配置
- hibernate配置jndi数据源
- 完全卸载oracle 11g
- 数据库数据读取并且生成excel
- 计算一个整数二进制位中1的个数
- js基础-数据类型
- 分享springcould大神视频
- hibernate动态读取配置及创建数据源
- SpannableStringBuilder的使用
- Android 信号处理面面观 之 信号定义、行为和来源
- 第三周项目4(2)-顺序表应用 将所有奇数移到所有偶数前面
- html之css、重要标签
- Spring学习笔记 Spring项目介绍
- 查看tomcat线上部署项目实时日志的方法
- PHP 常用的header头部定义
- 统计销量 Counter