前言
近日学习了一下Hibernate,着手写了一下Hibernate的第一个小例子,时间紧张,先记下来再说,日后细细整理。
完整代码
先上完整的项目代码,请移步:https://github.com/GaoZiqiang/HibernateDemo
运行环境
hibernate + MySQL
大体步骤
Hibernate作为处理关系型数据库的重要的轻量级的框架,其使用需要涉及到的东西有很多,涉及到数据库、数据库设计等等一系列的知识。
Hibernate的作用
附图:
Hibernate的工作原理
附图:
工具准备
a.Hibernate核心jar包;
b.MySQL合适的版本的驱动;
添加User Library的步骤
a.Windows菜单–>Preferences选项;
b.选择Java–>Build Path–>User Libraries;
c.new一个Libraries,命名(注意不要勾选),OK;
d.add需要的jar包,finish;
使用User Library的步骤
a.点击项目,右键“Build Path”–>Add Libraries;
b.选择User Libraries;
c.勾选需要的Library,finishi即可。
大体实现步骤
a.配置文件hibernate.cfg.xml和映射文件xxx.hbm.xml;
配置文件参考资料:
http://blog.csdn.net/qq_33429968/article/details/53769707
http://blog.csdn.net/qq_33429968/article/details/53770850
b.要持久的类;
c.Controller类。
项目目录
完整的项目目录:
配置文件
代码设计:
外加注释
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">666</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterencoding=UTF-8
</property>
<!-- <property name="myeclipse.connection.profile"> OracleDriver </property> -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!--connection.useUnicode连接数据库时是否使用Unicode编
码-->
<!--connection.characterEncoding连接数据库时数据的传输字符集编码方式,最好设置为gbk,用gb2312有的字符不全-->
<property name="Connection.useUnicode">true </property>
<!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于差错,程序运行时可以在Eclipse的控制台显示Hibernate
的执行Sql语句。项目部署后可以设置为false,提高运行效率-->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="current_session_context_class">thread</property>
<!-- 映射文件 -->
<mapping resource="person.hbm.xml" />
</session-factory>
</hibernate-configuration>
要点解析
a.数据库方言的配置;
参考资料:
http://blog.csdn.net/qq_33429968/article/details/53536778
b.映射文件xxx.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">
<!-- Mapping file autogenerated by MyEclipse Persistence Tools -->
<hibernate-mapping>
<class name="Person" table="person" schema="hibernate">
<id name="id" type="java.lang.String">
<column name="ID" length="32" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="20" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="20" not-null="true" />
</property>
<property name="sex" type="java.lang.String">
<column name="SEX" length="20" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" length="30" />
</property>
</class>
</hibernate-mapping>
实现方式
a.自己手动书写,便于了理解;
b.使用插件自动生成。
该法自行尝试。
持久化类
代码设计
public class Person { // POJO类
private String id;
private String name;
private String password;
private String sex;
private String email;
public Person(String id,String name,String password,String sex,String email) {
this.id = id;
this.name = name;
this.password = password;
this.sex = sex;
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
要点解析
a.属性为private。
Controller类
代码设计
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Controller {
public static void main(String[] args) {
// 读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
// 建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
// 取得session
Session session = null;
try {
// 创建session
session = factory.openSession();
// 开启事务
session.beginTransaction();
Person user = new Person("001", "gaoziqiang", "001", "nan", "163.com");
/*user.setId("001");
user.setName("gao自强");
user.setPassword("123");
user.setSex("F");
user.setEmail("163.com");*/
// 保存User对象
session.save(user);
// 提交事务
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
// 回滚事务
session.getTransaction().rollback();
} finally {
if (session != null) {
if (session.isOpen()) {
// 关闭session
session.close();
}
}
}
}
}
技术要点
a.Hibernate核心jar包的导入;
b.MySQL驱动;
c.两个配置文件的配置。