如何让猫活起来
hibernate如何让猫活起来
下面让我们用事例来阐述:

代码是从idiot那引入的,算是借花献佛了。

把数据库驱动放入C:\tomcat\common\lib\
修改C:\TOMCAT/conf/server.xml,在其中添加:


<Context path="/hibernate" docBase="hibernate">
<Resource name=
"jdbc/hibernate" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name=
"jdbc/hibernate">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- DBCP database connection settings -->
<parameter>
<name>url</name>
<value>jdbc:mysql:
//localhost:3306/hibernate?useUnicode=true</value>
</parameter>
<parameter>
<name>driverClassName</name><value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>abc</value>
</parameter>
<!-- DBCP connection pooling options -->
<parameter>
<name>maxWait</name>
<value>3000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>100</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
</ResourceParams>
</Context>


别忘了建立一张表,在mysql数据库,hibernate 库中

目录结构:c:\tomcat\webapps\hibernate\WEB-INF\classes\hb\Cat.class
c:\tomcat\webapps\hibernate\WEB-INF\classes\hb\HibernateUtil.class
c:\tomcat\webapps\hibernate\WEB-INF\classes\hb\Cat.hbm.xml

c:\tomcat\webapps\hibernate\WEB-INF\classes\hibernate.cfg.xml

c:\tomcat\webapps\hibernate\WEB-INF\lib\放置你所要的所有的包

c:\tomcat\webapps\hibernate\hb\addCat.jsp
c:\tomcat\webapps\hibernate\hb\getCat.jsp


<--------------------------------------------------------------->
Cat.java


package hb;

public class Cat {

private String id;
private String name;

public Cat() {
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
String strCat = new StringBuffer()
.append(this.getId()).append(
", ")
.append(this.getName()).append(
", ")
.toString();

return strCat;
}
}




<--------------------------------------------------------->

HibernateUtil.java


package hb;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException(
"Exception building SessionFactory: "
+ ex.getMessage(), ex);
}
}


public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
// Session s;
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}

}




<-------------------------------------------------------------->

Cat.hbm.xml


<?xml version=
"1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name=
"hb.Cat" table="cat">

<id name=
"id" type="java.lang.String" unsaved-value="null" >
<column name=
"CAT_ID" sql-type="varchar(32)" not-null="true"/>
<generator class=
"uuid.hex"/>
</id>

<property name=
"name">
<column name=
"NAME" sql-type="varchar(20)" not-null="true"/>
</property>


</class>

</hibernate-mapping>



<-------------------------------------------------------------->

hibernate.cfg.xml

<?xml version=
"1.0" encoding="GB2312" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name=
"hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name=
"hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true</property>
<property name=
"hibernate.connection.username">root</property>
<property name=
"hibernate.connection.password">abc</property>

<property name=
"show_sql">false</property>
<property name=
"dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<mapping resource=
"hb/Cat.hbm.xml" />


</session-factory>
</hibernate-configuration>




<-------------------------------------------------------------->

addCat.jsp


<%@ page contentType=
"text/html; charset=GB18030" %>
<%@ page import=
"hb.*, net.sf.hibernate.*" %>
<html>
<head>
<title>test</title>
</head>
<body bgcolor=
"#ffffff">
<h1>Test Hibernate</h1>
<a href=
"getCats.jsp">View Cats</a><br>
<%
if(request.getParameter(
"Go") != null) {
String name = request.getParameter(
"name");
if(name == null || name.length() < 1) {
out.println(
"Name can not be empty!");
return;
}

SessionFactory sessionFactory;
net.sf.hibernate.Session hsession = HibernateUtil.currentSession();

Transaction tx = hsession.beginTransaction();
Cat c;
c = new Cat();
c.setName(name);
hsession.save(c);
hsession.flush();

tx.commit();

HibernateUtil.closeSession();
out.println(
"Done.");
}
%>
Add New Cats:<br>
<form method=post action=addCat.jsp>
Name:<input name=name><br>
<input type=submit name='Go' value='Add'><br>
</form>
</body>
</html>



<------------------------------------------------------>


getCat.jsp


<%@ page contentType=
"text/html; charset=GB18030" %>
<%@ page import=
"hb.*, net.sf.hibernate.*,
java.util.*
" %>
<html>
<head>
<title>Hibernate Test</title>
</head>
<body bgcolor=
"#ffffff">
<h1>Test Hibernate</h1>
<a href=addCat.jsp>Add Cat</a><br>
<p> ID Name </p>
<%
SessionFactory sessionFactory;
net.sf.hibernate.Session hsession;
hsession = HibernateUtil.currentSession();

Transaction tx = hsession.beginTransaction();

Query query = hsession.createQuery (
"select cat from Cat as cat ");

for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println(cat.getId()+
"&nbsp; " + cat.getName() + "<br>");
}

tx.commit();

HibernateUtil.closeSession();
out.println(
"Done.");
%>
</body>
</html>



air_tuyh   2005-05-11 21:36:48 评论:0   阅读:1570   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2008 powered by YuLog