1銆佺幆澧冨噯澶?/strong>
閲囩敤鐨勬槸eclipse騫沖彴鍔犱笂J2EE鎻掍歡myeclipse鐨勯厤緗紝鏁版嵁搴撲負(fù)寰蔣鐨凷QL Server2000錛岄┍鍔ㄤ負(fù)寰蔣鐨刯tds錛屽叿浣撻厤緗涓嬶細(xì)
eclipse 3.2.0
myeclipse 5.1.1GA
Sqlserver2000+SP4
jtds1.2.jar
2銆佹暟鎹簱
鏁版嵁搴撳悕涓篽ibernate錛屽彧鏈変竴寮犺〃computer
create table computer(
id int(11) not null primary key auto_increment,
cpu varchar(50),
mainboard varchar(50),
displaycard varchar(50),
harddisk varchar(50),
display varchar(50),
memory varchar(50),
soundcard varchar(50),
cdrom varchar(50),
mouse varchar(50),
keyboard varchar(50)
);
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">profile</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">xxxxx</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="org/bwgl/sample/hibernate3/Computer.hbm.xml" />
</session-factory>
</hibernate-configuration>
javabean : Computer.java[鏁版嵁搴撴墍瀵瑰簲鐨刯ava綾繪枃浠禲
package com.weportal.computer;
public class Computer {
private int id;
private String cpu;
private String mainboard;
private String memory;
private String harddisk;
private String display;
private String keyboard;
private String mouse;
private String displaycard;
private String soundcard;
private String cdrom;
/*
*鑾峰彇computer灞炴х殑get鏂規(guī)硶
*/
public int getId(){
return id;
}
public String getCpu(){
return cpu;
}
public String getMainboard(){
return mainboard;
}
public String getMemory(){
return memory;
}
public String getHarddisk(){
return harddisk;
}
public String getDisplay(){
return display;
}
public String getKeyboard(){
return keyboard;
}
public String getMouse(){
return mouse;
}
public String getDisplaycard(){
return displaycard;
}
public String getSoundcard(){
return soundcard;
}
public String getCdrom(){
return cdrom;
}
/*
* 璁劇疆computer灞炴х殑set鏂規(guī)硶闆?br />
*/
public void setId(int id){
this.id=id;
}
public void setCpu(String cpu){
this.cpu=cpu;
}
public void setMainboard(String mainboard){
this.mainboard=mainboard;
}
public void setMemory(String memory){
this.memory=memory;
}
public void setHarddisk(String harddisk){
this.harddisk=harddisk;
}
public void setDisplay(String display){
this.display=display;
}
public void setKeyboard(String keyboard){
this.keyboard=keyboard;
}
public void setMouse(String mouse){
this.mouse=mouse;
}
public void setDisplaycard(String displaycard){
this.displaycard=displaycard;
}
public void setSoundcard(String soundcard){
this.soundcard=soundcard;
}
public void setCdrom(String cdrom){
this.cdrom=cdrom;
}
}
Computer.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.weportal.computer.Computer" table="computer">
<id name="id" type="int">
<generator class="native"/>
</id>
<property name="cpu"></property>
<property name="mainboard" ></property>
<property name="displaycard" ></property>
<property name="harddisk" ></property>
<property name="display"></property>
<property name="memory"></property>
<property name="soundcard"></property>
<property name="cdrom"></property>
<property name="mouse"></property>
<property name="keyboard"></property>
</class>
</hibernate-mapping>
log4j.Properties鏂囦歡
鏂板緩涓鏂囦歡錛岃搗鍚嶄負(fù)log4j.Properties錛屽湪鏂囦歡涓坊鍔犲涓嬪唴瀹癸細(xì)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.rootLogger=warn,stdout
4銆佹搷浣滅被鏂囦歡
娣誨姞鏁版嵁綾籆omputerCreate.java
package com.weportal.computer;
import java.sql.SQLException;
import org.apache.log4j.PropertyConfigurator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ComputerCreate {
public static void main(String args[]){
//PropertyConfigurator.configure("log4j.Properties");
Configuration cfg=new Configuration();
cfg.configure();
// cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
SessionFactory sf=cfg.buildSessionFactory();
Session sess=sf.openSession();
Computer pc=new Computer();
pc.setCpu("Intel Pentuim4 2.4B GHZ");
pc.setDisplay("LG 563LS");
pc.setDisplaycard("NVIDA GeForce 4 MX 440");
pc.setHarddisk("WD120JB");
pc.setMainboard("鎶鍢?8I845PE-RZ");
pc.setMemory("kingMax 512MBX2");
pc.setMouse("Logitech MX 500");
pc.setSoundcard("Creative SB Live");
pc.setKeyboard("Logitech");
pc.setCdrom("Sony DVD-ROM");
sess.save(pc);
sess.flush(); try{
//鎻愪氦hibernate浼?xì)璇潣q炴帴鐨勭姸鎬?br />
sess.connection().commit();
}catch(HibernateException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
sess.close();
}
}
鍙﹀錛屽熷姪浜巋ibernate鑷韓鍔熻兘錛屽彲浠ユ牴鎹?hbm.xml鏄犲皠鏂囦歡鐢熸垚鏁版嵁搴撹〃銆侱atabaseGenerate.java綾葷敤浜庡畬鎴愯繖涓鍔熻兘銆傚煎緱娉ㄦ剰鐨勬槸錛屽鏋滄暟鎹簱涓瓨鍦ㄥ悓鍚嶈〃錛宧ibernate閲囧彇鐨勬帾鏂芥槸鍏堝垹闄よ琛紝鍐嶇敓鎴愬悓鍚嶈〃錛屽洜姝わ紝濡傛灉鏁版嵁琛ㄤ腑鏈夋暟鎹紝搴旇榪涜澶囦喚
package com.weportal.computer;
import org.apache.log4j.PropertyConfigurator;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
public class DatabaseGenerate {
public static void main(String args[]){
//閰嶇疆log4j.Properties
PropertyConfigurator.configure("log4j.Properties");
//浠巋ibernate.cfg.xml涓鍏ibernate閰嶇疆鏁版嵁
Configuration cfg=new Configuration();
cfg.configure();
//鎵撳紑鑷姩鍒涘緩鍜屼慨鏀規(guī)暟鎹簱璧勬簮鐨勫紑鍏?br />
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
SessionFactory sf=cfg.buildSessionFactory();
}
}