IDEA how to create Hibernate

Front tips

1. In the configuration process, the software prompts missing anything, click Download on it, if download speed is too slow, Baidu "IDEA how to change the image address" to the address to download resources from abroad into a domestic (such as Ali cloud ), which will be faster.

2. To be honest, IDEA automatic download feature is not easy to use, often fail or download case "Quejinshaoliang", the proposed Final still go to the official website to download the Hibernate archive, select manually import in IDEA inside, find the download after extracting the file folder, click lib-> required, hold down the shift key to select all the jar packages.

Process

1. Create a new project
Here Insert Picture Description

2. Select Hibernate, remember to check the two options
Here Insert Picture Description
Here Insert Picture Description

Just a name, and then click Next

4. Automatic pop configuration database, the configuration starts.
Here Insert Picture Description

5. Start configuration database
Here Insert Picture Description

6. Enter your account number and password database ,, select the database you need, click test, after the test by clicking OK

Here Insert Picture Description
7. Back to the configuration interface, just select the database configuration
Here Insert Picture Description

8. Select the type stored in the database package
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
9. just needs to select a table, select OK

Here Insert Picture Description

10. configure the hibernate.cfg.xml
Here Insert Picture Description
11. Here you can see jdbc driver in question, so let's import jdbc jar package, if not, then you can go online to download.
Note that, depending on your version of MySQL download the corresponding jar package, if it is MySQL8.0, will jar with the 8.0 version of the package, or database connection is not on.
5.0
If not, you can download from My Drive: https://pan.baidu.com/s/1U3Xjh3OR1Ql7jdDLtpR9Kw
8.0
If not, you can download from My Drive:
https://pan.baidu.com/s/1dWZ84KvOz9V3whzY_kcUYg
Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description

When finished, click OK to.

12. The configuration is as follows, to choose their own username and password database account and password, the drive can also choose their own path name of the database (my account is root, password is oushile123, the database name is student)
Here Insert Picture Description

<?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>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/student</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">oushile123</property>
        <property name="show_sql">true</property>
        <mapping class="JDBC.Student11Entity"/>


        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

13. The preparation of an entity class: student.java, writing style using javaBean

Here Insert Picture Description
All fields in the table as a member of a private entity class, providing getter and setter methods, parameters, and containing no argument constructor.

Entity class finished, but the system did not recognize it, it still needs to be configured in the configuration file inside, this is the previously mentioned second profile, is responsible for mapping objects and relationships.

After writing to the configuration inside hibernate.cfg.xml
Here Insert Picture Description

<?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>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/student</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">oushile123</property>
        <property name="show_sql">true</property>
        <mapping class="JDBC.Student11Entity"/>
        <mapping resource="PO/student.hbm.xml"/>

        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

14. New Profile student.hbm.xml
Here Insert Picture Description
Here Insert Picture Description

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="PO.student" table="student11">   <!--类和表对应-->
        <id name="id" column="id" >       <!--主键-->
            <generator class="assigned"/>           <!--主键生成策略,assigned表示由用户赋值-->
        </id>
<!--        <property name="password" column="pwd"/>-->
    </class>
</hibernate-mapping>


Note: If there are other fields in addition to the primary key, add the property inside, because I am here only a field id, and is the primary key, so the property commented out.

15. start the test, create a new folder test, wait for the next test result is ready to accept Junit
Here Insert Picture Description

16. A new category, here as an example to insert
Here Insert Picture Description

17. Download Junit test

Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description

After the installation is complete restart the IDE, back to the test class

18. Configure Junit4

Here Insert Picture Description
Here Insert Picture Description
We modify the pre-defined test folder, so Junit test class will automatically generate the test folder inside, it is not very convenient.

19. began testing
the shortcut Alt + Insert, the option to automatically generate test files will appear:

Here Insert Picture Description

Here Insert Picture Description

And then automatically generate the test folder inside the test class
Here Insert Picture Description

20. The writing test code

package  test;

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import PO.student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;


/** 
* insert Tester. 
* 
* @author <Authors name> 
* @since <pre>11月 3, 2019</pre> 
* @version 1.0 
*/ 
public class insertTest {
    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

@Before
public void before() throws Exception {
    final StandardServiceRegistry registry=new StandardServiceRegistryBuilder().configure().build();
    try {
        sessionFactory=new MetadataSources(registry).buildMetadata().buildSessionFactory();
        session=sessionFactory.openSession();
        transaction=session.beginTransaction();
    }catch(Exception e){
        StandardServiceRegistryBuilder.destroy(registry);
    }
}
@Test
public void testInsert(){
    student st=new student(2);
    session.save(st);
    }

@After
public void after() throws Exception {
    transaction.commit();
    session.close();
    sessionFactory.close();
} 


} 

start operation

Here Insert Picture Description

Here Insert Picture Description

Successfully inserted

Here Insert Picture Description

Published 100 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43576028/article/details/102879283