ORM environment build in eclipse

1. First, we need to introduce the jar package we want to use

hibernate3.jar core + required (6) + jpa directory + database driver package

As shown below:


I encountered some problems when introducing the jar package. At first, it was installed in the newly created lib folder under src, but it was found that it could not be used, and a new one had to be created.

User Library, add the jar package you need, then right-click on the project Build path---add library, and import the previously self-built library.

2. In order to establish a mapping relationship with the database table, write an object User.java

package sunrui;
public class User {
	private int id;
    private String username;
    private String password;
    private String cellphone;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getCellphone() {
		return cellphone;
	}
	public void setCellphone(String cellphone) {
		this.cellphone = cellphone;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

3. Write Object Mapping -> User.hbm.xml.

This part is the writing code I found on the Internet. I didn't see it in project>etc. Only the .cfg.xml file was found in it.

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping package="sunrui">

    <!--The class name is User, and the table name is User-->
    <class name="User"  table="m">

        <!--Primary key mapping, the attribute name is id, and the column name is also id-->
        <id name="id" column="id">
            <!--Automatic growth based on the underlying database primary key-->
            <generator class="native"/>

        </id>
        
        <!--Non-primary key mapping, one-to-one correspondence between attributes and column names -->
        <property name="username" column="username"/>
        <property name="cellphone" column="cellphone"/>
        <property name="password" column="password"/>
    </class>
</hibernate-mapping>

Note the class name and table. One is the class name written by yourself, and the other is the name of the table you want to create in the database. The name of the table can be written casually, but it must correspond to the code behind. If you write the wrong class name, the file will not be found.

There is no difficulty in this part. The problem is that there is no corresponding class name. In addition, id is the primary key column. The generator specifies the generation method according to the underlying database. If the primary key does not increase automatically, an error will be reported.

4. Configure the Hibernate core configuration file (hibernate.cfg.xml)

This needs to be created in the src directory, be careful not to write it in your own package, the hbm.xml file is written in your own package

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
        <hibernate-configuration>
    <session-factory>

        <!-- Database connection configuration -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/abc</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">sunrui1314</property>
        <!-- Database method configuration, when hibernate is running, it will generate sql that conforms to the current database syntax according to different dialects -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>


        <!-- Other related configuration-->
        <!-- Display the sql statement that hibernate executes at runtime -->
        <property name="hibernate.show_sql">true</property>
        <!-- format sql -->
        <property name="hibernate.format_sql">true</property>
        <!-- create table-->
        <property name="hibernate.hbm2ddl.auto">create</property>

        <!--Load all mappings-->
        <mapping resource="sunrui/User.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

In the hiberate file directory we downloaded, he originally looked like this


It should be noted in this part that

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>//This is the database driver
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/abc</property>//This is the database connection, your connection name and the built database name, we are here abc
        <property name="hibernate.connection.username">root</property>//This is the database username
        <property name="hibernate.connection.password">sunrui1314</property>//Do not write wrong password here

When loading all the mappings, the mapping resource should be followed by the package name you built + the cfg.xml file name. If it is written incorrectly, the file will not be found and the corresponding directory will not be successfully executed.

5. You're done, start the test, create the test class sr.java

package sunrui;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Sr {
	public static void main(String[] args) {

        //create object
        User m = new User();
        m.setPassword("666");
        m.setCellphone("18846420216");
        m.setUsername("ifyou");

        //Get loading configuration management class
        Configuration configuration = new Configuration();

        //The hibernate.cfg.xml file is loaded by default without parameters,
        configuration.configure();

        //Create a Session factory object
        SessionFactory factory = configuration.buildSessionFactory();

        //Get the Session object
        Session session = factory.openSession();

        / / Use Hibernate to operate the database, you must open the transaction, get the transaction object
        Transaction transaction = session.getTransaction();

        // start the transaction
        transaction.begin();

        // add the object to the database
        session.save(m);

        // commit the transaction
        transaction.commit();

        //Close Session
        session.close();
    }
}
The main problem here is that the class name is not wrong! The class name is the User that establishes the mapping relationship with the database, and don’t forget to add the corresponding import. Generally speaking, there will be a prompt after the code is typed. Select the relative import package to complete. Finally, don’t forget to close the session. If there is no explicit call to the commit() method, the cache will not be cleared, the corresponding SQL will be executed, and the transaction will not be committed.

6. Result display




You can see that the m table has been created in the database

Guess you like

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