The first time I came into contact with hibernate today, I found that the previous entry excellent posts did not involve the most pop combinations such as Tomcat + MySQL, and all of them were developed with IDE, either JBuilder or eclipse, simply put, I will use the simplest method, Tomcat (5.0.19) + MySQL (4) + ultraedit, to implement a basic Hibernate query, so as to facilitate later learning.
1. Ensure that your MySQL instance works properly.
[Code: 1]
Create Table CAT (
Cat_id varchar (20) not null,
Name varchar (20) not null,
Sex char (1 ),
Weight float,
Primary Key (cat_id)
);
Insert cat values ('1', 'ada ', 'F', 5 );
Insert cat values ('2', 'belinda ', 'F', 10 );
Commit;
[/Code: 1]
2. Ensure that your Tomcat works properly. Then stop tomcat.
Add the following content to {tomcat_home}/CONF/server. xml:
[Code: 1]
<Context Path = "/hellohibernate" docbase = "hellohibernate" reloadable = "true">
<Resource Name = "JDBC/hellohibernate" Scope = "retriable" type = "javax. SQL. datasource"/>
<Resourceparams name = "JDBC/hellohibernate">
<Parameter>
<Name> factory </Name>
<Value> org. Apache. commons. DBCP. basicperformancefactory </value>
</Parameter>
<! -- DBCP database connection settings -->
<Parameter>
<Name> URL </Name>
<Value> JDBC: mysql: // localhost: 3306/test </value>
</Parameter>
<Parameter>
<Name> driverclassname </Name> <value> com. MySQL. JDBC. Driver </value>
</Parameter>
<Parameter>
<Name> username </Name>
<Value> yourusername </value>
</Parameter>
<Parameter>
<Name> password </Name>
<Value> yourpassword </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>
[/Code: 1]
3. Rename the attachment hellohibernate.zip to hellohibernate. War, and throw it to {tomcat_home}/webapps to restart tomcat.
4. You will find hellohibernate has been automatically deployed, copy the following files to {tomcat_home}/webapps/hellohibernate/WEB-INF/lib:
Hibernate2.jar
Log4j-1.2.8.jar
Commons-collections-2.1.jar
Commons-logging-1.0.3.jar
Dom4j-1.4.jar
Cglib-2.0-rc2.jar
Odmg-3.0.jar
Ehcache-0.6.jar
JTA. Jar
(In fact, I could have packed them into war files, but now the war file reaches 2 MB, and it is only 8 KB)
5. http: // localhost: 9090/hellohibernate/servlet/hellohibernateservlet
If everything is correct, you can see the cat information.
Hope it's helpful, thanks
Hellohibernate.zip (7.1 KB)
From: http://www.javaeye.com/topic/4077