IDEA uses maven to build SSM framework integration project (super detailed, worth a visit)



This article is a long one. I believe that after reading this article, you will at least get twice the result with half the effort. It is impeccable. Please read it patiently. 😀

Super detailed!

dried food! dried food! dried food!

Briefly introduce SSM

SSM (Spring + spring MVC + MyBatis), a more mainstream enterprise architecture scheme at present. The standard MVC design pattern divides the whole system into four layers: display layer, Controller layer, Service layer and Dao layer. Spring MVC is used to forward requests and manage views. Spring realizes business object management, and MyBatis is used as the data object persistence engine.

Construction process

1, Frame introduction

  • Spring

    Spring is an open source framework created by Rod Johnson. It is created to solve the complexity of enterprise application development. Spring uses basic JavaBean s to do things that previously could only be done by EJB s. 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.

  • SpringMVC

    Spring MVC is a lightweight web framework based on Java that implements the request driven type of MVC design pattern. It uses the idea of MVC architecture pattern to decouple the responsibilities of the web layer. Request driven refers to the use of request response model. The purpose of the framework is to help us simplify development.

  • MyBatis

    MyBatis is an excellent persistence layer framework, which supports customized SQL, stored procedures and advanced mapping. MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets. MyBatis can use simple XML or annotations to configure and map native information, and map interfaces and Java POJOs (plain ordinary Java objects) into records in the database.

2, Download Maven

  • Download address :

  • Unzip the downloaded installation package. I unzip the installation package in my directory. After unzipping, I create the Maven repository directory of the local warehouse.

  • Modify the maven configuration file, enter the maven folder directory just extracted, enter conf, and use the editor to open settings XML file, I use BBEdit here, and EditPlus can be used for window

  • After opening, configure the local warehouse path instead of the default maven warehouse. Configure the maven warehouse to download the domestic image source, otherwise the download is particularly slow, and sometimes the download of jar package fails, as shown in the following figure. Modify the path corresponding to your computer.

  • maven warehouse Path

  • Alibaba's image warehouse
<!-- Configuring alicloud's image warehouse can improve jar Download speed of package -->   
     <name>aliyun maven</name>

  • The download and configuration of maven are completed. Next, let's create the project.

3, Create Maven project

  • First open IDEA and click Create New Project

  • Find Maven and click Next

  • Fill in the corresponding information. Groupid is the unique identifier of the project organization and actually corresponds to the package structure of JAVA.

    Artifactid is the unique identifier of the project. The actual name of the corresponding project is the name of the project root directory.

  • After filling in, click Next directly

  • Continue to click Finish

  • Next, the completed project directory is displayed in front of us. You will find that there is no webapp in it.

  • Next, because we are creating maven project, we need to configure maven warehouse in IDEA. The interface of MacBook I use may be a little different from that of Windows, but the interface is functional. Click IntelliJ IDEA in the upper left corner to find Preferences, Click to enter

  • Enter the upper left corner of the setting interface to search 🔍 maven, enter the maven setting interface

  • be careful ⚠️ If you have to download maven

  • Next, click the ellipsis behind the yellow box area Find the maven folder downloaded from your computer, select it and click OK.

  • At this time, you can see the following figure. It has been set successfully, and my maven warehouse path will change accordingly. Because I configured my own settings XML file.

  • Next, we modify maven's default settings in IDEA The XML configuration file is your own local configuration file, which can be modified according to the operation steps in the figure.

  • Next, the project of idea needs to be manually configured. The MacBook I use may have a slightly different interface from Windows, but the interface is functional. Click File in the upper left corner to find Project Structure Click to enter project configuration

  • Click Project and select the JDK of your computer. I use jdk1 here 8. Select 8 - Lambdas

  • Next, configure Modules, click Modules, click your own project, and then click the "+" sign to pull down to find the Web.

  • Next, configure the Web, click the path under Deployment Descriptors, select it, and click the modify icon

  • Next, change the path to the path shown below and click OK

  • After modification, you will see that the above path has been modified successfully. Next, click the web resource directories icon below

  • Similarly, change the original path to the path shown in the figure below, and click OK

  • After the modification, you will see the following screen. See the yellow exclamation icon below and click create artificet in the lower right corner. Web expanded is automatically added to the Artifacet

  • Next, click Apply and click OK.

  • Return to the editing interface, as shown in the following figure. Compared with the screenshot of the newly created project, there are more webapp folders here, with a small blue circle and web XML, indicating that the creation was successful.

  • Screenshot of the newly created project

  • Screenshot of the configured project

4, Jar package to be introduced for Maven project

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""



		<!--introduce pageHelper Paging plug-in PageInfo -->


		<!-- springwebmvc -->

		<!-- return json String support -->

		<!-- Spring integration Jdbc -->


		<!--Spring-test spring Test package -->


		<!-- Spring Aspect oriented programming -->

		<!--MyBatis -->

		<!-- MyBatis integration Spring Adaptive package for -->

		<!-- Alibaba data source druid -->

		<!-- mysql Driver package -->

		<!-- servlet api package -->

		<!-- junit Test package -->

		<!-- Log package -->

		<!-- excel Batch import and export -->

		<!-- Alibaba fastjson json and java Quick conversion kit -->



