今天终于将hibernate配置完成了,写点东西记录一下。
在网上看的视频,但是视频的hibernate版本太老了,大都是hibernate3版的,现在hibernate已经更新到hibernate4了,所以有些知识还是有点不一样的。
首先,在hibernate4中就已经没有hibernate3中的hibernate3.jar包了。
现在在hibernate官网上下载的hibernate4的解压目录是:
在lib目录下我们将required下的所有的jar包全部导入到工程中去,这样我们的jar包就算添加完成了。
下面是我们的配置文件的编写。
首先我们创建一个pojo叫做Customer
package cn.edu.qfnu.domain;
public class Customer {
private Integer id;
private String userName;
private String passWord;
private String realName;
private String address;
private String mobile;
public Customer(String userName, String passWord, String realName,
String address, String mobile) {
super();
this.userName = userName;
this.passWord = passWord;
this.realName = realName;
this.address = address;
this.mobile = mobile;
}
public Customer(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
然后我们创建pojo的映射文件,命名规则是类名.hbm.xml,所以我们创建Customer.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">
<!-- Generated 2013-8-29 18:32:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="cn.edu.qfnu.domain.Customer" table="CUSTOMER">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="userName" type="java.lang.String">
<column name="USERNAME" />
</property>
<property name="passWord" type="java.lang.String">
<column name="PASSWORD" />
</property>
<property name="realName" type="java.lang.String">
<column name="REALNAME" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
<property name="mobile" type="java.lang.String">
<column name="MOBILE" />
</property>
</class>
</hibernate-mapping>
下一步我们创建hibernate的hibernate.cfg.xml文件,这个文件会对我们创建的pojo对象和数据库产生映射,从而创建数据table。
<?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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="cn/edu/qfnu/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这一切都做好了,我们的hibernate就算配置ok了。
再贴一张工作目录看看
再贴一张lib/required目录下的jar包