Struts2 + Spring + Hibernate step by step 04 integrate Spring 2, verify the user name and password from the database

Source: Internet
Author: User

Note: Part of this series of articles is from the ssh Integrated Development tutorial written by instructor Wang Jian.

Using Spring AOP for project transaction management has become the first of all for many enterprises. As an excellent open-source project, Spring has shown its advantages in database connection and transaction management, of course, we will use Hibernate for data operations later, but we still cannot discard Spring as the middle layer.


Step 1: Write DDL statements to create mysql Databases and tables, as shown in-1:

Tip: Create an ssh. SQL file under the ssh project to store all the statements for creating databases and tables.


Figure-1

Step 2: create a resource file located in the WEB-INF/conf directory and enter the database url and other information, as shown in-2:

,

Figure 2

Step 3: copy the jar file connected to mysql to the WEB-INF/lib directory, due to the connection data path, later use AOP for transaction control, so you need to copy the following packages to the WEB-INF/lib directory.


Figure-3

Step 4: configure the database connection in applicationContext. xml, as shown in-4:


Figure 4

Step 5: Release the project and start the program. If the program can be started properly, the database connection is successful, and the program is debugged until it is started normally.

Step 6: Modify the OneDaoImpl. java class and run the data query. The modified code is as follows:


Figure 5

Step 7: In applicationContext. xml, inject the DataSource attribute to OneDaoImple, as shown in-6:


Figure 6

Step 8: Release the program and test logon. Enter the following information:


Figure-7

Step 9: Configure database transactions and advanced configurations. If you can master them, you can become an expert in the field:

Tip: During the above configuration process, although the database can be normally operated for addition, deletion, modification, and query operations, when the user encounters an exception, the user's operations cannot be rolled back to the original state, there is no transaction, so to ensure the atomicity of user operations, we should add a transaction to our data.

9.1 Add the Document Type header definition of applicationContext. xml to support the configuration of aop, as shown in-8:

Figure-8

Tip: to prevent errors, you can use the following document type headers to directly overwrite the Document Type headers of applicationContext. xml: (we recommend that you copy them directly)


  

9.2 configure transaction control by adding the following configuration to applicationContext. xml:


Figure-9

Tip: 1. Follow 1 ~ 3.

2. The green two-way arrow indicates the reference relationship.

3. execution (* com .. * Service. *(..)) transaction control is performed on all methods in all interfaces ending with a Service in the com package, regardless of what parameters are accepted and what data types are returned.


Step 10: Release the project and start the project. If the startup fails, check whether the configuration file is correct until the project can be started properly.


Conclusion: So far, Spring has been integrated. The next step is as follows:

1. added a function to display all users in the query.

2. Integrate Hibernate and complete the query to display all users.

3. added the functions of deleting, adding, and modifying users (using Hibernate ).


Source code:

Http://download.csdn.net/detail/u014548782/7216361



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.