Home  >  Article  >  Backend Development  >  Configuring hibernate2.1.2 connection mysql_PHP tutorial in resin3.0

Configuring hibernate2.1.2 connection mysql_PHP tutorial in resin3.0

WBOY
WBOYOriginal
2016-07-13 17:03:44854browse

Configure hibernate2.1.2 to connect mysql in resin3.0

Configure hibernate2.1.2 to connect to mysql in resin3.0

Author: hamal

Agreement:

resin3 represents the installation root directory of resin3.0

hibernate2 represents the installation root directory of hibernate2.1.2



1. Establish our web application root under resin3, such as resin3mydomain directory (the directory name of mydomain can be chosen arbitrarily, and can be configured in the configuration file later)

2. Create the resin3mydomainWEB-INFclasses directory and the resin3mydomainWEB-INFlib directory in the resin3mydomain directory.
These two directories correspond to the class loader search path of this web application context (resin3mydomainWEB-INFlib for jars, resin3mydomainWEB-INFclasses for class files). We call these two paths the application library class path (used to store the jar class library related to this application) and the context class path (used to store the class file and xml configuration file of this application).
There is also a path called the resin3lib directory, which we call the global library class path (the related jar class library stored on the resin3 server and shared 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. After that, 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 in the hibernate2lib directory to the resin3mydomainWEB-INFlib directory. If you are not sure which packages you need, you can refer to the hibernate2libREADME.txt file, or, to be simpler, we copy all the jar files in the hibernate2lib directory to the resin3mydomainWEB-INFlib directory.

5. Now we start to configure resin’s jdbc database connection pool. Modify resin3confresin.conf file

a) Search for webapps and replace webapps with our customized application directory mydomain.

b) Find the element and cancel the comment status of the element. The comments of this file use the HTML style comment method .

c) Modify the element to the following form



jdbc/mysql



& Lt; url & gt; jdbc: mysql: //192.162.125.3: 3306/mysql & lt;/url & gt;

                                                                                           
                                                                                                 


8

20

30s



In this way, the configuration of resin's jdbc connection pool is completed.

6. Copy the hibernate.properties, log4j.properties, and oscache.properties files in the hibernate2src directory to the resin3 mydomainWEB-INFclasses directory.

7. Since we are using the mysql database, modify the configuration of the mysql part in the hibernate.properties file and comment out the original default HypersonicSQL configuration. Comment configuration is to add the # symbol before the statement. Such as:

#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



What we need to modify is the following 3 lines:

url refers to the jdbc connection descriptor, the format is jdbc:mysql://database IP:port number/database name

username refers to the username used to log in to the database

passwordThis user’s password



8. Bind the database connection pools of hibernate and resin. Create a hibernate.cfg.xml file in the directory with the following content




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

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





& Lt; Property name = "Connection.datasource" & GT; Java: Comp/ENV/JDBC/MySQL & LT;/Property & GT;

                                                                                                                                                                                                                                                    true

& Lt; Property name = "dialect" & gt; net.sf.hibernate.dialect.mqldialect & lt;/propro & gt;

                                                                                                   
                                                                                         




The element tells hibernate to use jndi defined in resin to connect to the database. The sub-element is used to describe the configuration file path of each mapping database table,

It declares that User.hbm.xml is a Hibernate XML mapping file corresponding to the persistence class User. This file contains metadata that maps POJO classes to a database table (or multiple database tables). We'll come back to this document later. Let's write this POJO class first and then look at the mapping metadata that declares it.

9. Create a user table in mysql. The format of the table is 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

Create three new 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 an auxiliary class used to obtain a static SessionFactory. SessionFactory is responsible for a database and only corresponds to 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 turns ordinary Java objects (Plain Old Java Objects, POJOs, sometimes also called Plain Ordinary Java Objects) into persistent classes. A POJO is much like a JavaBean. Properties are accessed through getter and setter methods, hiding the internal implementation details from the outside.



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;

    }



}



Test.java 源代码如下:



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.  编写Hibernate XML映射文件

在resin3 mydomainWEB-INFclasses目录下新建User.hbm.xml文件,文件内容如下:






    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"

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







  

   

      

      

   




   

      

   




   

      

   




   

      











Simple description:

The name attribute in the element represents the full path name of the User class (that is, in the form of package name + class name), and the table attribute represents the mapping of the User class Database table name.

The element represents the primary key of the table, and the name attribute represents the corresponding class attribute name in the User class.

The element represents the user_id field in the database user table corresponding to the id attribute.





11. Interface test:

We create a new test.jsp file in the resin3mydomain directory as follows:



<%@ page contentType="text/html; charset=gb2312" %>





This is a test!





This is a test!

<%Test.insert();%>









12. Test

Okay, now that all the preparations have been done, let’s start testing.

We start the rensin server, the startup file is resin3binhttpd.exe, double-click the file.

The contents of the user table in mysql we saw before the test are 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

Now we open IE and enter http://localhost:8080/test.jsp
in the address bar
When This is a test! is displayed normally on the interface, we then check 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!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630945.htmlTechArticleConfigure hibernate2.1.2 to connect to mysql in resin3.0 Configure hibernate2.1.2 to connect to mysql in resin3.0 Author: Hamal convention: resin3 represents the installation root directory of resin3.0 hibernate2 represents hibernate2...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn