最终目的:直接使用save方法将student对象存入到表中
所需jar包
其中分别是hibernate-distribution-3.3.2.GAlib equired中的7个jar包,
由于slf4j-api-1.5.8.jar只是接口,实现还需要加入对应的slf4j-nop-1.5.8.jar
另外三个jar包是annotations时使用的。
连接数据库用的jar包mysql-connector-java-5.1.18-bin.jar
建数据库hibernate,建表student
建Student类
package hjj.lch.hibernate.model;
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;
}
}
建测试类StudentTest.java
建hibernate配置文件 hibernate.cfg.xml,默认让在src根目录下。该文件直接从参考文件中拷贝,修改对应的数据连接,注释掉暂时用不上的内容
<?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>
<!-- Database connection settings (数据库连接设置) -->
<!-- 此处连接的是MySQL数据库,则配置成MySQL数据库驱动 -->
<!-- Database connection settings -->
<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">hjj</property>
<!-- JDBC connection pool (use the built-in) (hibernate 自带的连接池)-->
<!-- <property name="connection.pool_size">1</property> -->
<!-- 将HQL hibernate 官方语言翻译成其他数据库语言 -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Echo all executed SQL to stdout (执行过程中是否打印SQL语句) -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="hjj/lch/hibernate/model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
ddl数据定义语言,即建表语句
将实体对象与数据库表关联,建立student映射文件 stuent.hbm.xml 和实体类放在一起
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hjj.lch.hibernate.model">
<!-- 以下是表的映射, 此处若不写 table="student" ,
则表示实体类名字和表名一样,数据库表名捕区分大小写-->
<class name="Student" table="student">
<!-- 以下为字段的映射 -->
<!-- id为主键,name="id"指的是类里面的属性,getId()方法,所以对应的字段还可以指定成别的。
因为实体类的属性名和表中字段名一致,所以column=""可以省略了-->
<id name="id" column="id"></id>
<!-- 普通属性 -->
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
<mapping resource="hjj/lch/hibernate/model/Student.hbm.xml"/>这句话的作用是告诉hibernate.cfg.xml去哪里找student.hbm.xml这个配置文件。
这些东西都弄好了开始写测试类StudentTest.java
package hjj.lch.hibernate.model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student(); // 创建student对象
s.setId(1);
s.setName("s1");
s.setAge(1);
// 解析hibernate.cfg.xml,拿到session
Configuration cfg = new Configuration(); // 用于解析hibernate.cfg.xml
SessionFactory sf = cfg.configure().buildSessionFactory(); // 产生session工厂,可以想象成产生数据库的connection
Session session = sf.openSession(); // 可以理解为数据库的connection
// 事务 transaction
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}
在hibernate里面,操作都应该放在一个事务里面
run as java applilcation 结果为:
hibernate的实现过程如图: