【Hibernate】入门搭建之XML方式

本程序使用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的写方请查看下一节。



   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值