[Study Notes] One of Hibernate Framework Learning

1. The correspondence between the three-layer structure and the three frameworks of JavaEE development:

 


  Struts2 framework -> presentation layer   web layer (MVC is the design model of presentation layer) business layer   service layer Hibernate framework -> persistence layer   dao layer Spring framework -> comprehensive class framework
          
  
  

 

    2. What are the persistence layer technologies: JDBC: The lowest-level way to operate the database Advantages     : The bottom layer, high     efficiency It is simple, and you still write SQL statements yourself, which has little impact on relative efficiency.     Disadvantages: The attribute name in the entity class and the field name of the database table must be consistent . There are        many things to memorize.   Common point: all require writing SQL statements by yourself
  


  



Problems encountered when using JDBC and DBUtils to manipulate product tables:
  JDBC: tedious when querying packaged results
  DBUtils: tedious when saving or updating 

 

Three, Hibernate overview

      Hibernate framework is one of the mainstream Java persistence layer frameworks. Because it is easy to learn, flexible and extensible, it can greatly simplify the code amount of the program and improve the work efficiency, so it is favored by the majority of developers.          

      Hibernate is an open-source ORM (Object Relational Mapping, Object Relational Mapping) framework, which encapsulates JDBC with lightweight objects, so that Java developers can use object-oriented programming ideas to operate the database. It is a lightweight, enterprise-level, open source persistence layer framework (usually, software engineering persistence layer solutions, one is the main and the other is auxiliary (write SQL statements (JDBC and DBUtils) and do not write SQL statements (Hibernate) ))), the frame (frame: a schema) that can operate the database.

Tips : ORM (Object Relational Mapping): Object Relational Mapping, establishing the corresponding relationship between entity classes and database tables, and realizing the operation class object is equivalent to operating the database.
       Operation mode: establish object relationship mapping, and realize the operation entity class is equivalent to operating the database table

Tips : Lightweight: few resources are used (currently, only log4j, c3p0 connection pool is used)
     Enterprise level: refers to more
     open source used in enterprise level applications: open source code

 

Compared with other technologies for operating databases, Hibernate has the following advantages :

    ● Hibernate encapsulates the JDBC access database code in a lightweight way, which greatly simplifies the cumbersome and repetitive code of the data access layer, reduces memory consumption, and speeds up the operation efficiency;
    ● Hibernate is a mainstream persistence framework based on JDBC. It is an excellent ORM implementation, which greatly simplifies the DAO (Data Access Object, data access object) layer coding work;
    ● Hibernate's performance is very good, and the mapping flexibility is excellent. It supports many relational databases, ranging from one-to-one to many-to-many complex relationships;
    ● Strong scalability, due to the open source code and API, when its own functions are not enough, it can be extended by coding.

4. CRM function module (Customer Relationship Management)

      The CRM system realizes the unified management of customer information and customer activities in various stages of enterprise sales, marketing, and service.

      The functions of the CRM system cover various business processes such as corporate sales, marketing, and user services. The activities related to customers in the business process will be managed in the CRM system. Some basic functional modules are listed below, including: customer information management, contact management, Business opportunity management, statistical analysis, etc., the CRM system table is as follows:

        

   1.客户信息管理:对客户信息统一维护,客户是指存量客户或拟营销的客户,通过员工录入形成公司的“客户库”,是公司最重要的数据资源。
   2.联系人管理:对客户的联系人信息统一管理,联系人是指客户企业的联系人,即企业的业务人员和客户的哪些人在打交道。
   3.客户拜访管理:业务员(用户)要开发客户需要去拜访客户,客户拜访信息记录了业务员与客户沟通交流方面的不足、采取的策略不当、有待改进的地方或值得分享的沟通技巧等方面的信息。
   4.综合查询:客户相关信息查询,包括:客户信息查询、联系人信息查询、商机信息查询等。
   5.统计分析:按分类统计客户信息,包括:客户信息来源统计、按行业统计客户、客户发展数量统计等。
   6.系统管理:系统管理属于crm系统基础功能模块,包括:数据字典、账户管理、角色管理、权限管理、操作日志管理等。
  

 

