[标题]:在MyEclipse中开发Hibernate入门
[时间]:2009-6-17
[摘要]:一个MyEclipse Hibernate HelloWorld。
[关键字]:Hibernate,入门,MyEclipse,HelloWorld,MySQL Connector/J
[环境]:MyEclipse7 ,Hibernate3.2,MySQL5.1
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys
[正文]:
1、新建工程:
在MyEclipse中新建一个Java Web Project( 或Java Project),名叫MyEclipseHibernate,在工程名上点击右键,在弹出菜单中选择"MyEclipse=>Add Hibernate Capabilities..."(或选择菜单栏上"MyEclipse=>Project Capabilities=>Add Hibernate Capabilities...")。其中连接数据库使用MySQL驱动(放置位置/MyEclipseHibernate/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.7-bin.jar)。MySQL驱动下载地址:http://dev.mysql.com/downloads/connector/j/5.1.html。
根据提示配置,会生成类似配置文件:
/MyEclipseHibernate/src/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="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/db
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile"></property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<!--后来添加的BookAuthor.hbm.xml-->
<mapping resource="wintys/hibernate/BookAuthor.hbm.xml" />
</session-factory>
</hibernate-configuration>
2、在MySQL中建立数据库表:
CREATE TABLE `bookauthor` (
`authorId` varchar(100) NOT NULL DEFAULT '0',
`authorAge` int(4) DEFAULT NULL,
`authorName` varchar(20) DEFAULT NULL,
PRIMARY KEY (`authorId`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
3、编写实体类及映射文件:
BookAuthor.java:
package wintys.hibernate;
/**
* BookAuthor Bean
* @version 2009-5-19
* @author Winty(wintys@gmail.com) http://www.blogjava.net/wintys
*/
public class BookAuthor{
private int authorId;
private int authorAge;
private String authorName;
public int getAuthorId(){
return authorId;
}
public void setAuthorId(int authorId){
this.authorId = authorId;
}
public int getAuthorAge(){
return authorAge;
}
public void setAuthorAge(int authorAge){
this.authorAge = authorAge;
}
public String getAuthorName(){
return authorName;
}
public void setAuthorName(String authorName){
this.authorName = authorName;
}
}
编写映射文件,并将其加入到hibernate.cfg.xml映射中:
/MyEclipseHibernate/src/wintys/hibernate/BookAuthor.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="wintys.hibernate.BookAuthor" table="bookauthor">
<id name="authorId" column="authorId">
<generator class="increment"/>
</id>
<property name="authorAge"/>
<property name="authorName"/>
</class>
</hibernate-mapping>
4、测试
/MyEclipseHibernate/WebRoot/index.jsp:
<%@page contentType="text/html;charset=GBK" %>
<%@page import="wintys.hibernate.*"%>
<%@page import="org.hibernate.*"%>
<%@page import="org.hibernate.cfg.*"%>
<%
try{
SessionFactory sf =
new Configuration().configure().buildSessionFactory();
Session ss = sf.openSession();
Transaction tc = ss.beginTransaction();
BookAuthor bookAuthor = new BookAuthor();
bookAuthor.setAuthorId(8884);
bookAuthor.setAuthorAge(449);
bookAuthor.setAuthorName("asefdww");
ss.save(bookAuthor);
tc.commit();
ss.close();
out.println("insert:OK");
}catch(HibernateException e){
out.println(e.getMessage());
e.printStackTrace();
}
%>
5、运行结果
控制显示:
Hibernate: select max(authorId) from bookauthor
Hibernate: insert into bookauthor (authorAge, authorName, authorId) values (?, ?, ?)
Web页面显示:
insert:OK
[参考资料]:
使用myeclipse开发第一个hibernate程序 : http://blog.csdn.net/freesnail/archive/2009/02/19/3908877.aspx
[附件]:手动开发Hibernate应用程序
[1]、
HibernateJavaSE_HelloWorld.zip : http://2xozaw.bay.livefilestore.com/y1pseTFkzzt9ht_m4qbiNjJvgDpKT5gp1ifnbX7HLhvjkmRQY0EmSyw9aE90Bu-mp6hkHEIcTDuIvm-6l_3BmztD2kRBO5d1kZe/HibernateJavaSETest_HelloWorld.zip?download
http://www.blogjava.net/Files/wintys/HibernateJavaSE_HelloWorld.zip
[2]、
HibernateJavaWeb_HelloWorld.zip : http://2xozaw.bay.livefilestore.com/y1pOG7LonhMrFHqc1OOqVYABFdZ8vONRfx2kgKu_TI5i6A6xqrah1fH7y2ImTcaMk-S0rrYUhoGJY7G3kqGFIrkEQ/HibernateJavaWeb_HelloWorld.zip?download
http://www.blogjava.net/Files/wintys/HibernateJavaWeb_HelloWorld.zip
posted on 2009-06-17 22:21
天堂露珠 阅读(489)
评论(0) 编辑 收藏 所属分类:
Hibernate