Configuration对象

时间:2023-03-09 21:50:49
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