六、搭建Hibernate开发环境

 配置SessionFactory过程:
    第一部分:
      连接数据库的信息:
        #hibernate.connection.driver_class com.mysql.jdbc.Driver
        #hibernate.connection.url jdbc:mysql:///test
        #hibernate.connection.username gavin
        #hibernate.connection.password
        #hibernate.dialect org.hibernate.dialect.MySQLDialect
    第二部分:
      hibernate的可选配置:
        #hibernate.show_sql true
        #hibernate.format_sql true
        #hibernate.hbm2ddl.auto update
    第三部分:
      映射文件的位置

Tips:SQL结构化查询语言:一共分为6个部分
      DDL:Data Definition Language  数据定义语言(建库、建表、修改表结构)
      DML:Data Manipulation Language  数据库操作语言
      DQL:Data Query Language  数据库查询语言
      DCL:Data Control Language  数据控制语言(授权)
      CCL:Cursor Control Language  游标控制语言(游标操作)
      TPL:Transaction Processing Language  事务处理语言(开启、提交、回滚事务)

映射文件的配置:

 

Hibernate常用配置属性:

 

 

七、Hibernate入门案例:
   步骤分析:    * 1. 解析主配置文件
          * 2. 根据配置文件SessionFactory
          * 3. 根据SessionFactory创建Session
          * 4. 开启事务
          * 5. 执行操作(保存)
          * 6. 提交事务
          * 7. 释放资源

 

八、hibernate的常用对象:
  1.Configuartion:(熟练使用)

    常用方法:●  加载核心配置文件:在使用 Hibernate时,首先要创建 Configuration实例,Configuration实例主要用于启动、加载、管理 hibernate的配置文件信息。

            ●  加载映射文件:Hibernate除了可以使用 Configuration对象加载核心配置文件以外,还可以利用该对象加载映射文件。


  2.SessionFactory:(重要)(线程安全,创建单例对象)

    ● 它是线程安全的,它的同一个实例能够供多个线程共享。

    ● 它是重量级的,不能随意的创建和销毁。
    使用细节:
      * 连接数据库信息;
      * hibernate基本配置;
      * 映射文件的位置以及映射文件中的配置;
      * 一些预定义的SQL语句(比如全字段缓存,根据id全字段更新、根据id全字段查询、根据id全字段删除);
      * hibernate的二级缓存。
    使用原则:一个应用应该只有一个SessionFactory,在应用加载时创建,应用卸载时销毁。


  3.Session:(很重要)Session是应用程序与数据库之间交互操作的一个单线程对象,是hibernate运作的中心。
    使用原则:一个线程只能有一个Session对象。
    常用方法:

        ● save( ):保存一个实体到数据库;

        ● update( ):更新一个实体;

        ● delete( ):删除一个实体;

        ● get(Class class,Serializable id)和get(Class class,Serializable id):根据id查询一个实体;

        ● createQuery和createSQLQuery:用于数据库操作对象

        ● createCriteria():条件查询

        ● beginTransaction( ):开启事务,并返回事物对象。


  4.Transaction:(熟练使用)  

    常用方法:

        ● commit( ):提交相关联的session实例;

        ● rollback( ):撤销事务操作。

 

 九、Hibernate中查询一个实体的方法:(面试考点)

  ● get方法:get(Class class,Serializable id); 

  ● load方法:load(class class,Serializable id);(load方法默认情况下是延迟,可以通过配置的方式改为立即加载)    

    共同点:都是根据id查找一个实体


    区别:
    1.查询的时机不一样:
      1.)get方法的查询时机:每次调用get方法时,马上查询 立即加载;
      2.)  load方法的查询时机:每次真正使用的时候,发起查询 延迟加载(懒加载/惰性加载)。
    2.返回结果不一样:
      1.)  get方法返回的对象是实体类类型;
      2.)  load方法返回的对象是实体类类型的代理对象。

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324796097&siteId=291194637