Hibernate in the Intelij

What is hibernate

First look at Baidu Encyclopedia description of the hibernate:

Hibernate is an object-relational mapping framework for open source, it had a very lightweight JDBC object package so that the programmer using the object can be arbitrary Java programming thinking to manipulate the database. Hibernate can be used in any occasion using JDBC, Java can be used in both client program can also be used in Servlet / JSP Web application, the most revolutionary is, Hibernate can replace EJB CMP in the application of the J2EE architecture to complete the task of data persistence.

This project uses hibernate Andrews also used to provide end-side and web database access, so step by step to get started, start with the first example start.


hibernate example

Before the first example to explain the compiler I use for Intellij IDEA, which automatically integrates hibernate and junit jar package, using the eclipse of little friends to install the tools on their own.

The first step: create a new project

This time to select the web application, Hibernage and automatically create profiles of these three options, the next step, the next step ...... connection to the database package import the jar itself. .

Write pictures described here


Step two: Modify the hibernate configuration file

Project to create the configuration file is automatically created after the completion of the following, we want to modify the configuration files.

Write pictures described here

Different database configuration information will be slightly different, here I use mysql

<?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="connection.url">jdbc:mysql://localhost:3306/sh_books</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- 配置hibernate的基本信息 -->

        <!-- 配置数据库方言dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <!-- 指定自动生成数据表的策略 -->
        <property name="hbm2ddl.auto">update</property>


        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25



Step 3: Create Object - relational mapping file

Well, let's connect it to the database

First click on the lower left corner of the little guy IDEA, IDEA far right this time there will be a toolbar, click on the database, to connect to our database.

Write pictures described here

Write pictures described here

Write pictures described here

This time the database has been successfully connected, to see the information in the database.

Write pictures described here

Next, create a mapping file by persistence.

Click IDEA leftmost column of persistence, right-click demo

Write pictures described here

Write pictures described here

Then IDEA is very smart to help us create the java files and xml files

Write pictures described here



The fourth step: preparation of the corresponding test class

import com.hibernate.demo.MajorEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;

/**
 * Created by violet on 2016/3/10.
 */
public class majorTest {

    private SessionFactory sessionFactory;
    private Session session;
    private Transaction transaction;

    @Test
    public void test(){
        System.out.println("test....");

        //1. 创建配置对象
        Configuration config = new Configuration().configure();
        //2. 创建服务注册对象
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
        //3. 创建会话工厂对象
        sessionFactory = config.buildSessionFactory(serviceRegistry);
        //4. 会话对象
        session = sessionFactory.openSession();
        //5. 开启事务
        transaction = session.beginTransaction();

        //6. 生成专业对象
        MajorEntity majorEntity = new MajorEntity("XXXX", "XXXX", 3);
        //7. 保存对象进入数据库
        session.save(majorEntity);

        //8. 提交事务
        transaction.commit();
        //9. 关闭会话
        session.close();
        //10. 关闭会话工厂
        sessionFactory.close();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

This time do not forget to hibernate configuration files to be updated, in fact, just generate relational mapping file when you can choose to update the xml file, to forget.

Write pictures described here

Well, this time we ran it, found a successful run, this time log file shows like this

Write pictures described here

Look again at the database, the perfect insert a new data, which is to use the process of creating the first intellij hibernate example, to continue to learn it!


Guess you like

Origin blog.csdn.net/Charles_dai001/article/details/78812815