hibernate--HelloWorld

  本次学习版本:hibernate-release-5.2.6.Final,要求java 1.8 和JDBC 4.2。

  hibernate是一个开放源代码的对象关系映射框架。对JDBC进行了非常轻量的封装。它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库

  hibernate可以在官网直接下载。

  

那么就来看用hibernate实现的一个Hello World的实例。

复制代码
package com.fuwh.model;

//这是一个实体类
public class Helloworld {
    
    private long id;
    private String helloWorld;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getHelloWorld() {
        return helloWorld;
    }
    public void setHelloWorld(String helloWorld) {
        this.helloWorld = helloWorld;
    }
    
    
}
复制代码
复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!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.fuwh.model">
    <class name="Helloworld" table="t_helloworld">
        <id name="id" type="long">
            <generator class="native"></generator>
        </id>
        <property name="helloWorld" column="helloworld"/>
    </class>

</hibernate-mapping>
复制代码
复制代码
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
    
    <session-factory>
        <!-- 数据库连接设置 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">mysqladmin</property>
        <!--
             指定方言,表明用的是哪种数据库,也可以不指定,hibernate默认会翻译成正确的数据库脚本
            方言可以在 hibernate-release-xx.xx/project/etc/hibernate.properties 中查找
         -->
        <property name="hibernate.dialect">MySQL5</property>
        <!-- 设定时候更新表结构,不存在或自动创建 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 配置 在后台打印出sql语句 -->
        <property name="show_sql">true</property>
       <!-- 引入实体类和表的映射文件 -->
        <mapping resource="com/fuwh/model/Helloworld.hbm.xml"/>
    </session-factory>
    
</hibernate-configuration>
复制代码

使用JUNIT的单体测试实例

复制代码
package com.fuwh.service;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.fuwh.model.Helloworld;

public class JunitTest {
    
    private SessionFactory sessionFactory;
    
    @Before
    public void setUp() throws Exception {
        final StandardServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
                                                        .configure()    //读取hibernate.cfg.xml文件
                                                        .build();
        try {
            sessionFactory=new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e);
            StandardServiceRegistryBuilder.destroy(serviceRegistry);                                                                                                                                    
        }
    }

    @After
    public void tearDown() throws Exception {
        if    (sessionFactory !=    null){
            sessionFactory.close();
        }
    }

    @Test
    public void test() {
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        
        Helloworld helloWorld=new Helloworld();
        helloWorld.setHelloWorld("Hello World!!!");
        //完全以操作对象的形式保存对象为表的一条记录
        session.save(helloWorld);
        
        session.getTransaction().commit();
        session.close();
        
        
    }

}
复制代码

执行的时候的部分log

生成的表结构

 插入的表数据

 

 下面在来看用注解的方式的实现

 注解的方式就不再需要Helloworld.hbm.xml文件

实体类修改如下

复制代码
package com.fuwh.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

//这是一个实体类
@Entity
@Table(name="t_helloworld")
public class Helloworld {
    
    private long id;
    private String helloWorld;
    
    @Id
    @GeneratedValue(generator="_native")
    @GenericGenerator(name="_native",strategy="native")
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    @Column(name="helloworld")
    public String getHelloWorld() {
        return helloWorld;
    }
    public void setHelloWorld(String helloWorld) {
        this.helloWorld = helloWorld;
    }
    
    
}
复制代码

另外在hibernate.cfg.xml中不再是引入Helloworld.hbm.xml资源文件,而是引入一个实体类

复制代码
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
    
    <session-factory>
        <!-- 数据库连接设置 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">mysqladmin</property>
        <!--
             指定方言,表明用的是哪种数据库,也可以不指定,hibernate默认会翻译成正确的数据库脚本
            方言可以在 hibernate-release-xx.xx/project/etc/hibernate.properties 中查找
         -->
        <property name="hibernate.dialect">MySQL5</property>
        <!-- 设定时候更新表结构,不存在或自动创建 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 配置 在后台打印出sql语句 -->
        <property name="show_sql">true</property>
       <!-- 引入实体类和表的映射文件 -->
        <mapping class="com.fuwh.model.Helloworld"/>
    </session-factory>
    
</hibernate-configuration>
复制代码

执行结果也是一样的。

 

 

  

posted @ 2017-09-22 10:20  王思聪6  阅读(162)  评论(0编辑  收藏  举报
友情链接:回力球鞋 | 中老年高档女装