Hibernate简介与HelloWorld

Posted 有点懒惰的大青年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate简介与HelloWorld相关的知识,希望对你有一定的参考价值。

一、Hibernate简介:                              

二、Hibernate4 版Hello World 实现                                  

工程结构:

com.cy.model.Student:

package com.cy.model;

public class Student {
    private long id;
    private String name;
    
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
View Code

Student.hbm.xml:

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

<hibernate-mapping package="com.cy.model">
    <class name="Student" table="t_student">
        <!-- 
            <id>代表主键
            类的属性名是id,对应表中列名是stuId
         -->
        <id name="id" column="stuId">
            <!-- 主键生成策略,这里采用mysql的native,自增 -->
            <generator class="native"></generator>
        </id>
        
        <!-- 普通属性采用property 
             不写column,那么数据库table的列名也叫name
        -->
        <property name="name"></property>
        
    </class>
    

</hibernate-mapping>

com.cy.service.StudentTest.java:

package com.cy.service;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.cy.model.Student;

public class StudentTest {
    public static void main(String[] args) {
        Configuration configuration = new Configuration().configure();    //实例化配置文件,默认指定的就是hibernate.cfg.xml
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();//实例化服务登记,服务注册
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);    //获取session工厂
        Session session = sessionFactory.openSession();    //生成一个session
        session.beginTransaction();        //开启事务
        
        Student s = new Student();
        s.setName("张三");
        session.save(s);
        
        session.getTransaction().commit();    //提交事务
        session.close();    //关闭session
        sessionFactory.close();    //关闭session工厂
    }
}

hibernate配置文件:hibernate.cfg.xml:

<?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">

<hibernate-configuration>

    <session-factory>

        <!--数据库连接设置 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
       
        <!-- 方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    
        <!-- 控制台显示SQL -->
        <property name="show_sql">true</property>

        <!-- 自动更新表结构 -->
        <property name="hbm2ddl.auto">update</property>
        
        <mapping resource="com/cy/model/Student.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

 

运行StudentTest.java,测试结果:

 

以上是关于Hibernate简介与HelloWorld的主要内容,如果未能解决你的问题,请参考以下文章

分享知识-快乐自己:初识 Hibernate 概念片

hibernate--HelloWorld

Hibernate之HelloWorld

hibernate的helloworld实现

Hibernate简介

hibernate学习笔记之一 hibernate简介