Hibernate入门配置详解及例子

1.添加数据库驱动(个人Add JARS选择ojdbc6.jar,看版本而言)


2.新建Java Project配置Hibernate(myeclipse版本较新,一些版本添加Hibernate可以直接在MyEclispe展开的子项中找到)

1)向添加hibernate功能

2)选择hibernate版本及运行环境

3)选择(新建)包放置配置完成后生成的HibernateSessionFactory.java

4)选择数据库连接驱动,点击Finish(第一步已完成驱动配置,数据库版本不影响运行,个人用的是oracle11g)

5)向生成的src下的hibernate.cfg.xml添加<property name="hbm2ddl.auto">update</property>,详解如下:

3、新建java程序及配置

1)新建POJO类Student.java:

package vo;

public class Student {
	private String name;
	private Integer sno;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getSno() {
		return sno;
	}
	public void setSno(Integer sno) {
		this.sno = sno;
	}
	
}

2)在POJO(Student)类的同一包下新建同名.hbm.xml文件,例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>
	<class name="vo.Student" table="tb_student">
		<id name="sno" column="sno" type="int"><!-- 主键 -->
			<generator class="native"></generator>
		</id>
		<property name="name" type="string">
			<column name="name"></column>
		</property>
	</class>
</hibernate-mapping>

附:通过配置XML类别可快速完成配置,关联dtd可通过搜索myeclipse安装目录下的插件库获得dtd文件位置,全名为hibernate-mapping-x.x.dtd


3)添加映射,可以如下图添加后Ctrl+S保存,也可以点击Configuration旁边的Source进行代码添加

hibernate.cfg.xml文件中所新加的代码段:<mapping resource="vo/Student.hbm.xml" />,如下:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hbm2ddl.auto">update</property>
		<property name="myeclipse.connection.profile">orcl</property>
		<property name="dialect">
			org.hibernate.dialect.Oracle9Dialect
		</property>
		<property name="connection.password">tiger</property>
		<property name="connection.username">scott</property>
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:orcl
		</property>
		<property name="connection.driver_class">
			oracle.jdbc.OracleDriver
		</property>
		<mapping resource="vo/Student.hbm.xml" />
	</session-factory>
</hibernate-configuration>

4)execute包下新建AddInfo.java(对数据库数据进行操作时需开启事务)

package execute;

import org.hibernate.Session;

import hibernate.HibernateSessionFactory;
import vo.Student;

public class AddInfo {

	public static void main(String[] args) {
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			session.beginTransaction();//开启事务
			Student stu = new Student();
			stu.setName("Wilson");//Student.hbm.xml已设置id生成方式为native,固此处即使设置了sno也无效
			session.save(stu);//执行数据库提交操作
			session.getTransaction().commit();//事务提交,若无此句无法进行数据库的数据修改操作
			System.out.println("添加成功");
		} catch (Exception e) {
<span style="white-space:pre">			</span>session.getTransaction().rollback();//事务回滚
			System.out.println("添加失败");
			e.printStackTrace();
		}finally {
			HibernateSessionFactory.closeSession();
		}
	}

}

4.查看数据库,程序成功运行(log4j警告可以通过向项目的src包添加log4j.properties消除,log4j资源文件可以在hibernate的文件目录下搜索获取然后copy过去)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值