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 ...
随机推荐
- .NET 4.0 MemoryCache with SqlChangeMonitor
Summary There isn't a lot of documentation on the internet about how to use the SqlChangeMonitor wit ...
- 13---Net基础加强
更新中,敬请期待............ 作业-复习 程序集1 程序集2 反射1 反射2 反射3 反射4-记事本插件1 反射5-记事本插件2
- ThinkPHP讲解(八)——显示、修改、添加、删除
一.显示数据 <h1>主页面</h1> <table width="100%" border="1" cellpadding=&q ...
- 我使用的vim配置文件
各种搜,拼凑出了这么一个配置文件,以下是文件的内容 syntax onfiletype onset linespace=0set rulerset nocompatibleset confirmset ...
- C++之: CDib类
头文件Cdib.h 源文件Cdib.cpp
- android显示当前时间
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss "); Date c ...
- struts2 笔记03 异常支持、防止页面刷新和后退、方法验证
Struts2对异常支持(声明式异常.自动的异常处理), 异常处理(运行期异常事务自动回滚) 1. 自定义异常类,继承RuntimeException或Exception实现构造方法. 2. 配置异常 ...
- iOS 开发 证书总结 开发证书和生产证书的区别
IOS开发 证书总结 开发者证书 ------>> 开发证书是你在真机推送时 用得, 生产证书是app 上架之后 推送给用户用的 首先你必须获得apple开发者证书,上图这个文件就是 ...
- 27、oracle(三)
1)掌握增.删.改数据和事务操作 2)掌握[视图]和同义词 3)掌握[序列]和索引 4)了解有关用户和权限的控制 ------------------------------------------- ...
- HDU 3966:Aragorn's Story(树链剖分)
http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:有n个点n-1条边,每个点有一个权值,有两种操作:询问一个点上权值是多少和修改u到v这条链上的权值. ...