orm environment construction and basic demo

What is an ORM?

ORM:Object Relational Mapping

O: Object in the object-oriented field (JavaBean object)
R: Relational in the relational database field (table structure)
M: Mapping (XML configuration file)

Overview of the Hibernate Framework

       Hibernate is an open source object-relational mapping (ORM) framework, which encapsulates JDBC with very lightweight objects, allowing Java programmers to use object programming thinking to manipulate the database at will.
       Hibernate can be used in any occasion using JDBC, either in Java client programs or in Servlet/JSP web applications.
       Hibernate is a persistent layer solution for lightweight JavaEE applications and a relational database ORM framework.

Build the development environment of Hibernate

Step 1: Download the Hibernate5 runtime environment and
   download the corresponding jar package
Step 2: Create a table structure
Step 3: Build a Hibernate development environment
1. Create a WEB project and introduce the jar package required for Hibernate development as shown in the figure below:
      MySQL driver jar package The jar package       log jar package
      required for Hibernate development



The fourth step: write javaBean entity class
Customer类的代码如下:
    public class Customer {
        private Long cust_id;
        private String cust_name;
        private Long cust_user_id;
        private Long cust_create_id;
        private String cust_source;
        private String cust_industry;
        private String cust_level;
        private String cust_linkman;
        private String cust_phone;
        private String cust_mobile;
        // 省略get和set方法
    }

Step 5: Create the mapping between the class and the table structure
1. Create the mapping configuration file under the package where the JavaBean is located. The
      default naming rule is: entity class name.hbm.xml
      Introduce constraints in the xml configuration file
        <!DOCTYPE hibernate-mapping PUBLIC 
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
2. If you cannot access the Internet, there is no prompt for writing a configuration file , you need to configure it yourself
      first copy http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd --> window --> preferences --> search xml --> select xml catalog --> click add --> Now URI --> Paste the copied address --> Select location, select the path of the local DTD

3. Write the mapping configuration file


Step 6: Write the Hibernate core configuration file
1. In the src directory, create a configuration file named hibernate.cfg.xml
2. Introduce DTD constraints in XML
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
3. 4 parameters that must be configured                 
        #hibernate.connection.driver_class com.mysql.jdbc.Driver
        #hibernate.connection.url jdbc:mysql:///test
        #hibernate.connection.username gavin

        #hibernate.connection.password

The dialect of the database (must be configured)

        #hibernate.dialect org.hibernate.dialect.MySQLDialect

The specific configuration is as follows:


Step 7: Write Hibernate entry code
1. The specific code is as follows
    /**
     * Test save customer
     */
    @Test
    public void testSave(){
        // First load the configuration file
        Configuration config = new Configuration();
        // load src by default The configuration file in the directory
        config.configure();
        // Create SessionFactory object
        SessionFactory factory = config.buildSessionFactory();
        // Create session object
        Session session = factory.openSession();
        // Open transaction
        Transaction tr = session.beginTransaction( );
        // write save code
        Customer c = new Customer();
        // c.setCust_id(cust_id);   
        c.setCust_name("Test Name");
        c.setCust_mobile("110");
        // save client
        session.save(c);
        // commit transaction
        tr.commit();
        // release resources
        session.close();
        factory.close();

    }

Problems encountered in the construction of the orm environment and the implementation of the basic demo

       First of all, as a beginner, I am not particularly familiar with the orm framework itself, and I have no idea how to start the environment construction at the beginning. After consulting the information on the Internet and discussing with my classmates, I finally completed the construction of the environment step by step. There are also many problems in the process of building the environment. For example, the overall structure of the framework is not very clear. This problem was solved by reviewing the courseware and surfing the Internet. There is also the unfamiliarity with the code writing, which can only be done through repeated repetitions in the future. Practice to improve your code writing skills. Also, when I arrived at the jar package, I didn't know which packages I should get, and finally completed it with the help of my classmates.

Guess you like

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