初次学习hibernate,第一个程序,用的是XML配置文件
准备工作:下载hibernate的JAR包,我下的是当前最新的4.2.3,SQL的数据库驱动文件SQLJDBC,导入上述架包。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAP8AAAGuCAIAAADQz0d7AAAgAElEQVR4nO2dXZAdxXXH7wvv+0AV8ATmwxF2BTDGOC5jmZeACA84qVKVoOyygoEQKw4SsY0IcrgSiAgQRtbn4sCywpaNLJCFFIkVH0HCSAjbhZePiDUpS7v6QKv9kK52pd17dxd3HsYMs9OnT5/u6ZnpmTm/mtqa27f79Jm5//nfuXfm7K0JR7zz/L3hIj8749a1riZimITUP6HmJFz3c/dOf7gw1oHVz/iDUv11NXKUNYsfXrP44TWLl00c75g43jF2cO3QO48e3v2jP25dsGbxMiHETf+xMegJqr9Wq9VqNXAd7OMJtU9AWsT0rWC8AlO/aoDc+PB9jy39yT5x6nV4iYhe5f2g4lV90sA0eEz0YIvw8qBlQtyoXwhx94JVYnAbvAix6fUPZty6dsata59+6R1weFnVbxGZyQxn6v/OPz0hjv5cueioRYi2gH3kxth6bAUfRYkMJhxbZ/UXDo36a9MRavULIQbeerpvw+yBt54WQsy9rWPubU8Fy7dve2ru7U99O1i//am5tz0lj1Wd6+N9wBWLUZS5VAkLVn9hceb9Qoi+DbPF1Ht9G2YLIV7o+1i1/P03fyaPtVa//I5BjCzbf2wU+IYA9mf1FxRn6h/YtbzZU//GjFqzpz6w6zEhxMb9rY37mxv3tyJL829nPwsOT+j9qiH4KMpcKlj9JcCZ+vs2zA7VH9h/x96Rjj0jHXtHntoz0rF3pGPvyFe+8Rw4NmbeoZjoZ+eUIfIsYGd8OtXU9BbGH9x839+3beHwrrnNnvq7T81s9tSHX5t7aNvClTuGVm4fXLljcOX2oZU7Bi//uy2ZbBHDUFGq34i+DbM/PrBk+PXvf2NGbfj173/8pyV9G2bPuG5bdHGVcS7UJPLOiHGAG/XvXXrZnw+vbfSs2L76Hxo9K8ShtXuXXuYqRYZJCTfqP/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nOlnYUissOAAAAAElFTkSuQmCC" alt="" />
至此准备工作OK。
步骤:
1:定义一个类,这个类的属性,就是你要存放到数据库中的内容。当然有些属性可以不存入,这些还需要配置文件来决定。
我的类的定义:
package com.hibernate.model; import java.util.Date; public class Log { public Log()
{
this.OperateTime = new Date();
} private int Id ;
private int Version;
private int VersionHelper;
private String OperateType;
private Date OperateTime ;
private String Operater ;
private String DetailOperate ;
private Boolean Success;
private String FailInfo ; public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public int getVersion() {
return Version;
}
public void setVersion(int version) {
Version = version;
}
public int getVersionHelper() {
return VersionHelper;
}
public void setVersionHelper(int versionHelper) {
VersionHelper = versionHelper;
}
public String getOperateType() {
return OperateType;
}
public void setOperateType(String operateType) {
OperateType = operateType;
}
public Date getOperateTime() {
return OperateTime;
}
public void setOperateTime(Date operateTime) {
OperateTime = operateTime;
}
public String getOperater() {
return Operater;
}
public void setOperater(String operater) {
Operater = operater;
}
public String getDetailOperate() {
return DetailOperate;
}
public void setDetailOperate(String detailOperate) {
DetailOperate = detailOperate;
}
public Boolean getSuccess() {
return Success;
}
public void setSuccess(Boolean success) {
Success = success;
}
public String getFailInfo() {
return FailInfo;
}
public void setFailInfo(String failInfo) {
FailInfo = failInfo;
} } 2:配置这个类的的映射文件。Log.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.model">
<class name="Log" table="WMS.Log">
<id name="Id" column="Id">
</id>
<property name="Version" column="Version" type="int" not-null="true"></property> <property name="VersionHelper" column="VersionHelper" type="int" not-null="true"></property> <property name="OperateType" column="OperateType" type="string"/> <property name="OperateTime" column="OperateTime" /> <property name="Operater" column="Operater" type="string"/> <property name="DetailOperate" column="DetailOperate" type="string"/>
<property name="Success" column="Success" type="boolean"/>
<property name="FailInfo" column="FailInfo" type="string"/> </class>
</hibernate-mapping>
配置文件的时候,里面的name是类中对应属性的名字,而column是这个属性对应数据库中的列,一般情况下基本相同,方便理解.
后面的TYPE,是属性对应的类型,如果不写的话,那么会自动识别。
3:配置hibernate.cfg.xml文件,所有这些文件的位置在上图中都有显示,这个配置文件要放在项目根目录下。
<?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="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;databaseName=wms_meide</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property> <property name="show_sqlserver">true</property> <mapping resource="com/hibernate/model/Log.hbm.xml" /> </session-factory>
</hibernate-configuration>
其中mapping配置,是要映射类的映射文件所在的地方。
到此所有的配置工作已经完成。
写一个单元测试:
public static void main(String[] args)
{ Log log=new Log();
log.setId(1111);
log.setOperater("gjf");
log.setOperateType("ccc");
log.setVersionHelper(1);
log.setVersion(2);
log.setSuccess(true); Configuration cfg=new Configuration();
// cfg.configure("/hibernate.cfg.xml"); @SuppressWarnings("deprecation")
SessionFactory sf=cfg.configure().buildSessionFactory();//这个方法已经过时,但是还是可以用
Session session=sf.openSession();//得到session
session.beginTransaction();//开启事务
session.save(log);//保存到数据库
session.getTransaction().commit();//提交事务,如果事务出现问题,那么保存会失败
session.close();
sf.close(); }
第一个hibernate已经写好并成功运行。
期间出现的问题:
1:无法解析hibernate.cfg.xml文件。
解决方法:这个问题主要是XML文件书写有误,导致格式不正确。将其用浏览器打开,如果不报错则说明已经OK。如果报错,按照提示,解决问题!
2:写入数据库的时候,无法执行SQL语句,
可能是因为配置文件时的格式和数据库格式不一致,将配置文件中的TYPE删除即可。