Build Springmvc+mybatis project based on MAVEN (2)

Source: Internet
Author: User

Start from scratch based on MAVEN build Springmvc+mybatis project (2)

Tags: javamavenmybatisspring mvc

Copyright NOTICE: Welcome Reprint, Reproduced please keep the original link.

Following the above, this section describes the aggregation and inheritance of maven.

Read the portal from the beginning

In the internet age, software is becoming more and more complex, and developers often divide the software into modules to get a clear design, a good division of labor, and a higher level of reusability. The aggregation feature of MAVEN enables multiple modules to be built together and facilitates the configuration of sub-modules by inheriting the parent module's POM configuration. To demonstrate these features, the examples in this article are divided into data persistence layer petstore-persist and petstore-web two modules.

The following describes the process of creating a project, first creating a parent module, Perstore-parent.

Maven project, File, New, simplifies the process and simply tick create a simple project, i.e. skip archetype selection

Next, fill in the group ID and artifact ID, note that as the parent module, the package type (packaging) must select POM.

After completion, the directory structure is as follows.

The parent module has no substance other than the build file Pom.xml, so delete the src directory manually, keep only the Pom.xml file, and delete the following directory structure:

Next, create the Petstore-persist module. Right-click on the Petstore-parent, select New Project, and select Maven Module in the wizard dialog box, such as:

Next, still choose Skip Archetype option, module name fill petstore-persist

Click Finish (Finish) to complete the following directory structure:

Next create the Web Submodule, repeat "right-click on Petstore-parent, select New Project, select Maven Module" Step in the wizard dialog, but do not tick skip archetype selection. After filling in the module name, click Next.

Archetype Select Maven-archetype-webapp.

After completion, the directory structure is as follows:

In my development environment, the MAVEN Wizard created a Web module version of 2.3, and the runtime has some compatibility issues, first manually adjusted to 3.0.

Right-click Properties on the project and select the Project Facets menu. Selecting version 3.0 now prompts an error, and the workaround is to first remove the dynamic WEB module option, such as:

Click OK to close the dialog box and modify the Petstore-web/src/main/webapps/web-inf/web.xml as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "><display-name>petstore-web</display-name ></web-app>
From the new project facets interface, tick the dynamic WEB module option and select the version number 3.0

Then click the Apply button to save the settings.

Then switch to the Java Build Path menu and find here a hint of another error, such as:

The workaround is to check the JRE System library[javase-1.x], click OK to save the settings and close the dialog box, after processing the project has added a webcontent directory, which is superfluous for the MAVEN project and deleted directly.

Now to resolve the Red Cross error hint that exists on the Web module, view the details and find that "the superclass" Javax.servlet.http.HttpServlet "is not found on the Java Build Path", The description is missing a compiled jar reference. In an aggregation structure, dependencies are usually added to the pom.xml of the parent module, which is inherited by other sub-modules. There are two configuration methods, the first of which is the same as the normal project, and the sub-project automatically inherits these dependencies.

Petstore-parent/pom.xml

    <dependencies>          <dependency>              <groupId>junit</groupId>              <artifactId> junit</artifactid>              <version>3.8.1</version>              <scope>test</scope>          < /dependency>      </dependencies>

However, this approach is not flexible enough, and in large projects, there are often a lot of dependencies, and it is obviously unreasonable for a project to inherit all dependencies even if it is a very simple base module. So here's the second configuration, which uses the dependencymanagement element, which can either standardize the dependent versions used by each submodule, or allow the sub-modules to add dependencies on demand, but there are more configuration file codes.

Petstore-parent/pom.xml

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "><modelVersion>4.0.0</modelVersion><groupId> Com.example</groupid><artifactid>petstore-parent</artifactid><version>0.0.1-snapshot </version><packaging>pom</packaging><modules><module>petstore-persist</module ><module>petstore-web</module></modules><properties><springframework.version> 4.2.6.release</springframework.version></properties><dependencymanagement><dependencies ><dependency><groupId>junit</groupId><artifactId>junit</artifactId>< Version>4.12</version><scope>test</scope></dependency><dependency><groupid >javax</groupid><artifacTid>javaee-web-api</artifactid><version>7.0</version><scope>provided</scope> </dependency><dependency><groupid>log4j</groupid><artifactid>log4j</ Artifactid><version>1.2.17</version></dependency><dependency><groupid> javax.servlet</groupid><artifactid>jstl</artifactid><version>1.2</version></ Dependency><dependency><groupid>org.apache.commons</groupid><artifactid> commons-lang3</artifactid><version>3.1</version></dependency><dependency>< groupid>org.springframework</groupid><artifactid>spring-core</artifactid><version>$ {springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-context</artifactid><version>${ Springframework.version}</version></dependency><depEndency><groupid>org.springframework</groupid><artifactid>spring-test</artifactid> <version>${springframework.version}</version></dependency><dependency><groupId> org.springframework</groupid><artifactid>spring-jdbc</artifactid><version>${ Springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-web</artifactid><version>${ Springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-webmvc</artifactid><version>${ Springframework.version}</version></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-beans</artifactid><version>${ Springframework.version}</version></dependency><dependency><groupid>org.hibernate</groupid><artifactid>hibernate-validator</artifactid><version>5.2.4.final</ version></dependency><dependency><groupid>com.fasterxml.jackson.core</groupid>< artifactid>jackson-core</artifactid><version>2.7.3</version></dependency>< dependency><groupid>com.fasterxml.jackson.core</groupid><artifactid>jackson-databind</ artifactid><version>2.7.3</version></dependency><dependency><groupid>c3p0< /groupid><artifactid>c3p0</artifactid><version>0.9.1.2</version></dependency> <dependency><groupid>mysql</groupid><artifactid>mysql-connector-java</artifactid ><version>5.1.18</version></dependency><dependency><groupid>org.mybatis</ Groupid><artifactid>mybatis</artifactid><version>3.4.1</version></dependency> <dependency><groupid>org.mybatis</groupid><artifactid>mybatis-spring</artifactid><version>1.3.0</ Version></dependency></dependencies></dependencymanagement></project>
Petstore-persist/pom.xml

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "><modelVersion>4.0.0</modelVersion><parent>< groupid>com.example</groupid><artifactid>petstore-parent</artifactid><version>0.0.1 -snapshot</version></parent><artifactid>petstore-persist</artifactid><dependencies ><dependency><groupid>junit</groupid><artifactid>junit</artifactid></ Dependency><dependency><groupid>org.springframework</groupid><artifactid> spring-test</artifactid></dependency><dependency><groupid>org.springframework</ Groupid><artifactid>spring-core</artifactid></dependency><dependency><groupid >org.springframework</groupid><artIfactid>spring-context</artifactid></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-beans</artifactid></dependency>< Dependency><groupid>org.springframework</groupid><artifactid>spring-jdbc</artifactid ></dependency><dependency><groupid>c3p0</groupid><artifactid>c3p0</ Artifactid></dependency><dependency><groupid>org.mybatis</groupid><artifactid >mybatis</artifactId></dependency><dependency><groupId>org.mybatis</groupId> <artifactid>mybatis-spring</artifactid></dependency><dependency><groupid>mysql </groupid><artifactid>mysql-connector-java</artifactid></dependency></dependencies ></project>
Petstore-web/pom.xml

