zoukankan      html  css  js  c++  java
  • 5 -- Hibernate的基本用法 --4 1 创建Configuration对象

      org.hibernate.cfg.Configuration实例代表了应用程序到SQL数据库的配置信息,Configuration对象提供了一个buildSessionFactory()方法,该方法可以产生一个不可变的SessionFactory对象。

      另外,先实例化Configuration实例,然后在添加Hiberante持久化类。Configuration对象可调用addAnnotatedClass()方法逐个地添加持久化类,也可调用addPackage()方法添加指定包下的所有持久化类。

      创建Configuration对象的方式根据Hibernate配置文件不同而不同:

        ⊙ 使用hibernate.properties文件作为配置文件

        ⊙ 使用hibernate.cfg.xml文件作为配置文件

        ⊙ 不使用任何配置文件,以编码方式创建Configuration对象

      Configuration实例的唯一作用是创建SessionFactory实例,所以它被设计成启动期间对象,一旦SessionFactory创建完成,它就被丢弃了。

      1. 使用hibernate.properties作为配置文件

        在Hibernate发布包的projectetc路径下,提供了一个hibernate.properties文件,该文件爱你详细列出了Hibernate配置文件的所有属性。

        由于hibernate.properties作为配置文件时,没有提供添加Hibernate持久化类的方式,因此必须调用Configuration对象的addAnnotatedClass()或addPackage()方法,使用这些方法添加持久化类。

    Configuration configuration = new Configuration().addAnnotatedClass(Person.class).addAnnotatedClass(Student.class);

      2. 使用hibernate.cfg.xml作为配置文件

        使用hibernate.cfg.xml配置文件可以通过<mapping.../>子元素添加Hibernate持久化类,因此无须通过编程方式添加持久化类。

            // configure()方法将会负责加载hibernate.cfg.xml文件
            Configuration config = new Configuration().configure();

      3. 不使用配置文件创建Configuration实例

        Configuration对象提供如下方法,通过编程方式创建Configuration实例:

          ⊙ Configuration addAnnotatedClass(Class annotatedClass) : 用于为Configuration对象添加一个持久化类。

          ⊙ Configuration addPackage(String packageName) : 用于为Configuration对象添加指定包下的所有持久化类。

          ⊙ Configuration setProperties(Properties properties) : 用于为Configuration对象设置一系列属性,这一系列属性通过Properties实例传入。

          ⊙ Configuration setProperty(String propertyName,String value) : 用于为Configuration对象设置一个单独的属性。

        Class : NewsManager

    package hibernate.book._5_4_1._3;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    public class NewsManager {
        public static void main(String[] args) {
            Configuration conf = new Configuration().addAnnotatedClass(News.class)
                    .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
                    .setProperty("hibernate.connection.url", "jdbc:mysql:///hibernate")
                    .setProperty("hibernate.connection.username", "root")
                    .setProperty("hibernate.connection.password", "System").setProperty("hibernate.c3p0.max_size", "20")
                    .setProperty("hibernate.c3p0.min_size", "1").setProperty("hibernate.c3p0.timeout", "5000")
                    .setProperty("hibernate.c3p0.max_statements", "100")
                    .setProperty("hibernate.c3p0.idle_test_period", "3000")
                    .setProperty("hibernate.c3p0.acquire_increment", "2").setProperty("hibernate.c3p0.validate", "true")
                    .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect")
                    .setProperty("hibernate.hbm2ddl.auto", "update");
            
            SessionFactory sessionFactory = conf.buildSessionFactory();
            
            Session session = sessionFactory.openSession();
            
            Transaction tx = session.beginTransaction();
            
            News news = new News();
            news.setTitle("Angel");
            news.setContent("Spend all your time waiting for that second chance");
            
            session.save(news);
            
            tx.commit();
            
            session.close();
            
            sessionFactory.close();
        }
    }

        适合将部分关键的配置属性放在代码中添加。

    啦啦啦

  • 相关阅读:
    自动发送邮件功能
    工作中常用的Android系统ADB命令收集
    商城系统必须知道的【订单、优惠金额、退货、实际营收】解释
    商城系统项目必须知道的专业数据指标
    接口加密思路
    Selenium使用Chrome模拟手机浏览器方法解析
    PHP上传图片基本代码示例
    iframe子页面获取父页面的点击事件
    javascript实现网页倒计时效果
    Crontab常用命令总结
  • 原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6759554.html
Copyright © 2011-2022 走看看