今天我就写一下怎么搭建Hibernate的框架的~
步骤如下:
1. 获取hibernate发布包
可以在github或者官网找找~
2.将hibernate依赖的jar集成到项目中
一共要9个jar包~
到这个目录hibernate\hibernate-distribution-3.6.10.Final\lib\required找,有6个jar包,如图:
有这些包还不够,还需要到hibernate\hibernate-distribution-3.6.10.Final\lib\jpa这个目录下,看图:
还需要俩个jar包,一个是连接数据库的包,一个是Hibernate包,如图:
到这里jar包全部找到,可以导入到项目里面了~
在项目中新建一个jar文件,把要依赖的jar包放进去,如图所示:
jar包包括驱动包和hibernate3.jar和一些别的包,都是一些必须要的包~
3.将hibernate依赖的配置文件和映射文件加入到项目中
hibernate.cfg.xml 配置文件
***.hbm.xml 映射文件
这时候就要把dtd文件放入到项目中了~
把hibernate3.jar包解压开来,进入到hibernate\hibernate-distribution-3.6.10.Final\hibernate3\org\hibernate,可以找到俩个dtd文件,如图:
在项目中新建一个dtd文件,把必要的dtd放入到dtd文件中,如图:
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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/ssh</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
这几句可以从dtd文件中hibernate-configuration-3.0.dtd找~,这几句是必须要的
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/ssh</property>中的ssh是在Native MySql中创建的一个数据库的名字~
上面的location就是这个项目中dtd文件的所在目录,而key就是http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd这个链接~
这样配置文件就配置好了
***.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 package="com.xu.day4.basic">
</hibernate-mapping>
<hibernate-mapping package="com.xu.day4.basic">是指要映射到哪个包下的哪个类~
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
这几行代码也是必须的~可以从hibernate-mapping-3.0.dtd文件中找~
<hibernate-mapping>如果这行代码也出不现的话,和配置文件的配置步骤是一样的~
4.将映射文件集成到配置文件中
看下图:
只需要这样集成就行~
那框架就搭配完成了~
现在就可以写写一个简单的例子:
要做一个用户的存储~
User类:
public class User implements Serializable{
private static final long serialVersionUID = 1L;
Long id;
String name;
Double account;
Date birthday;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(Long id, String name, Double account, Date birthday) {
super();
this.id = id;
this.name = name;
this.account = account;
this.birthday = birthday;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getAccount() {
return account;
}
public void setAccount(Double account) {
this.account = account;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", account=" + account + ", birthday=" + birthday + "]";
}
}
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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/ssh</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/xu/day4/basic/user.hbm.xml" />
</session-factory>
</hibernate-configuration>
映射文件user.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 package="com.xu.day4.basic">
<class name="User" table="tb_user">
<id name="id" column="id">
<generator class="increment" />
</id>
<property name="name" />
<property name="account" />
<property name="birthday" />
</class>
</hibernate-mapping>
在Test类中代码运行入口:
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1.创建配置对象
Configuration configuration=new Configuration();
//2.读取配置对象
configuration.configure();
//3.根据配置文件信息创建一级缓存,SessionFactory
/*
* Connection
* 映射文件,映射对象
* HttpSession 用户与服务器之间的会话
* Session 代码与数据库之间的会话
*/
SessionFactory ss=configuration.buildSessionFactory();
//4.创建Session,打开会话
Session session=ss.openSession();
//5.开启事务
Transaction tran=session.beginTransaction();
session.save(new User(null, "张二", 100.0, new Date()));
//6.事务的提交
tran.commit();
//7.session关闭
session.close();
System.out.println("1111111111111111111");
}
}
注释已经写的很清楚,我就不一一解释了~
下一篇我也讲个例子,可以把部分代码封装起来~
效果图我就不贴了,有什么需要的可以和我交流啊~
源码:下载