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>,详解如下:![](https://img-blog.csdn.net/20160630160530927?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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();
}
}
}