加下来,我们就要开始我们的第一个Hibernate程序了。
1、同上一讲创建一个java项目并导入相应的jar包和Log4j属性文件。
2、在数据库中创建一个表:(关系型数据库)
create database hibernate3_day01;
use hibernate3_day01;
create table customer(
id int primary key auto_increment,
name varchar(20),
age int
);
3、创建一个实体类:(面向对象)
package com.js.demo2;
public class Customer {
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;
}
}
4、新建映射文件(通常跟实体类在一个文件夹下,名字可以任意,一般有一定的命名规范:实体类名.hbm.xml)
所以我们命名为:Customer.hbm.xml
引入约束:hibernate3.jar/org.hibernate.hibernate-mapping-3.0.dtd
<?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:类的全路径 table:表名称 -->
<class name="com.js.demo2.Customer" table="customer">
<!-- 建立类中的属性和表中的字段的映射 -->
<!-- 唯一标识 -->
<!-- 使用id标签配置唯一属性 -->
<!-- 在id标签中配置一个主键的生成策略 -->
<id name="id" column="id">
<generator class="native"/>
</id>
<!-- 普通属性 -->
<!-- property标签:name:类中的属性名称 column:表中的字段名 -->
<!-- type有三种写法,可以写成java类型(java.lang.String)、hibernate类型(string)、
sql类型(不能直接使用type属性,使用子标签column,<column name="name" sql-type="varchar(20)></column>) -->
<!-- type可以不写,hibernate可以帮助我们完成转换 -->
<property name="name" column="name" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
5、在src下创建hibernate的核心配置文件hibernate.cfg.xml,通知hibernate你连接的是哪个数据库。
Hibernate中有个叫session的对象,类似于咱们JDBC中的connection对象,session-factory类似于数据库连接池。
关于property的配置,可以去Hibernate解压路径下的project中的etc文件夹下找到hibernate.properties文件。
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate3_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 配置hibernate方言,因为hibernate自动生成sql语句,这里确定底层生成的sql语句 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可选的属性 -->
<!-- 显示sql -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- 通知hibernate加载哪些映射文件 -->
<mapping resource="com/js/demo2/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6、编写测试类,向数据库中添加一条记录。
package com.js.demo2;
/**
* hibernate入门案例的测试:
*/
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateTestDemo1 {
@Test
//向数据库中插入一条记录
public void demo1(){
//1、hibernate框架加载核心配置文件(有数据库连接信息)
Configuration configuration = new Configuration().configure();
//2、创建一个sessionFactory对象(获得session--相当于连接对象)
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3、获得session对象
Session session = sessionFactory.openSession();
//4、默认的情况下,事务时不自动提交的。开启一个事务。
Transaction tx=session.beginTransaction();
//5、业务逻辑操作-向数据库中插入一条记录
Customer customer = new Customer();
customer.setName("武松");
customer.setAge(28);
session.save(customer);
//6、事务提交
tx.commit();
//7、释放资源
session.close();
sessionFactory.close();
}
}
7、运行测试:
数据库:
记录添加成功。这就是一个简单的入门案例,供大家参考。后续将系统介绍Hibernate框架的使用。