1、环境准备
采用的是eclipse平台加上J2EE插件myeclipse的配置,数据库为微软的SQL Server2000,驱动为微软的jtds,具体配置如下:
eclipse 3.2.0
myeclipse 5.1.1GA
Sqlserver2000+SP4
jtds1.2.jar
2、数据库
数据库名为hibernate,只有一张表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[数据库所对应的java类文件]
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方法
*/
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方法集
*/
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文件
新建一文件,起名为log4j.Properties,在文件中添加如下内容:
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、操作类文件
添加数据类ComputerCreate.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会话连接的状态
sess.connection().commit();
}catch(HibernateException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
sess.close();
}
}
另外,借助于hibernate自身功能,可以根据.hbm.xml映射文件生成数据库表。DatabaseGenerate.java类用于完成这一功能。值得注意的是,如果数据库中存在同名表,hibernate采取的措施是先删除该表,再生成同名表,因此,如果数据表中有数据,应该进行备份
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");
//从hibernate.cfg.xml中导入hibernate配置数据
Configuration cfg=new Configuration();
cfg.configure();
//打开自动创建和修改数据库资源的开关
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
SessionFactory sf=cfg.buildSessionFactory();
}
}