);
p.setAotoid("1");
p.setUsername("zhang");
p.setAmount(new Integer(10));
p.setPrice(new Integer(10));
System.out.println(p+"_______________1");
try{
session.save(p);
tx.commit();
session.close();
System.out.println(p+"_______________2");
}catch(Exception ex){
ex.printStackTrace();
}
}
}
myeclipse下hibernate入门实例介绍(3)
时间:2011-03-24 CJamie
7.在test.hibernate包下建立:Product.hbm.xml.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping- 3.0.dtd" >
<hibernate-mapping package="test.hibernate">
<class table="test_products" name="Product">
<!--自动编号-->
<id name="id" type="int" column="id" unsaved- value="0">
<generator class="identity"/>
</id>
<property name="aotoid" column="aotoid" type="string" length="50"/>
<property name="username" column="username" type="string" length="50"/>
<property name="price" column="price" type="int"/>
<property name="amount" column="amount" type="int"/>
</class>
</hibernate-mapping> 8.打开hibernate.cfg.xml. 加入:
<mapping resource="hibernate.cfg.xml" />
<mapping resource="test/hibernate/Product.hbm.xml" />
形成完整的config:
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">jdbc:jtds:sqlserver://192.168.1.8 8:1433;DatabaseName=goto
</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect&l t;/property>
<property name="connection.password">hcsys</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdb c.Driver</property>
<mapping resource="hibernate.cfg.xml" />
<mapping resource="test/hibernate/Product.hbm.xml" />
</session-factory>
</hibernate-configuration>
9.运行InsertProduct.java就可以看到数据库goto表 test_products中增加了一条记录。 |