5, Integrate SSM framework, relevant required configuration files and configuration items

  • web.xml

    Modify the web under WEB-INF under webapp xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""
    <!--  spring -->
    <!--  Listen to whether there are abnormal problems in the loaded configuration file -->
     <!-- prevent Spring Memory overflow listener -->  

    <!--  Front end controller -->


    <!-- spring A character encoding filter is provided Filter -->




  • Spring configuration file

    I call it beans_core.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

	<!-- Enable annotation mode -->
	<!-- Scan annotation package -->
	<context:component-scan base-package="*"></context:component-scan>

	<!-- Database connection pool object -->
	<!-- Ali druid Database connection pool -->
	<bean id="dataSource"
		class="" destroy-method="close">
		<!-- Basic database information configuration -->
		<property name="url" value="${url}" />
		<property name="username" value="${jdbc_username}" />
		<property name="password" value="${jdbc_password}" />
		<property name="driverClassName" value="${driverClassName}" />
		<property name="filters" value="${filters}" />
		<!-- Maximum concurrent connections -->
		<property name="maxActive" value="${maxActive}" />
		<!-- Number of initialization connections -->
		<property name="initialSize" value="${initialSize}" />
		<!-- Configure the timeout time for getting connections -->
		<property name="maxWait" value="${maxWait}" />
		<!-- Minimum number of free connections -->
		<property name="minIdle" value="${minIdle}" />
		<!-- How often is the configuration interval detected? Idle connections that need to be closed are detected in milliseconds -->
		<property name="timeBetweenEvictionRunsMillis"
			value="${timeBetweenEvictionRunsMillis}" />
		<!-- Configure the minimum lifetime of a connection in the pool, in milliseconds -->
		<property name="minEvictableIdleTimeMillis"
			value="${minEvictableIdleTimeMillis}" />
		<property name="validationQuery" value="${validationQuery}" />
		<property name="testWhileIdle" value="${testWhileIdle}" />
		<property name="testOnBorrow" value="${testOnBorrow}" />
		<property name="testOnReturn" value="${testOnReturn}" />
		<property name="maxOpenPreparedStatements"
			value="${maxOpenPreparedStatements}" />
		<!-- open removeAbandoned function -->
		<property name="removeAbandoned" value="${removeAbandoned}" />
		<!-- 1800 Seconds, that is, 30 minutes -->
		<property name="removeAbandonedTimeout"
			value="${removeAbandonedTimeout}" />
		<!-- close abanded Output error log when connecting -->
		<property name="logAbandoned" value="${logAbandoned}" />

	<!-- spring integration mybatis Tool classes used -->
	<bean id="sqlSessionFactory"
		<property name="dataSource" ref="dataSource"></property>
		<!-- Scan the files in our directory mapper.xml file -->
		<property name="mapperLocations"
		<!-- load mybatis Configuration file for -->
		<property name="configLocation"
		<!-- introduce pagehelper Paging plug-in -->
		<property name="plugins">
				<bean class="com.github.pagehelper.PageInterceptor">
					<property name="properties">

	<!-- Load our mapper xml and mapper Interface -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

		<property name="basePackage" value=""></property>

	<!--<import resource="classpath:beans_transaction.xml"></import> -->
	<!--<bean name="customerController" class ="com.ujiuye.cus.controller.CustomerController"></bean> -->

	<!-- introduce spring Transaction management provided -->
	<bean id="tm"
		<property name="dataSource" ref="dataSource"></property>

	<!-- transaction-manager: Value is spring Of the transaction manager provided id value -->
	<tx:advice id="myadvice" transaction-manager="tm">
			<tx:method name="add*" isolation="READ_COMMITTED"
				propagation="REQUIRED" />
			<tx:method name="save*" isolation="READ_COMMITTED"
				propagation="REQUIRED" />
			<tx:method name="delete*" isolation="READ_COMMITTED"
				propagation="REQUIRED" />
			<tx:method name="update*" isolation="READ_COMMITTED"
				propagation="REQUIRED" />
			<tx:method name="drop*" isolation="READ_COMMITTED"
				propagation="REQUIRED" />

			expression="execution(**(..))" id="pt" />
		<aop:advisor advice-ref="myadvice" pointcut-ref="pt" />

  • Spring MVC configuration file

    I name it spring MVC xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

	<context:annotation-config />
	<!-- Which packages are scanned -->
	<context:component-scan base-package="*" />

	<bean name="conversionService"
		<property name="converters">
				<bean class=""></bean>
				<bean class=""></bean>

	<!-- open springmvc Annotation operations for -->
		conversion-service="conversionService" />

	<!-- take dispatcherServlet Requests that cannot be processed are pushed to the default servlet implement -->
	<mvc:default-servlet-handler />

	<!-- view resolver  -->
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>


