前言:
我们知道java是面向对象的语言,而我们现在使用的数据库是关系型数据库,所以当用java这种面向对象的程序语言去操作关系型数据库的时候,就会出现不匹配的情况,也就是不能把一个对象很好的保存到关系型数据库中,这种问题叫做阻抗不匹配,关系模型和对象模型之间不能够对应起来,所以,我们要做是对这两种模型进行转换.
在JDBC中,查找方法,如下面这段代码:
查找用户的方法,就是从数据库里读一条记录出来.我们从结果集ResultSet中把数据读出来然后赋给user对象,这个过程相当于把关系模型转化成了对象模型.
插入方法,如下面这段代码:
插入用户的方法,就是将一条记录插入到数据库中.我们user对象保存到关系型数据库中,可以看出我们将对象模型转换成关系型模型.
正文开始:
第一步:创建一个普通的java project hibernate
第二步:创建一个实体对象(User类),如下代码:
package cn.ljq.hibernate.domain;
import java.util.Date;
public class User {
private int id;
private String name;
private Date 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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "ID: "+ id + " Name: "+ name +" Birthd: "+ birthday;
}
}
--------------------------------------------------
第三步:导入hibernate依赖的jar包和hibernate3.jar(真正来完成映射的包)
--------------------------------------------------
第四步:先做映射文件
其作用是将对象模型跟关系模型衔接起来,也就是将user对象跟数据库表对应起来.
步骤:创建一个User.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="cn.ljq.hibernate.domain">
<class name="User">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="birthday"/>
</class>
</hibernate-mapping>
--------------------------------------------------
第五步:创建hibern的配置文件hibernate.cfg.xml
代码如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test_1</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="cn/ljq/hibernate/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
---------------------------------------
第六步:创建一个Test测试类:
代码如下:
package cn.ljq.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import cn.ljq.hibernate.domain.User;
public class Test {
public static void main(String[] args){
//初始化
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.openSession();
User user = new User();
user.setBirthday(new Date());
user.setName("name");
s.save(user);
s.close();
System.out.println(user);
System.out.println("end");
}
}
到此,一个简单的Hibernate连接数据的程序就完成了.
--------------------------------------------------
下面是经常出现的2个异常的解释:
异常一:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: cn.ljq.hibernate.domain.User
解释:该异常原因是没有找映射文件
解决:在Hibernate配置文件中加入此代码: 注意,映射文件路径根据当前项目而定
<mapping resource="cn/ljq/hibernate/domain/User.hbm.xml"/>
异常二:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [cn.ljq.hibernate.domain.User]
Caused by: java.sql.SQLException: Table 'test_1.user' doesn't exist
解释:该异常原因是没有找到数据库表
解决:两种方式:
第一种:到数据库创建User表
第二种:在Hibernate配置文件中加入此代码:这是自动建表的意思
<property name="hbm2ddl.auto">create</property>
其中有4种方式:均经过本人实际测试,事实说话(^_^)
create-drop: 根据model类生成表,但是sessionFactory一关闭,表就自动删除,注
意,一定是关闭sessionFactory,只关闭session是不会删除表的
create: 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成
表,哪怕2次没有任何改变
update: 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存
在,不会删除以前的行
validate: 只会和数据库中的表进行比较,不会创建新表,但是会插入新值,正式运行
时用这种方式比较安全