The clearest SSM integration

Spring, SpringMVC, and Mybatis frameworks integrate related configurations

The three layers are configured separately

Maven’s POM files depend on the resource export configuration

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <! -- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <! -- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>
        <! -- Database driven -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <! -- Database connection pool -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <! --Servlet - JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <! --Mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>

        <! --Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9. RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9. RELEASE</version>
        </dependency>

        <! --Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
    </dependencies>
	<build>
        <! -- Build resources to prevent us from exporting resources -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
Copy the code
  1. The first is the overall configuration file, ApplicationContext.xml
<? xml version="1.0" encoding="UTF-8"? > <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>

</beans>
Copy the code
  1. Spring-mvc.xml is configured for the MVC layer

      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <! -- configure SpringMVC -->
    <! --> < p style = "margin-bottom: 0em; margin-bottom: 0em; margin-bottom: 0em;
    <mvc:annotation-driven />
    <! -- 2. Default servlet configuration for static resources -->
    <mvc:default-servlet-handler/>

    <! --> < p style = "text-align: center;
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <! -- 4. Scan web-related beans -->
    <context:component-scan base-package="tech.zhjweb.controller" />

</beans>
Copy the code
  1. The web.xml configuration

      
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="true">

    <! --DispatcherServlet-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <! -- load the total configuration file -->
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <! --encodingFilter-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>
            org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/ *</url-pattern>
    </filter-mapping>

    <! --Session expiration time -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

</web-app>

Copy the code
  1. The Servics layer configures spring-service.xml

      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <! -- Scan the beans related to the service -->
    <context:component-scan base-package="tech.zhjweb.service" />

    <! --BookServiceImpl is injected into the IOC container -->
    <bean id="BookServiceImpl" class="tech.zhjweb.service.BookServiceImpl">
        <property name="bookMapper" ref="bookMapper"/>
    </bean>

    <! -- Configure transaction Manager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <! -- Inject database connection pool -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <! -- AOP object Support -->

</beans>
Copy the code
  1. The Dao layer configulates spring-dao.xml

      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <! -- configure integrated mybatis -->
    <! -- 1. Associate database files -->
    <context:property-placeholder location="classpath:database.properties"/>

    <! Database connection pool -->
    <! Hikari: --> database connection pool DBCP semi-automatic operation can not automatically connect to C3P0 automatic operation (automatically load configuration files and set them to objects)
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <! -- Configure connection pool properties -->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <! -- c3P0 connection pool private property -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <! -- do not commit automatically after closing the connection -->
        <property name="autoCommitOnClose" value="false"/>
        <! -- Obtain connection timeout -->
        <property name="checkoutTimeout" value="10000"/>
        <! -- Number of retries when obtaining a connection failure -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

    <! --> SqlSessionFactory --> SqlSessionFactory
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <! -- Inject database connection pool -->
        <property name="dataSource" ref="dataSource"/>
        <! -- Config MyBaties global config file: MyBaties -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <! --> < span style = "box-sizing: border-box; style =" box-sizing: border-box;
    <! - explanation: https://www.cnblogs.com/jpfss/p/7799806.html-- >
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <! -- insert sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <! Dao interface package to scan -->
        <property name="basePackage" value="tech.zhjweb.dao"/>
    </bean>

</beans>
Copy the code
  1. The Mybatis layer configures Mybatis -config.xml

      
<! DOCTYPEconfiguration
        PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <! -- configure data source spring to do -->
    <typeAliases>
        <package name="tech.zhjweb.pojo"/>
    </typeAliases>

    <mappers>
        <mapper class="tech.zhjweb.dao.BookMapper"/>
    </mappers>
</configuration>
Copy the code
  1. Database configuration database.properties
JDBC. Driver = com. Mysql. JDBC. After the driver # url parameter secure connection, note: for character MySQL8.0 to add timezone setting JDBC. Url = JDBC: mysql: / / localhost: 3306 / ssmbuild? useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=rootCopy the code
  1. Log configuration log4j.properties
Log4j. RootLogger = INFO, the Console, the File # define log output destination for Console log4j. Appender. The Console = org.. Apache log4j. ConsoleAppender . Log4j appenders. Console. Target = System. # out the flexibility to specify the log output format, The following line is to specify the specific format of the log4j. Appender. Console. The layout = org.. Apache log4j. PatternLayout Log4j. Appender. Console. Layout. ConversionPattern = % [c] - % m % n # File size to specified size to create a new File log4j. Appender. The File = Org, apache log4j. RollingFileAppender # log4j. The output directory appender. File. The File =. / logs/SSM log # definition File maximum size . Log4j appenders. File. MaxFileSize = 10 MB # output so log, Said if switch to the DEBUG output above the DEBUG level logs log4j. Appender. File. The Threshold = ALL log4j. Appender. File. The layout = org.. Apache log4j. PatternLayout  log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%nCopy the code