Instead of writing data access object (DAO) code manually using SqlSessionDaoSupport or SqlSessionTemplate, mybatis spring provides an implementation of dynamic proxy: MapperFactoryBean. This class allows you to directly inject the data mapper interface into your service layer bean. When using mappers, you can just call them like calling your Dao, but you don't need to write any code for Dao implementation, because mybatis spring will create a proxy for you.

  • MyBatis profile

    I name it sqlmapconfig xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-// Config 3.0//EN"

	<!-- No other configuration is required for the time being--> 


  • Log profile
# Global logging configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

  • Database configuration file
validationQuery:SELECT 'x'


  • All configuration files have been configured, and the project directory is shown in the figure below.

6, Importing Jar package and configuring tomcat project

  • Next, click File in the upper left corner to find Project Structure Click to enter the project configuration, find Libraries, enter the view, and you will find your POM The maven jar package imported by XML has been downloaded.

  • Continue to click on Artifacts and you will find Available There are also maven jar packages

  • Next, look at the operation steps in the figure

  • Right click the ssmDemo folder and click Put into Output Root

  • At this time, you can see the changes shown in the figure below

  • Then, all the jar packages have been deployed in the lib directory of WEB-INF. next, click Apply and then click OK.

  • Next, let's configure tomcat, click OK, enter the editing area and click "Add Config..."

  • Click the Add icon ➕

  • Pull down to find Tomcat Server and select Local to enter the interface shown in the figure below.

  • Next, edit the tomcat configuration and configure it according to the operation steps in the figure.

  • At this time, observe that tomcat has been configured.

7, Reverse engineering using MyBatis (Generator)

  • MyBatis Generator can automatically create entity classes, Mapper mapping files and DAO interfaces

  • There are three kinds of reverse engineering of MyBatis: command line, eclipse Plug-in and maven plug-in.

  • I use the maven plug-in here

  • Both Eclipse and IDEA can be used. Let's use IDEA in the tutorial here

In POM Add plugin to XML

            <id>Generate MyBatis Artifacts</id>
  • generatorConfig. The location of XML is Src / main / resources / mybatis generator / generatorconfig XML, you can adjust the path yourself.

  • generatorConfig.xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-// MyBatis Generator Configuration 1.0//EN"

	<classPathEntry location="/Users/wht/maven/mavenrepository/mysql/mysql-connector-java/5.1.41/mysql-connector-java-5.1.41.jar"/>

	<context id="DB2Tables" targetRuntime="MyBatis3">

			<property name="suppressAllComments" value="true"></property>

		<!-- Set database connection -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"

			<property name="forceBigDecimals" value="false" />

		<!-- generate bean Storage location -->
		<javaModelGenerator targetPackage=""
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />

		<!-- Generated mapper File location -->
		<sqlMapGenerator targetPackage=""
			<property name="enableSubPackages" value="true" />

		<!-- Generated mapper.xml The corresponding one mapper Storage location of interface -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="" targetProject="./src/main/java">
			<property name="enableSubPackages" value="true" />

		<!-- Set reverse generated table -->
		<table tableName="test"></table>

  • After configuration, see the figure below

  • Click mybatis generator: generate to automatically generate entity classes, Mapper mapping files and DAO interfaces, as shown in the following figure.

  • I'm used to reverse generated * * mapper The XML file is placed in the mapper folder under mybatis in the configuration file resources, as shown in the following figure.

  • The code generated by reverse engineering has been completed. Isn't it Nice 😄, Go and have a try

8, Establish Service layer and conrorller layer

Service layer interface



public interface TestService {
    Test login(String name, String password);

Service layer interface implementation class


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class TestServiceImpl implements TestService  {

    private TestMapper testMapper;
    public Test login(String name, String password) {
        return testMapper.login(name, password);

Controller layer


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Map;

public class TestController {

    private TestService testService;

    public String login(String name ,String password){
        Test login = testService.login(name, password);
        if (login != null ) {
            return "redirect:/index.jsp";
        } else {
            return "redirect:/login.jsp";


The database is created by ourselves. I won't create it in detail here. I simply wrote a login page here

Remember to create a database and change the database name of the database configuration file to the name you created

9, Run test

  • At this time, run tomcat and the browser will load automatically. If not, access the URL

  • http://localhost:8080/ssmDemo

  • You will see the following page, log in your name and password to verify login.

  • The project structure is as follows:


So far, the whole SSM project has been perfectly integrated. If you have any needs, you can continue to write on this basis. If you need any questions, you can comment below. thank you 🙏

I hope it can help more scholars 💪, You can continue to learn a lot of follow-up articles 😁 Don't worry, there are more secrets and interesting code waiting for you to explore!

Share Hi, pass on virtue, point a star, and then the road of exploration will not be confused!

Scan the QR code on wechat, follow my original official account, and you can view the secrets of more programs!

Tags: Java

Posted by Eugene on Tue, 17 May 2022 08:33:50 +0300