关于Hibernate问题??? 小弟求一个hibernate入门的例子,最好是有简单的增,删,改,查。 还有导入hiberate后的配置问题? 谢谢了!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 第一步:建好数据库表第二步:配置好hibernate.cfg.xml文件第三步:生成好pojo类(javabean)剩下的就是使用hibernate进行操作表了。具体方法的话楼主想要给邮箱我,发你点示范代码 [email protected]麻烦楼上的一起发一点给!不甚感激 参考下吧1、环境准备采用的是eclipse平台加上J2EE插件myeclipse的配置,数据库为微软的SQL Server2000,驱动为微软的jtds,具体配置如下:eclipse 3.2.0myeclipse 5.1.1GASqlserver2000+SP4jtds1.2.jar2、数据库数据库名为hibernate,只有一张表computercreate 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:jtds:sqlserver://localhost:1433/hibernate</property><property name="connection.username">sa</property><property name="connection.password">sa</property><property name="connection.driver_class"> net.sourceforge.jtds.jdbc.Driver</property><property name="dialect">org.hibernate.dialect.SQLServerDialect</property><mapping resource="com/weportal/computer/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.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.rootLogger=warn,stdout4、操作类文件 添加数据类ComputerCreate.javapackage 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(); 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();}} 查询类ComputerView.javapackage 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(); 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();}} 请问哪里有买《Expert One-On-One J2Ee Design and Development》这本书? 急问! Java Servlet Api (chm格式) 求助:哪里有mysql jdbc驱动程序下载 高分急问:struts实现最简单的jsp功能 xml中bean调用问题 『高手』WEB上如何实现所有终端的同步性。 讨论-Java接口使用规则 colspan问题 ,简单得很! 关于struts2中的标签属性 开源项目怎么参加啊? socket 客户端不能同时读写
第二步:配置好hibernate.cfg.xml文件
第三步:生成好pojo类(javabean)
剩下的就是使用hibernate进行操作表了。具体方法的话楼主想要给邮箱我,发你点示范代码
麻烦楼上的一起发一点给!不甚感激
1、环境准备
采用的是eclipse平台加上J2EE插件myeclipse的配置,数据库为微软的SQL Server2000,驱动为微软的jtds,具体配置如下:
eclipse 3.2.0
myeclipse 5.1.1GA
Sqlserver2000+SP4
jtds1.2.jar2、数据库
数据库名为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:jtds:sqlserver://localhost:1433/hibernate
</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">
net.sourceforge.jtds.jdbc.Driver
</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="com/weportal/computer/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,stdout4、操作类文件
添加数据类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();
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();
}} 查询类ComputerView.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();
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();
}
}