锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
璇存槑錛氭湰渚嬮噰鐢╩yeclipse+sqlserver2000
hibernate鐗堟湰涓?.0.
鏁版嵁搴撻┍鍔ㄤ負錛歫tds-1.0.2銆?/p>
1銆傞厤緗甿yeclipse鐜錛坋clipse-SDK-3.1.1+myeclipse4.1.1GA_E3.1錛夈?br>2.閰嶇疆濂絪qlserver2000鏁版嵁搴擄細寤虹珛鏁版嵁搴擄細goto . 琛?test_products
CREATE TABLE [dbo].[test_products] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[aotoid] [char] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[username] [char] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[price] [int] NULL ,
[amount] [int] NULL
)
id涓鴻嚜鍔ㄧ紪鍙楓?/p>
3銆傛柊寤洪」鐩?-myeclipse--Web Project 銆傛湰渚嬩負錛歱rodcast <!-- Generated by MyEclipse Hibernate Tools. --> <session-factory> </session-factory> </hibernate-configuration> import java.io.Serializable; public class Product implements Serializable { private static final long serialVersionUID = 3858043967100350732L; public Integer getAmount() { import org.hibernate.SessionFactory; public class InsertProduct { public static void main(String[] args) { <property name="aotoid" column="aotoid" type="string" length="50"/> </hibernate-mapping> 8.鎵撳紑hibernate.cfg.xml.鍔犲叆錛?br> <mapping resource="hibernate.cfg.xml" /> <!-- Generated by MyEclipse Hibernate Tools. --> <session-factory> </session-factory> </hibernate-configuration>
4. 鍙沖崟鍑籶rodcast錛屽姞鍏yeclipse鐨刪ibernate.騫跺湪姝ゅ伐紼嬩腑寤虹珛鏁版嵁榪炵粨銆?br>寤虹珛瀹屾垚鍚庯紝鍦╯rc涓嬭嚜鍔ㄧ敓鎴恏ibernate.cfg.xml錛屽唴瀹逛負錛?br><?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"
<hibernate-configuration>
<property name="connection.username">sa</property>
<property name="connection.url">jdbc:jtds:sqlserver://192.168.1.88:1433;DatabaseName=goto</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.password">hcsys</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
5銆傚緩绔嬶細Product.java:
package test.hibernate;
private Integer id;
private String aotoid;
private String username;
private Integer price;
private Integer amount;
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public String getAotoid() {
return aotoid;
}
public void setAotoid(String aotoid) {
this.aotoid = aotoid;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
6.寤虹珛InsertProduct.java
package test.hibernate;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;
import org.hibernate.classic.Session;
SessionFactory sf = new Configuration().configure()
.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Product p = new Product();
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();
}
}
}
7.鍦╰est.hibernate鍖呬笅寤虹珛:Product.hbm.xml.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"
<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="username" column="username" type="string" length="50"/>
<property name="price" column="price" type="int"/>
<property name="amount" column="amount" type="int"/>
</class>
<mapping resource="test/hibernate/Product.hbm.xml" />
褰㈡垚瀹屾暣鐨刢onfig:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"
<hibernate-configuration>
<property name="connection.username">sa</property>
<property name="connection.url">jdbc:jtds:sqlserver://192.168.1.88:1433;DatabaseName=goto</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.password">hcsys</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<mapping resource="hibernate.cfg.xml" />
<mapping resource="test/hibernate/Product.hbm.xml" />
9.榪愯InsertProduct.java灝卞彲浠ョ湅鍒版暟鎹簱goto琛╰est_products涓鍔犱簡涓鏉¤褰曘?br>