zoukankan      html  css  js  c++  java
  • 一个Hibernate的Hello World, 基于Hibernate 4.0

    先在官网上下载最近的Hibernate完整包, 目前最新的是hibernate-release-4.0.0.Final http://www.hibernate.org/downloads .

    打开Myeclipse, 新建一个JavaProject. 

    1.导入Hibernate需要的Jar包.

      需要加入的包有:hibernate-release-4.0.0.Final里面Lib目录下Required的所有Jar包.

    2.加入Hibernate配置文件hibernate.cfg.xml, 文件内容可参考文档(这里使用的是么Mysql):

    <?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>
    <!-- Database connection settings -->
    <property name="connection.driver_class">
    com.mysql.jdbc.Driver
    </property>
    <property name="connection.url">
    jdbc:mysql://localhost:3306/hibernate
    </property>
    <property name="connection.username">用户名</property>
    <property name="connection.password">密码</property>

    <!-- JDBC connection pool (use the built-in)
    <property name="connection.pool_size">1</property>
    -->
    <!-- SQL dialect -->
    <property name="dialect">
    org.hibernate.dialect.MySQLDialect
    </property>

    <!-- Enable Hibernate's automatic session context management
    <property name="current_session_context_class">thread</property>
    -->
    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">
    org.hibernate.cache.internal.NoCacheProvider
    </property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup
    <property name="hbm2ddl.auto">update</property>
    -->
    <property name="myeclipse.connection.profile">
    Hibernate_Test
    </property>
    <mapping resource="jyu/hibernate/model/Student.hbm.xml" />
    <mapping class="jyu.hibernate.model.Teacher" />
    </session-factory>
    </hibernate-configuration>

    3.新建一个实体类Student, 并成长get, set方法:

    package jyu.hibernate.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;
    }


    }


    4.在数据库中建好对应的表Student, 有id, name ,age三个字段.

    5.编写实体与关系的映射文件: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 name="jyu.hibernate.model.Student">
    <id name="id" column="id"/>
    <property name="name" />
    <property name="age" />
    </class>

    </hibernate-mapping>

    6.在hibernate.cfg.xml配置文件中加入Student.hbm.xml:在 </session-factory> 之前加入:<mapping resource="jyu/hibernate/model/Student.hbm.xml" />

    7.测试:StudentTest

    package jyu.hibernate.model;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;

    public class StudentTest {

    public static void main(String[] agrs){
    Student st = new Student();
    st.setId(2);
    st.setName("学生");
    st.setAge(20);

    Configuration cfg = new Configuration(); //读取配置hibernate文件
    SessionFactory sf = cfg.configure().buildSessionFactory(); //根据配置文件生成事务工厂
    Session session = sf.openSession();
    session.beginTransaction(); //事务开启
    session.save(st); //保存对象
    session.getTransaction().commit();
    session.close();
    sf.close();
    }
    }

    运行, 可以查看到数据库中已经插入了一条记录.


    这种方法是比较常用的使用XML来映射实体与关系的方法, 另有使用Annotation(注解)方式也能完成映射功能.

     

    本文为尚学堂马士兵Hibernate视频教程笔记.

    
    



      

  • 相关阅读:
    为什么电影里的黑客都不屑用鼠标? (转)
    专注做好一件事(转) focus---这个世界上最成功的人,他们在某一领域获得成功之后,可通过经营杠杆进入任何他们想要涉足的领域。而这都得依赖于他们曾极致的专注在做好一件事情上。
    世间万物都是遵循某种类似的规律,谁先把握了这些规律,谁就最早成为了强者。
    走的时候不要太急,有时间要停下来想一想当初为什么而走,这样,才会走的更稳,走的更明白。
    Android笔记: Android版本号
    Android笔记:真机调试无法输出Log 信息的问题
    阿里云服务器试用
    Android笔记:利用InputStream和BufferedReader 进行字节流 字符流处理
    Android笔记:java 中的数组
    Android笔记:C memory copy
  • 原文地址:https://www.cnblogs.com/myfjd/p/2325906.html
Copyright © 2011-2022 走看看