Hibernate初识

一、创建工程

  1. 在MyEclipse中new-->Java Project

  2. 准备jar

    antlr-2.7.6.jar

    commons-collections-3.1.jar

    dom4j-1.6.1.jar

    ejb3-persistence.jar

    hibernate-annotations.jar

    hibernate-commons-annotations.jar

    hibernate-jpa-2.0-api-1.0.1.Final.jar

    hibernate3.jar

    javassist-3.12.0.GA.jar

    jta-1.1.jar

    mysql-connector-java-5.1.5-bin.jar

    slf4j-api-1.6.1.jar 

  3. 项目结构图

    技术分享

二、创建数据库(mysql) 

技术分享
-- step 1
CREATE DATABASE hibernate;

-- step 2
USE hibernate;

DROP TABLE IF EXISTS student; 
CREATE TABLE student(
    id INT PRIMARY KEY,
    name VARCHAR(20),
    age INT
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
DROP TABLE IF EXISTS Teacher; 
CREATE TABLE Teacher(
    id INT PRIMARY KEY,
    name VARCHAR(20),
    title VARCHAR(20)
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;
View Code

 

三、编写步骤

  1.配置方式

    1) 创建bean(Class)Student.java

    技术分享
package com.cool.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;
    }
}
View Code

    2) 配置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">

<hibernate-mapping>
    <!-- Class Mapping To Table -->
    <class name="com.cool.model.Student" table="student">
        <id name="id" type="int" column="id">
            <generator class="native"></generator>
        </id>
        <property name="name" type="string" column="name"></property>
        <property name="age" type="int" column="age"></property>
    </class>
</hibernate-mapping>
View Code

    3) 配置hibernate.cfg.xml

    技术分享
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//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://192.168.1.248:3306/hibernate</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">admin</property>

        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <!-- 导入映射文件 -->
        <mapping resource="com/cool/model/Student.hbm.xml" />

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

  2.注解方式

    1)创建bean,Teacher.java    

    技术分享
package com.cool.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Teacher {
    private int id;
    private String name;
    private String title;

    @Id
    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 getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
View Code

    2)配置hibernate.cfg.xml

    技术分享
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//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://192.168.1.248:3306/hibernate</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">admin</property>

        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

        <!-- 导入映射文件 -->
        <mapping class="com.cool.model.Teacher" />

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

  3.Test

    1)Student

    技术分享
package com.cool.test;

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

import com.cool.model.Student;

public class StudentTest {

    @Test
    public void studentTest() {
        Configuration cfg = new Configuration();

        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
        Transaction tran = session.beginTransaction();
        Student s = new Student();
        s.setId(1);
        s.setName("myTest");
        s.setAge(20);
        session.save(s);
        tran.commit();
        session.close();
        sf.close();
    }
}
View Code

    2)Teacher

    技术分享
package com.cool.test;

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

import com.cool.model.Teacher;

public class TeacherTest {

    @Test
    public void teacherTest() {
        // JPA:javax.persistence
        Configuration cfg = new AnnotationConfiguration();

        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
        Transaction tran = session.beginTransaction();
        Teacher s = new Teacher();
        s.setId(1);
        s.setName("s1");
        s.setTitle("ming");
        session.save(s);
        tran.commit();
        session.close();
        sf.close();
    }
}
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。