分享
 
 
 

安装配置Hibernate+Tomcat

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

关于服务器Tomcat和数据库MySQL的安装配置参见本人上一篇文章

《在Windows 2000 Server安装配置Tomcat+MySQL》

在Tomcat上建立独立的服务myService

0.set %WebApp% = $Tomcat_HOME$/webapps/myService

1 在$Tomcat_HOME$/webapps下创建目录结构

myService

+-----WEB-INF

+-----lib

+-----class

+-----net

+-----sf

+-----hibernate

+-----examples

+-----quickstart

2 在Tomcat的server.xml($Tomcat_HOME$/conf)中为此服务配置独立的Context

<Context className="org.apache.catalina.core.StandardContext" cachingAllowed="true" charsetMapperClass="org.apache.catalina.util.CharsetMapper" cookies="true" crossContext="false" debug="0" displayName="Welcome to Tomcat" docBase="C:\Tomcat4.1\webapps\myService" mapperClass="org.apache.catalina.core.StandardContextMapper" path="/myService" privileged="false" reloadable="false" swallowOutput="false" useNaming="true" wrapperClass="org.apache.catalina.core.StandardWrapper">

<Resource name="jdbc/mysqltest" scope="Shareable" type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/mysqltest">

<parameter>

<name>maxWait</name>

<value>5000</value>

</parameter>

<parameter>

<name>maxActive</name>

<value>4</value>

</parameter>

<parameter>

<name>password</name>

<value>test</value>

</parameter>

<parameter>

<name>url</name>

<value>jdbc:mysql://localhost:3306/study</value>

</parameter>

<parameter>

<name>driverClassName</name>

<value>com.mysql.jdbc.Driver</value>

</parameter>

<parameter>

<name>maxIdle</name>

<value>2</value>

</parameter>

<parameter>

<name>username</name>

<value>test</value>

</parameter>

</ResourceParams>

<ResourceLink name="jdbc/mysqltest" global="jdbc/mysqltest" type="javax.sql.DataSourcer"/>

</Context>

3 解压下载的压缩包hibernate-2.1.2.zip,将解压出来的hibernate2.jar复制到%WebApp%/WEB-INF/lib

4 将解压出来的lib目录下的

cglib-full-2.0.2.jar

commons-collections-2.1.1.jar

commons-logging-1.0.4.jar

dom4j-1.4.jar

ehcache-0.9.jar

jta.jar

log4j-1.2.8.jar

odmg-3.0.jar

文件同样复制到%WebApp%/WEB-INF/lib

5 编写hibernate配置文件hibernate.cfg.xml放在%WebApp%/WEB-INF/classes下

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration

PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="connection.datasource">java:comp/env/jdbc/mysqltest</property>

<property name="show_sql">false</property>

<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

<!-- Mapping files -->

<mapping resource="Cat.hbm.xml"/>

</session-factory>

</hibernate-configuration>

*connection.datasource的名称必须和[Config Tomcat DBCP for Mysql]配置中server.xml的context指定的DBCP的名称一致。

6 编写第一个测试代码

6.1 将以下代码保存为Cat.java,并生成相应的Cat.class,放入%WebApp%/WEB-INF/classes,这里无论你用什么方法生成Cat.class,但最终Cat.class应在%WebApp%/WEB-INF/classes/net/sf/hibernate/examples/quickstart目录下

package net.sf.hibernate.examples.quickstart;

public class Cat {

private String id;

private String name;

private char sex;

private float weight;

public Cat() {

}

public String getId() {

return id;

}

private void setId(String id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public char getSex() {

return sex;

}

public void setSex(char sex) {

this.sex = sex;

}

public float getWeight() {

return weight;

}

public void setWeight(float weight) {

this.weight = weight;

}

}

6.2 将以下代码保存为O/R映射文件Cat.hbm.xml,放入%WebApp%/WEB-INF/classes

<?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="net.sf.hibernate.examples.quickstart.Cat" table="CAT">

<!-- A 32 hex character is our surrogate key. It's automatically

generated by Hibernate with the UUID pattern. -->

<id name="id" type="string" unsaved-value="null" >

<column name="CAT_ID" sql-type="char(32)" not-null="true"/>

<generator class="uuid.hex"/>

</id>

<!-- A cat has to have a name, but it shouldn' be too long. -->

<property name="name">

<column name="NAME" length="16" not-null="true"/>

</property>

<property name="sex"/>

<property name="weight"/>

</class>

</hibernate-mapping>

6.3 将以下代码保存为HibernateUtil.java,并生成相应的HibernateUtil.class,放入%WebApp%/WEB-INF/classes,同样注意package

package net.sf.hibernate.examples.quickstart;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import net.sf.hibernate.*;

import net.sf.hibernate.cfg.*;

public class HibernateUtil {

private static Log log = LogFactory.getLog(HibernateUtil.class);

private static final SessionFactory sessionFactory;

static {

try {

// Create the SessionFactory

sessionFactory = new Configuration().configure().buildSessionFactory();

} catch (Throwable ex) {

log.error("Initial SessionFactory creation failed.", ex);

throw new ExceptionInInitializerError(ex);

}

}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {

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();

}

}

6.4 将以下代码保存为testcat.jsp,放入%WebApp%

<%@ page language="java" pageEncoding="GB2312" %>

<%@ page import="net.sf.hibernate.Transaction"%>

<%@ page import="net.sf.hibernate.Session"%>

<%@ page import="net.sf.hibernate.cfg.*"%>

<%@ page import="net.sf.hibernate.Query"%>

<%@ page import="net.sf.hibernate.examples.quickstart.HibernateUtil"%>

<%@ page import="net.sf.hibernate.examples.quickstart.Cat"%>

<%@ page import="java.util.*"%>

<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">

<html>

<head>

<title>Lomboz JSP</title>

</head>

<body bgcolor="#FFFFFF">

<%

//添加一只Cat

Session ses = HibernateUtil.currentSession();

Transaction tx= ses.beginTransaction();

Cat princess = new Cat();

princess.setName("chenm");

princess.setSex('F');

princess.setWeight(7.4f);

ses.save(princess);

tx.commit();

HibernateUtil.closeSession();

//读取库里所有Cat

ses = HibernateUtil.currentSession();

tx= ses.beginTransaction();

Query query = ses.createQuery("select c from Cat as c where c.sex = :sex");

query.setCharacter("sex", 'F');

for (Iterator it = query.iterate(); it.hasNext();) {

Cat cat = (Cat) it.next();

out.println("Female Cat: " + cat.getName() );

}

tx.commit();

HibernateUtil.closeSession();

%>

</body>

</html>

6.5 用http测试,http://localhost:8090/myService/testcat.jsp

测试结果,看见:Female Cat: chenm

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有