zoukankan      html  css  js  c++  java
  • 用工具快速建立hibernate框架

    ,一、建好项目后先导入两类jar包,一类是hibernate的jar包,一类是jdbc的jar包

    二、点击“窗口”--“显示视图”--“其它”-“Hibernate configurations”

    三、添加配置。在"hibernate configurations"窗口中右击,选择“Add Configuration”。在窗口中配置数据库链接的相关信息。

     配置好之后就会出来hibernate.cfg.xml文件

     

    四、生成每个表的类和配置文件

    点击“运行”--“Hibernate Code Generation...”--“Hibernate Code Generation配置”

     

     

    点击运行之后会自动生成

    如上图所示  基本上配置就完成了,不过自己还要到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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/mydb</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.default_catalog">mydb</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="show_sql">ture</property>
            <!-- 每个的表映射 -->
            <mapping resource="com/itnba/maya/model/Family.hbm.xml"/>
            <mapping resource="com/itnba/maya/model/Info.hbm.xml"/>
            <mapping resource="com/itnba/maya/model/Nation.hbm.xml"/>
            <mapping resource="com/itnba/maya/model/Title.hbm.xml"/>
            <mapping resource="com/itnba/maya/model/Work.hbm.xml"/>
           
          
        </session-factory>
    </hibernate-configuration>

    测试

    package com.itnba.maya.model;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.hibernate.*;
    import org.junit.Test;
    
    import junit.framework.TestCase;
    
    public class A extends TestCase {
        @Test
        public void testa(){
            Session session =null;
            try{
                session =HibernateUtil.getSession();
                Info info=session.get(Info.class, "p003");
                System.out.println(info.getName()+info.getNation().getName());
                
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                HibernateUtil.closeSession();
            }
        }
    }

    运行结果,没有问题

  • 相关阅读:
    RK 清理后台所有历史App任务
    RK onConfigurationChanged ConfigChanges 设备状态的改变
    RK audio 拨号同时输出Speaker和USB音频
    RK 微信视频通话预览倒立
    Unity 笔记
    C# 泛型约束为枚举
    Unity Editor 扩展PropertyDrawer (属性的 Inspector )
    Unity Editor 笔记
    Unity 反转法线,在 Hierarchy 视图对象的快捷菜单中增加 Flip Mesh Normals(反转网格法线)项
    Blender 2.9 骨骼
  • 原文地址:https://www.cnblogs.com/hq233/p/6510172.html
Copyright © 2011-2022 走看看