Hibernate (1)

Source: Internet
Author: User

1. What is hibernate?

Hibernate is an open-source framework used to encapsulate the data access layer. It is called a data access layer framework (or a persistent layer framework)

Previously, we used JDBC/SQL statements to access/operate data from the database. hibernate encapsulates this write operation, which is specially used for the component technology of the data access layer.

Ii. Required jar packages

Hibernate: hibernate3.jar

Antlr-2.7.6.jar

Commons-collections-3.1.jar

Dom4j-1.6.1.jar

Javassist-3.9.0.GA.jar

Jta-1.1.jar

Slf4j-api-1.5.8.jar

Server Load balancer: slf4j-nop-1.5.8.jar

JDBC: mysql-connector-java-5.1.16-bin.jar

3. Create a database and a table

create database hibernate;create table student (id int primary key,name varchar(20),age int);

4. Prepare the hibernate configuration file

This file is used to store the link information of the database and the configuration information of hibernate. It is named hibernate. cfg. xml.

<? Xml version = '1. 0' encoding = 'utf-8'?> <! DOCTYPE hibernate-configuration PUBLIC "-// Hibernate/Hibernate Configuration DTD 3.0 // EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <! -- Associate the photo Printing file -->
<Mapping resource = "org/hibernate/tutorial/domain/Event. hbm. xml"/> </session-factory>

5. Create entity Student

package com.xxx.model;public class Student {    private int id;    private String name;    private int age;    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 int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}

Create the Print Object of Student: student. hbm. xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

Modify the associated Printing file in hibernate. cfg. xml

<mapping resource="com/xxx/model/Student.hbm.xml"/>

 

Then you can create a test class StudentTest.

package com.xxx.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.liguobing.model.Student;public class StudentTest {    public static void main(String[] args) {        Student s = new Student();        s.setId(1);        s.setName("SSS");        s.setAge(40);        Configuration cfg = new Configuration();        SessionFactory sf = cfg.configure().buildSessionFactory();        Session session = sf.openSession();        Transaction t = session.getTransaction();        t.begin();        session.save(s);        t.commit();        session.close();    }}

Run it and you will see it on the console.

Check the database

 

  

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.