一.hibernate和mybatis都是orm产品
1.orm:object-realation-mapping对象关系映射
二.开发步骤
1.导入相关jar 包括hibernate和oracle的驱动
2.在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="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.username">java</property> <property name="connection.password">java123</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 所有的实体类映射文件要在此处全部注册 --> <mapping resource="com/pojo/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
3.写一个实体类
Student.java
package com.pojo; import java.util.Date; public class Student { int studentId; String studentName; String studentSex; int studentAge; int classId; Date birthday; public Student() { } public Student(int studentId, String studentName, String studentSex, int studentAge, int classId, Date birthday) { this.studentId = studentId; this.studentName = studentName; this.studentSex = studentSex; this.studentAge = studentAge; this.classId = classId; this.birthday = birthday; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getStudentSex() { return studentSex; } public void setStudentSex(String studentSex) { this.studentSex = studentSex; } public int getStudentAge() { return studentAge; } public void setStudentAge(int studentAge) { this.studentAge = studentAge; } public int getClassId() { return classId; } public void setClassId(int classId) { this.classId = classId; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
4.给实体类写一个*.hbm.xml的映射配置文件
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.pojo.Student" table="student_info" schema="JAVA"> <id name="studentId"> <column name="student_id"></column> <!--主键的生成方式 --> <generator class="increment"></generator> </id> <property name="studentName" column="student_name" type="java.lang.String"></property> <property name="studentSex" column="student_sex" type="java.lang.String"></property> <property name="studentAge" column="student_age" type="java.lang.Integer"></property> <property name="classId" column="class_Id" type="java.lang.Integer"></property> <property name="birthday" column="birthday" type="java.util.Date"></property> </class> </hibernate-mapping>
5.在hibernate.cfg.xml配置中加入映射文件的注册
见第2步
6.访问代码
Test.java
package com.dao; import java.util.Date; import java.util.List; import org.hibernate.*; import org.hibernate.cfg.*; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import com.pojo.Student; public class Test { public static void main(String[] args) { // 配置对象,用来获取核心配置文件的 // 如果核心配置文件名是:hibernate.cfg.xml,并且该文件在src根目录下,configure()可以不带参数 Configuration configuration = new Configuration().configure(); ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); SessionFactory sf = configuration.buildSessionFactory(sr); Session session = sf.openSession(); Student stu = new Student(11,"你好","2",22,1,new Date()); // session.save(stu); // session.delete(stu); session.update(stu); session.beginTransaction().commit();//提交事务 session.flush(); session.close(); } }