<?xml version= "1.0"? ><projectxsi:schemalocation= "http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "xmlns=" http://maven.apache.org/POM/4.0.0 "xmlns:xsi=" http://www.w3.org/ 2001/xmlschema-instance "><modelVersion>4.0.0</modelVersion><parent><groupId> Com.example</groupid><artifactid>petstore-parent</artifactid><version>0.0.1-snapshot </version></parent><artifactid>petstore-web</artifactid><packaging>war</ Packaging><name>petstore-web Maven webapp</name><url>http://maven.apache.org</url> <dependencies><dependency><groupid>junit</groupid><artifactid>junit</ Artifactid></dependency><dependency><groupid>javax</groupid><artifactid> javaee-web-api</artifactid></dependency><dependency><groupid>log4j</groupid>< Artifactid>log4j</artifactid></dependenCy><dependency><groupid>javax.servlet</groupid><artifactid>jstl</artifactid> </dependency><dependency><groupId>org.apache.commons</groupId><artifactId> commons-lang3</artifactid></dependency><dependency><groupid>org.springframework</ Groupid><artifactid>spring-core</artifactid></dependency><dependency><groupid >org.springframework</groupId><artifactId>spring-context</artifactId></dependency> <dependency><groupid>org.springframework</groupid><artifactid>spring-test</ artifactid></dependency><dependency><groupid>org.springframework</groupid>< Artifactid>spring-jdbc</artifactid></dependency><dependency><groupid> org.springframework</groupid><artifactid>spring-web</artifactid></dependency>< Dependency><groupid>org.springframework</grOupid><artifactid>spring-webmvc</artifactid></dependency><dependency><groupid >org.hibernate</groupId><artifactId>hibernate-validator</artifactId></dependency> <dependency><groupid>com.fasterxml.jackson.core</groupid><artifactid>jackson-core</ Artifactid></dependency><dependency><groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid></dependency><dependency><groupid>c3p0 </groupId><artifactId>c3p0</artifactId></dependency><dependency><groupId> mysql</groupid><artifactid>mysql-connector-java</artifactid></dependency>< dependency><groupid>org.mybatis</groupid><artifactid>mybatis</artifactid></ dependency><dependency><groupid>org.mybatis</groupid><artifactid>mybatis-spring< /artifactid></dependency> <dependency><groupid>com.example</groupid><artifactid>petstore-persist</artifactid ><version>0.0.1-SNAPSHOT</version></dependency></dependencies><build>< Finalname>petstore-web</finalname></build></project>
In the above configuration, dependencies in the submodule do not need to be added <version> elements, consistent with the settings in the parent module. Also, note that the dependency definitions for JUnit and Javaee-web-api in the main Pom have one <scope>test</scope> and one <scope>provided</scope> ,<scope> defines the scope of the dependency, with a total of 5 types:

Compile compile, test, run are valid test testing dependencies, only for the test, compile the main code and run the project can not use such a dependency provided for the compilation and testing is valid, run-time Invalid runtime test and run time, compile the main code is not valid, such as The JDBC driver system is consistent with the provided range and is associated with the native file path through the Systempath element for jar packages outside of the MAVEN library
Back in the project, the red warning disappears after adding a dependency, but there is one more step to do, or the Web module may not run. Right-click Maven, Update Project ... on the petstore-web.

Here, the project is complete. Let's write some test code to check to see if the project works. The final directory structure, such as the red box is the addition or modification of the code of the file, the source of each file packaging provided, no longer one by one pasted.

The results of the operation are as follows:

This section of the code download

Summarize

This section describes the creation of complex multi-module projects through MAVEN's aggregation and inheritance features, although it seems to be a bit cumbersome, but mastering is not a complex process. The next section begins the introduction of functions and integrations of SPRINGMVC and mybatis, including additions and deletions, and other functions.

Start from scratch based on MAVEN build Springmvc+mybatis project (1) Build Springmvc+mybatis project from scratch based on MAVEN (2) Build Springmvc+mybatis project from scratch based on MAVEN (3) Start from scratch based on MAVEN build Springmvc+mybatis project (4)

Build Springmvc+mybatis project based on MAVEN (2)

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.