Hibernate的入门
:
第一步:下载
Hibernate
的开发包
:
第二步:Hibernate框架目录结构
:
documentation :Hibernate文档
lib :Hibernate开发jar包
* bytecode :操作字节码jar包.
* jpa :Hibernate的实现jpa规范.
* optional :Hibernate的可选jar包.
* required :Hibernate的必须的jar包.
project :Hibernate提供的工程
第三步:创建一个工程
:(Java
工程
)
导入相应jar包:
* hibernate3.jar
* HIBERNATE_HOME/lib/required/*.jar
* HIBERNATE_HOME/lib/jpa/hibernate-jpa-2.0-api-1.0.1.Final.jar
* 导入日志记录的包:
* log4j-1.2.16.jar
* slf4j-log4j12-1.7.2.jar
* 导入数据库驱动:
第四步:创建表
:(
关系型数据库
)
create database hibernate3_day01;
use hibernate3_day01;
create table customer(
id int primary key auto_increment,
name varchar(20),
age int
);
第五步:创建一个实体类
:(
面向对象
)
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;
}
}
第六步:创建
ORM
的映射
.
映射文件只要是一个XML格式文件就可以.名字任意.
* 通常情况下名称规范:
* 实体类名称.hbm.xml
引入约束:
* hibernate3.jar/org.hibernate.hibernate-mapping-3.0.dtd
<!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="cn.itcast.hibernate3.demo1.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)"/>
-->
<property name="name" column="name" type="string"/>
<property name="age" column="age"/>
</class>
</hibernate-mapping>
第七步:创建一个
Hibernate
的核心配置文件
.
通知Hibernate连接是哪个数据库.
在src下创建一个hibernate.cfg.xml
引入约束:
* hibernate3.jar/org.hibernate.hibernate-configuration-3.0.dtd
<!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
">123
</property>
<!-- Hibernate的方言 -->
<!-- 生成底层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>
<!-- hbm:映射 to DDL: create drop alter -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 通知Hibernate加载那些映射文件 -->
<mapping resource="cn/itcast/hibernate3/demo1/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
第八步:编写测试
向数据库中插入一条记录
@Test
// 向数据库中插入一条记录
public void demo1(){
// 1.Hiberante框架加载核心配置文件(有数据库连接信息)
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();
}
发现了中文乱码问题:
解决方法:
1.java代码中
String name = new String("哈哈".getBytes("utf-8"));
2.在hibernate.cfg.xml中修改数据库连接url,添加useUnicode=true&characterEncoding=UTF-8。
注意:在xml中 & -> &
</property>
<property name="
hibernate.connection.url">
jdbc:mysql:///hibernate_test?useUnicode=true&characterEncoding=UTF-8
</property>