Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
preface
After learning the three frameworks of Spring, Spring MVC and MyBatis, we need to integrate them. Before writing business logic, we can first build a project framework and integrate and configure these three frameworks. The integrated framework is not only applicable to this project, but also can be modified on the basis of the subsequent development of other projects.
Involved Technology stack
The main tools and technology stacks used in the integration process are as follows:
- MyBatis 2.6.2
- Spring 5.2.7
- Spring MVC 5.2.7
- Tomcat 9.0.x
- MySQL 5.7.x
- OS: Windows 10 2004
- IDE: IntelliJ IDEA 2020.1
SSM project structure preparation
- The first step is to create a normal Maven project, and then add Web dependencies. For details, see my other article, Spring MVC instance creation. The project structure after creation is shown below:
- Next, set up the directory structure;
The directory structure is shown in the figure above. The built-in directory is not explained after the project is created. The functions of the newly created directory and package are explained as follows:
directory | function |
---|---|
src/main/java/xxx/pojo |
The entity class layer, generally corresponds to the table in the database, encapsulates the data as an object, used indao 和 service Layer to layer transmission |
src/main/java/xxx/dao |
Data access layer, generally used to encapsulate the database operation interface |
src/main/java/xxx/dto |
Data transfer layerforservice 和 controller Data transfer between layers |
src/main/java/xxx/controller |
Control layerWhere Spring MVC comes into its own is for Model and View interaction |
src/main/java/xxx/service |
Business logic interface layerEncapsulates the business logic interface |
src/main/java/xxx/service/impl |
Business logic implementation layer, to achieve the interface in the business logic interface layer, and general transaction control |
src/main/resources/spring |
Save Spring configuration filesdao ,service ,controller Three layers |
src/main/resources/mapper |
storedao Corresponding to interfaces in the layersql |
web/WEB-INF/jsp |
Store front-end displayjsp page |
web/resoruces |
This directory has three subdirectories for the static resources corresponding to the project |
- Importing the dependency packages required by the project is only required in the project because Maven is used for management
pom.xml
To add the corresponding dependency;
<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.cunyu</groupId>
<artifactId>book-manage-system</artifactId>
<version>1.0 the SNAPSHOT</version>
<dependencies>
<! -- 1. Unit Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<! -- 2. Log -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<! -- 3. Database -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<! -- 4.MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>At 2.0.5</version>
</dependency>
<! -- 4.Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<! -- 5.Spring core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.7. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.7. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7. RELEASE</version>
</dependency>
<! -- 6.Spring DAO -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7. RELEASE</version>
</dependency>
<! -- 7. Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.7. RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7. RELEASE</version>
</dependency>
<! -- 8.Spring test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7. RELEASE</version>
</dependency>
</dependencies>
</project>
Copy the code
- In general, if you add the dependencies in Step 4 to your project, it will work, but sometimes you may encounter static resource export problems, so it is best to add the following configuration as well
pom.xml
;
<! Static resource export problem -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
Copy the code
conclusion
Well, after the above preparations, a simple SSM framework is ready, and that concludes this article. Next, it’s time to put the SSM together.