【Hibernate】入门搭建之XML方式

来源:互联网 发布:淘宝学生男鞋店铺排行 编辑:程序博客网 时间:2024/06/05 18:29

本程序使用Hibernate3.6.10版本


Hibernate 核心包下载地址:http://sourceforge.net/projects/hibernate/files/hibernate3/3.6.10.Final/

Hibernate 对应Annotation下载地址:http://sourceforge.net/projects/hibernate/files/hibernate-annotations/3.4.0.GA/hibernate-annotations-3.4.0.GA.zip/download

一、XML方式所需要的jar包有:

    hibernate-distribution-3.6.10.Final/hibernate3.jar

    hibernate-distribution-3.6.10.Final/lib/required/所有

    hibernate-distribution-3.6.10.Final/lib/jpa/hibernate-jpa-2.0-api-1.0.1.Final.jar

    slf4j-1.6.1/slf4j-nop-1.6.1.jar   (注:此处的slf4j的版本取决于hibernate-distribution-3.6.10.Final/lib/required 下 slf4j-api.*.*.*.jar的版本号,后期可换成log4j)

二、在项目src目录下创建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/hibernate</property>        <property name="connection.username">root</property>        <property name="connection.password">root</property>        <!-- JDBC 连接池 -->        <!-- <property name="connection.pool_size">1</property> -->        <!-- SQL dialect 方言-->        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</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.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> -->        <!-- xml方式配置 -->        <mapping resource="com/ycao/bean/Student.hbm.xml"/>                <!-- annotation方式配置 -->        <mapping class="com.ycao.bean.Teacher"/>    </session-factory></hibernate-configuration>


三、创建实体对象

       

package com.ycao.bean;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;}}

三、创建实体映射文件Student.hbm.xml

      

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">                <hibernate-mapping package="com.ycao.bean">    <class name="Student" table="student">        <id name="id" column="id">        </id>        <property name="age"/>        <property name="name"/>    </class></hibernate-mapping>

四、写测试文件

   

package com.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.ycao.bean.Student;public class TestStudent {public static void main(String[] args) {Student s = new Student();  s.setName("zhang san");s.setAge(20);  Configuration cfg = new  Configuration();    SessionFactory sessionFactory = cfg.configure().buildSessionFactory();  //创建SessionFactory    Session session = sessionFactory.openSession();   //创建hibernate  session    //所有的操作都要放在一个事务里面  session.beginTransaction();    session.save(s); //执行插入操作    session.getTransaction().commit();  //提交事务管理    session.clear();    sessionFactory.close();}}

项目分布截图:

   


注:HibernateUtil 、TestTeacher.java、Teacher.java 是用来测试Annotation时所用到,与本程序无关,如果想了解Annotation的写方请查看下一节。



   

原创粉丝点击