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 ...
随机推荐
- po line received is canceled(恢复PO被取消的余量)
1張PO已部分收貨,後來由于某種原因,將部分收貨的PO明行取消,現在要對已收料的這一部分進行退貨處理,要怎麼做才好呢? [@more@]DATA COLLECTED===============COL ...
- hql语句查询实体类采用list方法的返回结果集
在hibernate中,用hql语句查询实体类,采用list方法的返回结果为一个List,该List中封装的对象分为以下三种情况:1.查询全部字段的情况下,如"from 实体类", ...
- vscode icon in elementary os
chmod +x /home/shenfeng/vscode/code /home/shenfeng/vscode/code sudo ln -s /home/shenfeng/vscode/code ...
- 查看linux僵尸进程
top ps -A -o stat,ppid,pid,cmd | grep -e '^[zZ]' kill -HUP 进程号
- python 提取图片转为16 24BPP 的方法
python 中处理图片用的是 pil ,在 linux 和 win 上都可以使用. centOS 5.x 上安装的方法是 yum install python-imaging 24BPP: imp ...
- oracle日期函数2!
1.日期时间间隔操作 当前时间减去7分钟的时间 select sysdate,sysdate - interval '7' MINUTE from dual 当前时间减去7小时的时间 ...
- Can't create/write to file '/tmp/#sql_887d_0.MYD' (Errcode: 17)
lsof |grep "#sql_887d_0.MYD" 如果没有被占用就可以删掉 . https://wordpress.org/support/topic/cant-creat ...
- apache支持中文域名绑定,apache支持中文域名绑定,教你怎样让apache支持中文域名绑定
摘要:apache支持中文域名绑定,apache支持中文域名绑定,教你怎样让apache支持中文域名绑定,根据本人实际经验,叫你如何让apache支持中文域名绑定,绝对管用的让apache支持中文域名 ...
- PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...
- Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58072 Accepted: 18061 ...