使用Eclipse配置Hibernate环境并实现用户添加功能
什么是Hibernate框架
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的JavaEE架构中取代CMP,完成数据持久化的重任。
使用Hibernate的便捷之处
- 在DAO层操作XML,将数据封装到XML文件上,读写XML文件数据实现CRUD;
- 在DAO层使用原生JDBC连接数据库,实现CRUD;
- 简而言之,我们使用Hibernate框架就不用我们写很多繁琐的SQL语句,从而简化我们的开发!
准备工作
- 导入Hibernate库
- 创建数据库表
- 创建配置文件
导入Hibernate库
下载网址:http://sourceforge.net/projects/hibernate/files/
数据库表结构
Hibernate的配置文件
- hibernate.cfg.xml
- User.hbm.xml(.hbm.xml文件和类文件都要放到.pojo包内,同时文件名要一致)
hibernate.cfg.xml
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hellohibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="show_sql">true</property>
<mapping resource="cn/hrbust/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
创建User类
package cn.hrbust.pojo;
import java.util.Date;
public class User {
private int id;
private String name;
private String gender;
int age;
String birthday;
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
User.hbm.xml
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<class name="cn.hrbust.pojo.User" table="T_USER">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="gender"/>
<property name="age"/>
<property name="birthday"/>
</class>
</hibernate-mapping>
使用Hibernate的七个步骤
package cn.hrbust.dao;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.hrbust.pojo.User;
public class manageUser {
public static void main(String[] args) {
Configuration conf = null;
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
User user = new User();
user.setName("叶海天");
user.setGender("男");
user.setAge(21);
user.setBirthday("2000-6-27");
try {
conf = new Configuration().configure();
sf = conf.buildSessionFactory();
session = sf.openSession();
ts = session.beginTransaction();
session.save(user);
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(ts != null) {
ts.rollback();
}
}finally {
session.close();
sf.close();
}
}
}
添加log4j文件在控制台显示生成的语句
log4j文件可以在官方给出的project中找到:hibernate-release-5.2.10.Final\project\etc
运行结果
运行后控制台输出结果如下图:
数据库查询结果如下图:
过程中遇到的问题
用Data Source Explorer进行连接验证
在验证数据库是否成功连接成功时,由于老师所使用的是MyEclipse,我使用的是Eclipse没有MyEclipse的DB Browser功能,于是我使用Data Source Explorer进行验证。
操作步骤
- Database Connections右键new
- 选择MySQL点击next
- 点击下拉框右侧第一个按钮
- 输入url、用户名、密码
- 点击OK即可进行测试
当看到如下图所示提示信息后,证明连接成功: