Some little things about hibernate,,, who hasn't had a first time?

      After three years of training in college, I finally decided to write a blog by myself. Although no one may read it, I still have to write it and exercise myself. So good, the content of the first blog is set to "some little things about Hibernate". OK, let's get started.

       First, let's learn about Hibernate:

       Hibernate is an open source object-relational mapping framework. It encapsulates JDBC with very lightweight objects. It establishes a mapping relationship between POJOs and database tables. It is a fully automatic orm framework. Hibernate can automatically generate SQL statements. Automatic execution, so that Java programmers can 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. The most revolutionary thing is that Hibernate can replace CMP in the J2EE architecture using EJB. , to complete the task of data persistence.

        So, what are the advantages of Hibernate?

1. Encapsulates jdbc, which simplifies a lot of repetitive code.
2. Simplified the DAO layer coding work, making the development more object-oriented.
3. It has good portability and supports various databases. If you change the database, you only need to change the configuration in the configuration file, without changing the hibernate code.

4. Support transparent persistence, because hibernate operates a pure (pojo) java class, does not implement any interface, and is not intrusive. So it is a lightweight framework.

      Let's start implementing a small exercise now:

The first step: database establishment

CREATE TABLE `t_emp` (
  `id` int(20) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `sex` varchar(1) DEFAULT NULL,
  `address` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Step 2: Create an entity class

package com.hust.fubo;


public class Person {



	private int id;
	private String name;
	private String sex;
	private String address;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", sex=" + sex
				+ ", address=" + address + "]";
	}
	
	
}

Step 3: Create a database connection

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


    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/emp?serverTimezone=UTC</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <mapping resource="com/hust/mo/Person.hbm.xml"/>
    </session-factory>


</hibernate-configuration>

Step 4: Configure the corresponding .hbm.xml file for the entity class

<?xml version="1.0"?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="com.hust.mo.Person" table="t_emp">
        <id name="id" column="id" >
            <generator class="native"/>
        </id>

     <property name="name" column="name" type="string" />
     <property name="sex" column="sex" type="string" />
     <property name="address" column="address" type="string" />
    </class>

</hibernate-mapping>

Step 5: Test Class

package com.hust.fubo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class TestDemo {
	@Test
		public void testPerson(){
			
			Configuration  configuration = new Configuration().configure();
			SessionFactory sessionFactory = configuration.buildSessionFactory();
			 Session session = sessionFactory.openSession();
			  Transaction t = session.beginTransaction();
			 Person p = (Person) session.get(Person.class, 1);	
			 System.out.println(p.toString());
			  t.commit();
			  session.close();
			  sessionFactory.close();
		}
}

Running results show:     

 

      Summary: This is the first time I write a blog by myself. Although there are big and small problems, it is generally okay. After all, it is the first time. Among the problems I encountered, the most impressive thing is that the use of hibernate's framework in writing code is always not very proficient, and it always needs to be solved with the help of Baidu or my roommate. Solve, and strive to solve these problems in the future study.

      Unconsciously, I have fallen in love with blogging. In the future, I will strive to complete a blog every week to record the bits and pieces of my learning, and other bigwigs can also come to comment and give suggestions. Humbly accept.

Guess you like

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