search
HomeBackend DevelopmentPHP TutorialConfiguring hibernate2.1.2 connection mysql_PHP tutorial in resin3.0

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



br />
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文件,文件内容如下:





br />
    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:









This is a test!





This is a test!











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
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.