二、在Eclipse中生成POLO和映射文件
(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)1) 在Eclipse中配置环境,包括数据库驱动程序(此处略)
2) 生成POLO类和映射文件
此处以Houses为例,生成的原代码和映射文件如下:
a) hibernate.cfg.xml
?xml version=´1.0´ encoding=´UTF-8´?
!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"
!-- DO NOT EDIT: This is a generated file that is synchronized --
!-- by MyEclipse Hibernate tool integration. --
hibernate-configuration
session-factory
!-- properties --
property name="connection.username"sa/property
property name="connection.url"jdbc:microsoft:sqlserver://localhost:1433;databaseName=RMSDB/property
property name="dialect"net.sf.hibernate.dialect.SQLServerDialect/property
property name="connection.password"jckjdkmcj/property
property name="connection.driver_class"com.microsoft.jdbc.sqlserver.SQLServerDriver/property
!-- mapping files --
mapping resource="zy/pro/hrms/dao/Staffs.hbm.xml"/
mapping resource="zy/pro/hrms/dao/Houses.hbm.xml"/
mapping resource="zy/pro/hrms/dao/Menus.hbm.xml"/
mapping resource="zy/pro/hrms/dao/Tables.hbm.xml"/
mapping resource="zy/pro/hrms/dao/Customers.hbm.xml"/
mapping resource="zy/pro/hrms/dao/Notice.hbm.xml"/
/session-factory
/hibernate-configuration
b) Houses.hbm.xml
?xml version="1.0"?
!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
!-- DO NOT EDIT: This is a generated file that is synchronized --
!-- by MyEclipse Hibernate tool integration. --
!-- Created Fri May 13 14:09:45 CST 2005 --
hibernate-mapping package="zy.pro.hrms.dao"
class name="Houses" table="Houses"
id name="hid" column="HID" type="java.lang.Integer"
generator class="increment"/
/id
property name="alias" column="alias" type="java.lang.String" /
property name="description" column="description" type="java.lang.String" /
property name="capity" column="capity" type="java.lang.Integer" not-null="true" /
/class
/hibernate-mapping
c) Houses类
/*
* Created Fri May 13 14:09:45 CST 2005 by MyEclipse Hibernate Tool.
*/
package zy.pro.hrms.dao;
import java.io.Serializable;
/**
* A class that represents a row in the ´Houses´ table.
* This class may be customized as it is never re-generated
* after being created.
*/
public class Houses
extends AbstractHouses
implements Serializable
{
/**
* Simple constructor of Houses instances.
*/
public Houses()
{
}
/**
* Constructor of Houses instances given a simple primary key.
* @param hid
*/
public Houses(java.lang.Integer hid)
{
super(hid);
}
/* Add customized code below */
}
3) 将生成的文件导入到JBuilder工程中
三、编写测试类
我们的第一个测试类的代码如下:
package zy.pro.hrms.test;(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import zy.pro.hrms.util.HibernateSessionFactory;
import zy.pro.hrms.dao.*;
public class HouseTest {
public HouseTest() {
}
public static void main(String args[]) {
try {
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i 50; i++) {
Houses h = new Houses();
//h.setHid(new Integer(2));
h.setAlias("dddd");
h.setCapity(new Integer(4));
session.save(h);
tx.commit();
}
//tx.commit();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
注意:
1) 此处的粗体内容,放的位置不同对程序有什么影响和不同效果(这是我的一个疑点)。
2) 我们在创建数据库时不能在数据库端实现主键的自动增长,而应该留给Hibernate来实现主键的生成
--------------------------此程序运行调试成功--------------------------
运行和编程环境:
window 2000+Jbuilder X+ SQL Server 2000
运行环境的配置另附
还有问题,请和我联系zhyiwww@21cn.com