Configure hibernate2.1.2 to connect to mysql in resin3.0
Configure hibernate2.1.2 to connect to mysql in resin3.0
Author: hamal
Conventions:
Resin3 indicates the installation root directory of resin3.0
Hibernate2 indicates the installation root directory of hibernate2.1.2.
1. Create a web application root under resin3, such as the resin3mydomain Directory (the directory name of mydomain can be retrieved at will, and then configured in the configuration file)
2. Create the resin3mydomainWEB-INFclasses directory and the resin3mydomainWEB-INFlib directory under the resin3mydomain directory respectively.
These two directories correspond to the classloader search path for the context of the web application (resin3mydomainWEB-INFlib for jar and resin3mydomainWEB-INFclasses for class files ). These two paths are called application library class paths (used to store jar class libraries related to the application) and context class paths (used to store the class files and xml configuration files of the application ).
Another path is the resin3lib directory, which we call as the global library class path (stored on the resin3 server for shared use by all web applications on the server ).
3. This example uses the mysql database, so we put the mysql jdbc driver jar package (mm. Mysql-2.0.4-bin.jar) into the resin3lib directory, then the driver will be used by all web applications.
4. Copy the hibernate2 hibernate2.jar file to the resin3mydomainWEB-INFlib directory, and then copy the necessary jar files under the hibernate2lib directory to the resin3mydomainWEB-INFlib directory. If you are not clear about which package is yours, you can refer to the hibernate2libreadme.txt file, or, simply put, we copy all the jar files under the hibernate2lib directory to the resin3mydomainWEB-INFlib directory.
5. Now we start to configure the resin jdbc database connection pool. Modify the resin3confresin. conf file
A) search for webapps and replace webapps with the custom application directory mydomain.
B) search for the <database> element and cancel the annotation status of the element. The annotation in this file adopts the html-style annotation method. <! -->.
C) modify the <database> element to the following format:
<Database>
<Jndi-name> jdbc/mysql </jndi-name>
<Driver type = "org. gjt. mm. mysql. Driver">
<Url> jdbc: mysql: // 192.162.125.3: 3306/mysql </url>
<User> root </user>
& Lt; password & gt; 12345678 & lt;/password & gt;
</Driver>
<Prepared-statement-cache-size> 8 </prepared-statement-cache-size>
<Max-connections> 20 </max-connections>
<Max-idle-time> 30 s </max-idle-time>
</Database>
So the configuration of the resin jdbc connection pool is complete.
6. Copy the hibernate. properties, log4j. properties, and oscache. properties files under the hibernate2src directory to the resin3 mydomainWEB-INFclasses directory.
7. Because we use the mysql database, modify the mysql configuration in the hibernate. properties file and comment out the original default HypersonicSQL configuration. Note configuration is to add the # symbol before the statement. For example:
# Hibernate. dialect net. sf. hibernate. dialect. HSQLDialect
The following is a typical mysql configuration:
Hibernate. dialect net. sf. hibernate. dialect. MySQLDialect
Hibernate. connection. driver_class org. gjt. mm. mysql. Driver
Hibernate. connection. driver_class com. mysql. jdbc. Driver
Hibernate. connection. url jdbc: mysql: // 192.162.125.3: 3306/mydb
Hibernate. connection. username root
Hibernate. connection. password 12345678
We need to modify the following three lines:
Url refers to the jdbc connection descriptor in the format of jdbc: mysql: // database IP: port number/database name
Username is the username used to log on to the database.
Password this user password
8. Bind the database connection pool of hibernate and resin. Create a hibernate. cfg. xml file in the directory. The file content is as follows:
<? 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/mysql </property>
<Property name = "show_ SQL"> true </property>
<Property name = "dialect"> net. sf. hibernate. dialect. MySQLDialect </property>
<! -- Mapping files -->
<Mapping resource = "User. hbm. xml"/>
</Session-factory>
</Hibernate-configuration>
The <session-factory> element tells hibernate to connect to the database using the jndi defined in resin. The <mapping> sub-element is used to describe the configuration file path of each ing database table,
It declares that User. hbm. xml is a Hibernate XML ing file, which corresponds to the persistence class User. This file contains metadata that maps POJO classes to database tables (or multiple database tables. We will be back later to read this file. Let's write this POJO class first and then declare its ing metadata.
9. Create a user table in mysql in the following format:
User_id Password Nick_name E_mail
1 6666 hamal hamal@sohu.com
2 6666 vampire vampire@sina.com
3 6666 ande ande@yahoo.com
Create three java files in the resin3 mydomainWEB-INFclasses Directory: Test. java, HibernateUtil. java, and User. java.
The source code of HibernateUtil. java is as follows: This class is a helper class used to obtain a static SessionFactory. SessionFactory is responsible for a database and corresponds to only one XML configuration file (hibernate. cfg. xml ).
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) 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 ();
}
}
The source code of User. java is as follows: Hibernate converts a common Java Object (Plain Old Java Objects, or POJOs, also known as Plain Ordinary Java Objects) into a persistence class. A POJO is similar to a JavaBean, and its attributes are accessed through the getter and setter methods, which hides the internal implementation details.
Public class User {
Private Integer id;
Private String nick;
Private String password;
Private String email;
Public User (){
}
Public Integer getId (){
Return id;
}
Public void setId (Integer id ){
This. id = id;
}
Public String getNick (){
Return nick;
}
Public void setNick (String nick ){
This. nick = nick;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}
Public String getEmail (){
Return email;
}
Public void setEmail (String email ){
This. email = email;
}
}
The source code of Test. java is as follows:
Import javax. naming .*;
Import net. sf. hibernate .*;
Import java. util .*;
Public class Test {
Void Test (){
}
Public static void insert (){
Try {
Session hSession = HibernateUtil. currentSession ();
Transaction tx = hSession. beginTransaction ();
User newp = new User ();
Integer id = new Integer ("4 ");
Newp. setId (id );
Newp. setNick ("love ");
Newp. setPassword ("123 ");
Newp. setEmail ("test@sohu.com ");
HSession. save (newp );
Tx. commit ();
HibernateUtil. closeSession ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
10. Compile The Hibernate XML ing file
Create a User. hbm. xml file in the resin3 mydomainWEB-INFclasses Directory. The file content is as follows:
<? 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 = "User" table = "user">
<Id name = "id" type = "java. lang. Integer">
<Column name = "user_id" SQL-type = "Integer" not-null = "true"/>
<Generator class = "assigned"/>
</Id>
<Property name = "password" type = "java. lang. String">
<Column name = "password" SQL-type = "varchar (20)"/>
</Property>
<Property name = "nick" type = "java. lang. String">
<Column name = "nick_name" SQL-type = "varchar (50)"/>
</Property>
<Property name = "email" type = "java. lang. String">
<Column name = "e_mail" SQL-type = "varchar (30)"/>
</Property>
</Class>
</Hibernate-mapping>
Simple description:
<Class name = "User" table = "user"> The name attribute in the element represents the full path name of the User class (in the form of package name + class name ), the table attribute indicates the database table name mapped to the User class.
<Id name = "id" type = "java. lang. Integer"> The element represents the primary key of the table. The name attribute represents the class property name corresponding to the User class.
The <column name = "user_id" SQL-type = "Integer" not-null = "true"/> element represents the user_id field in the database user table corresponding to the id attribute.
11. Interface test:
Create a test. jsp file in the resin3mydomain directory as follows:
<% @ Page contentType = "text/html; charset = gb2312" %>
<Html>
<Head>
<Title> This is a test! </Title>
</Head>
<Body>
This is a test!
<% Test. insert (); %>
</Body>
</Html>
12. Test
Now all the preparations have been completed. Let's start the test.
Start the rensinserver with the file resin3binhttpd.exe. Double-click the File.
Before the test, we can see the following content in the mysql user table.
User_id Password Nick_name E_mail
1 6666 hamal hamal@sohu.com
2 6666 vampire vampire@sina.com
3 6666 ande ande@yahoo.com
Open IE and enter http: // localhost: 8080/test. jsp in the address bar.
This is a test! Then, we can view the database content as follows:
User_id Password Nick_name E_mail
1 6666 hamal hamal@sohu.com
2 6666 vampire vampire@sina.com
3 6666 ande ande@yahoo.com
4 123 love test@sohu.com
Congratulations, you have completed this example!