Maven based SSM framework integration example code

Basic concepts

1.1、Spring

Spring is an open source framework. Spring is a lightweight java development framework rising in 2003. It is derived from some concepts and prototypes described by rod Johnson in his book expert one on one J2EE development and design. It is created to solve the complexity of enterprise application development. Spring uses basic JavaBeans to do things that previously could only be done by EJBs. However, the use of spring is not limited to server-side development. From the perspective of simplicity, testability and loose coupling, any Java application can benefit from spring. Simply put, spring is a lightweight inversion of control (IOC) and aspect oriented (AOP) container framework.

1.2、SpringMVC

Spring MVC is a follow-up product of spring framework and has been integrated into spring web flow. Spring MVC separates the roles of controller, model object, dispatcher and handler object, which makes them easier to customize.

1.3、MyBatis

Mybatis was originally an open source project ibatis of Apache. In 2010, the project was migrated from Apache Software Foundation to Google Code and renamed mybatis. Mybatis is a Java based persistence layer framework. The persistence layer framework provided by ibatis includes SQL maps and data access objects (DAO). Mybatis eliminates the manual setting of almost all JDBC codes and parameters and the retrieval of result sets. Mybatis uses simple XML or annotations for configuration and original mapping, and maps the interface and Java POJOs (plain old Java objects) into records in the database.

Step 1: build a web project through Maven.

Step 2: import the POM file into the jar package

(1) pom. xml

I copied all my POM files. You should remove some initialization items and keep the one generated by your own POM.

Step 3: look at the overall project architecture. First of all, I don't have anything related to spring MVC here, because I first integrate spring mybatis to test whether it is successful. Success is in integrating spring MVC

Step 4: create model class

Step 5: create the database, userdao interface and mapper mapping file

(1) Create a very simple table

(2) Userdao interface

(3)UesrMapper.xml

Step 5: create userservice and userserviceimpl

(1)UserService

(2)UserServiceImpl

Step 6: create JDBC files and log files

(1)jdbc.properties

(2)log4j.properties

Step 7: integrate spring mybatis xml

Step 8: create a test class

After the above steps (log4j does not match or affect), we have completed the integration of spring and mybatis, so we can write a piece of test code to try whether it is successful.

The test class is established in Src / test / Java. If the test is successful, it means that spring and mybatis have been successfully integrated. The output information is printed to the console using log4j.

(1) Testmybatis test class

last! The key is to see the background output, which is also a time to witness a miracle. If you output an object, it means that your configuration and integration are successful!

Then let's start integrating spring MVC

Step 9: configure spring MVC xml

Step 10: configure web XML file

Here we face spring mybatis The introduction of XML and the configured spring MVC servlet is to complete SSM integration. The previous 2 framework integration does not need any configuration here.

Step 11 final test:

(1) Write login first jsp

Interface:

(2) Writing usercontroller

(3) Finally, susscss jsp

Perfect!

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>