Configuration对象
Configuration对象
Hibernate的持久化操作离不开SessionFactory对象,使用该对象的openSession()方法可以打开Session对象。而SessionFactory对象就是由Configuration对象创建的。
Configuration实例代表了应用程序到SQL数据库的配置信息。Configuration对象提供了一个buildSessionFactory()方法,使用该方法可以产生一个不可变的SessionFactory对象。也可以先实例化Configuration对象,然后在添加Hibernate的持久化类(使用addAnnotatedClass()方法逐个添加持久化类,使用addPackage()方法添加指定包下的所有持久化类)。
Configuration实例唯一的作用是创建SessionFactory实例,一旦SessionFactory实例被创建后,此Configuration对象便被丢弃。
创建Configuration对象的方法:
1.使用hibernate.properties作为配置文件
必须通过调用Configuration对象的addAnnotatedClass()方法手动添加持久化类,这是特别麻烦的,所以在开发中不使用这种方式创建Configuration对象。
//实例化Configuration
Configuration cfg = new Configuration()
//多次调用addAnnotatedClass(Xxx.class)
.addAnnotatedClass(Xxx.class)
.addAnnotatedClass(Zzz.class);
2.使用hibernate.cfg.xml作为配置文件(常用)
因为可以在hibernate.cfg.xml中添加Hibernate的持久化类,所以在使用hibernate.cfg.xml作为配置文件创建Configuration对象时可以直接使用下面的代码:
Configuration config = new Configuration().configure();
注意configure()方法十分重要,该方法负责加载hibernate.cfg.xml配置文件。
//实例化Configuration
Configuration cfg = new Configuration()
//configure()方法将会负责加载hibernate.cfg.xml文件
.configure();
3.通过编程的方式创建Configuration实例
在Configuration对象中有以下几个方法:
Configuration addAnnotatedClass(Class annotatedClass):
为Configuration对象添加一个持久化类。
Configuration addPackage(String packageName):
为Configuration对象添加指定包下的持久化类。
Configuration cfg = new Configuration()
//设置连接属性
.addAnnotatedClass(Users.class)
//设置数据库方言
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
//设置数据库驱动
.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
//设置数据库
.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test")
//设置数据库用户名
.setProperty("hibernate.connection.username", "root")
//设置数据库密码
.setProperty("hibernate.connection.password", "stx12345")
//C3P0数据源相关配置================================================================
.setProperty("hibernate.c3p0.max_size", "200")
.setProperty("hibernate.c3p0.min_size", "1")
.setProperty("hibernate.c3p0.timeout", "5000")
.setProperty("hibernate.c3p0.max_statements", "200")
.setProperty("hibernate.c3p0.idle_test_period", "3000")
.setProperty("hibernate.c3p0.acquire_increment", "2")
.setProperty("hibernate.c3p0.validate", "false");
附Hibernate连接MySQL常用配置:
## MySQL #MySQL --- org.hibernate.dialect.MySQLDialect #MySQL with InnoDB --- org.hibernate.dialect.MySQLInnoDBDialect #MySQL with MyISAM --- org.hibernate.dialect.MySQLDialect #配置数据库方言 #hibernate.dialect org.hibernate.dialect.MySQLDialect #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect #配置数据库驱动 #hibernate.connection.driver_class com.mysql.jdbc.Driver #配置数据库URL #hibernate.connection.url jdbc:mysql:///test #配置数据库用户名 #hibernate.connection.username gavin #配置数据库密码 #hibernate.connection.password ########################### ### C3P0 Connection Pool### ########################### #连接池最大连接数 #hibernate.c3p0.max_size 2 #连接池最小连接数 #hibernate.c3p0.min_size 2 #连接超时 #hibernate.c3p0.timeout 5000 #最多可创建的Statement对象数 #hibernate.c3p0.max_statements 100 #检测有多少连接超时的周期 #hibernate.c3p0.idle_test_period 3000 #当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 #hibernate.c3p0.acquire_increment 2 #每次都验证连接是否可用 #hibernate.c3p0.validate false
Configuration对象的更多相关文章
- 无法为请求的 Configuration 对象创建配置文件 错误原因
Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 无法为请求的 Configura ...
- mybatis源码解读(二)——构建Configuration对象
Configuration 对象保存了所有mybatis的配置信息,主要包括: ①. mybatis-configuration.xml 基础配置文件 ②. mapper.xml 映射器配置文件 1. ...
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...
- 5 -- Hibernate的基本用法 --4 1 创建Configuration对象
org.hibernate.cfg.Configuration实例代表了应用程序到SQL数据库的配置信息,Configuration对象提供了一个buildSessionFactory()方法,该方法 ...
- Mybatis 创建Configuration对象
Mybatis 创建Configuration对象是在项目启动时就创建了. 具体创建流程为: https://blog.csdn.net/wolfcode_cn/article/details/80 ...
- Mybatis源码解析,一步一步从浅入深(四):将configuration.xml的解析到Configuration对象实例
在Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码中我们看到了XMLConfigBuilder(xml配置解析器)的实例化.而且这个实例化过程在文章:Mybatis源码解析,一步一步从浅 ...
- .net core Configuration对象
前因:最近在阅读.net core源码,发现关于Configuration介绍的文档都比较多,但是都比较杂乱,(微软文档太官方),所以写下一些自己的感想 主要通过三种使用情况来介绍 Web应用程序使用 ...
- Hibernate的Configuration对象的configure()方法
Configuration configuration=new Configuration(); configuration.configure(); 在Hibernate底层实现configure( ...
- hibernate之Configuration对象
任务:读取主配置信息 1. Configuration config = new Configuration(); 使用hibernate,但并没有读取 2. config.config ...
随机推荐
- ligerui_实际项目_001:利用ligerLayout、ligerAccordion实现可折叠的菜单效果
效果:利用ligerLayout.ligerAccordion实现可折叠的菜单效果 可能用到的js.css.images等,可到官网下载: 第01步:引入相应的文件 <head><l ...
- 包括post,get请求(http,https)的HttpClientUtils
package cn.knet.data.untils; import java.io.IOException; import java.net.SocketTimeoutException; imp ...
- Oracle存储过程总结
1.存储过程结构 1.1 第一个存储过程 create or replace procedure proc1( para1 varchar2, para2 out varchar2, para3 in ...
- smarty简单介绍
smarty简单介绍 示意图如下 简单介绍smarty.class.php类的大体内容,如下: <?php class Smarty //此类就是libs中的Smarty.class.php类 ...
- 手机端js模拟长按事件(代码仿照jQuery)
代码编写: $.fn.longPress = function(fn) { var timeout = undefined; var $this = this; for(var i = 0;i< ...
- [OrangePi] Building the system
You can try to build Debian/Ubuntu for OrangePI yourself. Clone my GitHub repository. You will need ...
- centos 7 systemctl
Linux Systemctl是一个系统管理守护进程.工具和库的集合,用于取代System V.service和chkconfig命令,初始进程主要负责控制systemd系统和服务管理器.通过Syst ...
- struct termios结构体【转】
本文转载自:http://blog.csdn.net/vevenlcf/article/details/51096122 一.数据成员 termios 函数族提供了一个常规的终端接口,用于控制非同步通 ...
- javaWeb 在jsp中 使用自定义标签输出访问者IP
1.java类,使用简单标签,jsp2.0规范, 继承 SimpleTagSupport public class ViewIpSimpleTag extends SimpleTagSupport { ...
- 快速搭建Redis缓存数据库
之前一篇随笔——Redis安装及主从配置已经详细的介绍过Redis的安装于配置.本文要讲的是如何在已经安装过Redis的机器上快速的创建出一个新的Redis缓存数据库. 一.环境介绍 1) Linux ...