码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate简介与HelloWorld

时间:2017-07-26 01:46:13      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:服务   open   new   开启   --   private   技术   comm   reg   

一、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

标签:服务   open   new   开启   --   private   技术   comm   reg   

原文地址:http://www.cnblogs.com/tenWood/p/7237224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!