操作系统:window8
数据库版本:mysql-installer-community-5.6.17.0(安装版)
jdbc版本:mysql-connector-java-5.1.30-bin
hibernate版本:hibernate-release-4.2.3.Final
下面是具体操作:
1.首先在eclipse中新建项目hibernatedemo
2.解压hibernate-release-4.2.3.Final将里面lib/required中的的包直接复制到WEB-INF/lib文件夹下同时将jdbc驱动也复制到此文件夹下如下图(注:也可通过eclipse中project选项 properties中的java build path导入)
3.在eclipse中新建数据库example在创建表person代码如下:
create table person(
id int(11) not null auto_increment primary key,
name varchar(100) not null default ‘‘,
age int)
4.创建持久化类person.java代码如下:
package hibernate4.object;
public class person {
private Integer id;
private String name;
private Integer age;
public person(){}
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id=id;}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public Integer getAge(){
return age;
}
public void setAge(Integer age){
this.age=age;
}
public String toString(){
return "person:[id="+id+";name="+name+";age="+age+"]";
}
}
5.编写映射文件person.hbm.xml代码如下:
<?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="hibernate4.object.person" table="person">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="name" column="name" type="java.lang.String"/>
<property name="age" column="age" type="java.lang.Integer"/>
</class>
</hibernate-mapping>
6.编写hibernate配置文件hibernate.cfg.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!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="show_sql">true</property>
<property name="connection.url">
jdbc:mysql://127.0.0.1:3306/example
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.username">root</property>
<property name="connection.password">Scorpio19920204</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<mapping resource="person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
7.编写测试文件FirstHibernate.java代码如下:
package hibernate4.object;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import hibernate4.object.person;
public class FirstHibernate {
public static void main(String args[]){
person person=new person();
person.setName("neo");
person.setAge(20);
Configuration cfg = new Configuration();
cfg.configure();
//SessionFactory sf=cfg.configure().buildSessionFactory();//*过期方法*解析Hibernate.cfg.xml 然后返回一个已经拥有配置选项的Configuration
ServiceRegistry sr=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
//SessionFactory类似数据库库的一个Connection
SessionFactory sf =cfg.buildSessionFactory(sr);
//打开一个连接
Session session =sf.openSession();
//开始一个事务
session.beginTransaction();
session.save(person);
//获得一个事务,并提交
session.getTransaction().commit();
//关闭连接
session.close();
//关闭工厂
sf.close();
}
}
8.运行测试,启动mysql数据库并运行FirstHibernate.java文件运行结果如下:
数据库中显示如下:
学习感悟:
在学习过程中遇到了许多问题,由于参考书上内容一些代码过旧所以在操作过程中遇到了许多问题,但是还好通过上网搜索相关资料都一一解决了,也从中也学到了许多新的知识。