作为SSH开发框架中的ORM部分,感觉这个ORM跟以前用过的一些ORMapping框架相比,配置起来还是相对麻烦的。
一,引入Jar包
刚开始只是引入了hibernat非常基本的jar包进去,后来debug的时候,发现缺失了很多jar包,就又引了一批进去。所以特别想问问大家引包的时候,有什么方法可以提高引入jar包的效率吗?
二,创建实体类及实体类_数据库对应xml文件
例如,表结构如下:
然后我建立如下实体类:
package net.blogjava.nokiaguy.models;
public class MapEntity {
private int id;
private String keyword;
private String value;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
并对这个实体类建立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="net.blogjava.nokiaguy.models.MapEntity" table="t_map">
<!-- 将id属性应射成自增类型 -->
<id name="id" column="id" type="int">
<generator class="increment"/>
</id>
<property name="keyword" column="keyword"/>
<property name="value" column="value"/>
</class>
</hibernate-mapping>
之后是核心配置文件:hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,XML解析器使用DTD文档来检查XML文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在Hibernate3.1.3软件包中的src\org\hibernate目录中找到此文件-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!--表明以下的配置是针对session-factory配置的,SessionFactory是Hibernate中的一个类,这个类主要负责保存HIbernate的配置信息,以及对Session的操作-->
<hibernate-configuration>
<!--配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序-->
<session-factory>
<!--设置数据库的连接url:jdbc:mysql://localhost/**,其中localhost表示mysql服务器名称,此处为本机, **是数据库名-->
<property name="connection.url">
jdbc:mysql://localhost/testhibernate
</property>
<!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接那种类型的数据库服务器。-->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- 显示hibernate生产的SQL语句 -->
<property name="show_sql">true</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 指定Map.hbm.xml的位置 -->
<mapping resource="net/blogjava/nokiaguy/models/Map.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这个里面主要是配置连接的一些需要的参数及实体类_表的配置文件的位置。
三,action中通过hibernate调用操作数据库
public String execute()
throws Exception
{
Configuration configuration=new Configuration();
SessionFactory sessionFactory;
configuration.configure("/hibernate.cfg.xml");
sessionFactory=configuration.buildSessionFactory();
//开始一个会话
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
//开始事物
transaction.begin();
MapEntity mapEntity1=new MapEntity();
mapEntity1.setKeyword("ihu");
mapEntity1.setValue("bc...");
MapEntity mapEntity2=new MapEntity();
mapEntity2.setKeyword("iou");
mapEntity2.setValue("bc...");
//持久化两个对象
session.save(mapEntity1);
session.save(mapEntity2);
//提交事务
transaction.commit();
session.close();
result="保存成功";
return "success";
}
对比下.net平台下的Nhibernate(http://blog.csdn.net/lhc1105/article/details/48709295),会发现98%的东西都是一致的。