SSM construction notes

SSM construction notes

  1. Components of ssm: spring+spring mvc+mybatis

  2. This tutorial uses Java 8, idea, and maven

  3. Start the tutorial:

    1. Create a maven web project using idea. Note that you must select your configured Alibaba cloud maven, or the creation will fail

    2. If you see build success in the console, it will be regarded as a success. If the creation fails, please refer to the first article. The load is slow during the first creation. Please wait patiently

    3. After creation, find POM XML (project dependency management file) writes the following code in dependencies:

       <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.7.0</version>
          </dependency>
          <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.5</version>
          </dependency>
          <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
          </dependency>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
          </dependency>
          <!-- c3p0 Database connection pool start -->
          <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
          </dependency>
          <!-- c3p0 Database connection pool end -->
          <!-- jsp-servlet support start -->
          <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
          </dependency>
          <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
          </dependency>
          <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jstl-impl</artifactId>
            <version>1.2</version>
            <exclusions>
              <exclusion>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
              </exclusion>
              <exclusion>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
              </exclusion>
              <exclusion>
                <groupId>javax.servlet.jsp.jstl</groupId>
                <artifactId>jstl-api</artifactId>
              </exclusion>
            </exclusions>
          </dependency>
          <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
          </dependency>
          <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
          </dependency>
          <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
          </dependency>
          <!-- jsp-servlet support end -->
          <!-- Tag library start -->
          <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-spec</artifactId>
            <version>1.2.1</version>
          </dependency>
          <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.1</version>
          </dependency>
          <!-- Tag library end -->
          <!-- apache of commons Series package start -->
          <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
          </dependency>
          <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
          </dependency>
          <!-- apache of commons Series package end -->
          <!-- apache tool kit start -->
          <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
          </dependency>
          <!-- apache tool kit end -->
          <!-- spring start -->
          <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.0.6.RELEASE</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-websocket</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc-portlet</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-messaging</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument-tomcat</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
          </dependency>
          <!-- spring start -->
          <!-- mysql -->
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
          </dependency>
          <!-- mybatis start -->
          <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.0.5</version>
          </dependency>
          <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.0.1</version>
          </dependency>
          <!-- mybatis end -->
          <!--Log management start-->
          <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
            <scope>compile</scope>
          </dependency>
          <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
            <scope>compile</scope>
          </dependency>
          <!--Log management end-->
          <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
          <!--getter setter annotation start-->
          <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
          </dependency>
          <!--getter setter annotation end-->
          <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
          </dependency>
          <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
          </dependency>
          <!--json start-->
          <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
          </dependency>
          <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
          </dependency>
          <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.verson}</version>
          </dependency>
          <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.verson}</version>
          </dependency>
          <!--json end-->
      
    4. Fill in the following code in properties

      <spring.version>4.0.6.RELEASE</spring.version>
          <jackson.verson>2.8.7</jackson.verson>
      
    5. Create java (blue) source files and resources (gray with three bars) resource folders under the main folder

    6. Create the following packages in the java source folder:

      • cn
        • kgc
          • ssm
            • common (store public files in the project, such as unified return classes)
            • controller (control layer in the project)
            • dao (data access layer in the project)
            • service (business logic layer interface in the project)
              • impl (implementation class of business logic layer interface in the project)
            • util (tool classes in the project, such as paging tool class and date conversion class)
            • vo (view Object, view Object, vo class is used when the display content in the current page is inconsistent with the entity class)
            • Entity (entity class, mainly responsible for one-to-one correspondence between attributes and database fields in java)
    7. Create the following folder under the resources folder:

      1. mappers (the distribution center of sql mapping files, where the xml files should correspond to the interfaces in the dao layer one by one)
      2. applicationContext.xml (spring configuration file, which mainly configures data sources and session factories in the project)
      3. applicationContext-mvc.xml (spring mvc configuration file, which mainly configures MVC direction related contents, such as view parser, static resources and message converter)
      4. db.properties (this is mainly used to store the resource configuration related to the database)
      5. logbak.xml (here is the information about the configuration log)
    8. Open web XML changes its header information because 2.3 does not support the execution of EL expressions

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      
    9. On the web Write the following code in XML

          <!--  Listening context loading -->
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          <!--  Context initialization, configuration spring configuration file  -->
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath*:applicationContext.xml</param-value>
          </context-param>
          <!--  to configure spring mvc-->
          <servlet>
              <servlet-name>springmvc</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>classpath*:applicationContext-mvc.xml</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
          </servlet>
          <servlet-mapping>
              <servlet-name>springmvc</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      
          <!--  Set a filter, which is mainly used to solve the problem of Chinese garbled code  -->
          <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>
              <init-param>
                  <param-name>forceEncoding</param-name>
                  <param-value>true</param-value>
              </init-param>
          </filter>
          <filter-mapping>
              <filter-name>encodingFilter</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>
      
    10. 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"
             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">
      
          <!--  Enable annotation scanning-->
          <context:component-scan base-package="cn.kgc"></context:component-scan>
      
          <!--  When reading the configuration file, be sure to add classpath*-->
          <context:property-placeholder location="classpath*:jdbc.properties"/>
      
          <!--  Configure data sources-->
          <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
              <property name="driverClass" value="${jdbc.driver}"/>
              <property name="jdbcUrl" value="${jdbc.url}"/>
              <property name="user" value="${jdbc.user}"/>
              <property name="password" value="${jdbc.password}"/>
          </bean>
      
          <!--  Configure session factory-->
          <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
              <property name="dataSource" ref="dataSource"></property>
              <!--  The same is true here when reading the mapping file. You need to add classpath-->
              <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>
              <!--  Set alias for entity class-->
              <property name="typeAliasesPackage" value="cn.kgc.ssm.entity"/>
          </bean>
      
          <!--  scanning dao layer-->
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <property name="basePackage" value="cn.kgc.ssm.dao"/>
          </bean>
      
          <!--  spring  In charge of transaction processing-->
          <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
              <property name="dataSource" ref="dataSource"></property>
          </bean>
      
          <!--  Since this exercise does not involve addition, deletion or modification, declarative transactions are not written -->
      	<!-- todo Implementing declarative transactions using annotations -->
          <tx:annotation-driven transaction-manager="transactionManager"/>	
      </beans>
      
    11. applicationContext-mvc.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"
             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 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
      
          <!--  Enable annotation scanning-->
          <context:component-scan base-package="cn.kgc"/>
      
          <!--  Configure view parser-->
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/WEB-INF/jsp/"/>
              <property name="suffix" value=".jsp"/>
          </bean>
      
          <!--  Dimension control layer  -->
          <mvc:annotation-driven conversion-service="conversionService2"/>
      
          <!--  Configure static resources -->
          <mvc:resources mapping="/statics/**" location="/statics/"/>
      
          <!--  Message converter  -->
          <bean class="org.springframework.context.support.ConversionServiceFactoryBean" id="conversionService2">
              <property name="converters">
                  <set>
                      <bean class="cn.kgc.ssm.util.DataConveter"></bean>
                  </set>
              </property>
          </bean>
      
          <!--  Profile upload-->
          <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
              <property name="defaultEncoding" value="utf-8"/>
              <property name="maxUploadSize" value="4096000000"/>
              <property name="maxInMemorySize" value="4096"/>
          </bean>
      
          <!--  Global exception handling  -->
          <!--<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
              <property name="defaultErrorView" value="error"/>
              <property name="exceptionAttribute" value="e"/>
          </bean>-->
      
      </beans>
      
  4. Introduction to notes in the project

    1. @Service ---- it is mainly used on the entity class of the service layer to mark that the current layer is service
    2. @Controller ----------- it is mainly used in the controller layer, marking the current class as the control layer
    3. @RequestMapping - this annotation can be used on a class or a method. The purpose is that the marked method can be used to process network requests
    4. @ResponseBody - mainly used in methods to tell the front-end control layer to return data directly to the front-end page without using the view parser
    5. @RequestParam - used to mark the parameters of the control layer. If the parameter name passed by the front end is inconsistent with the parameter name of the control layer, this annotation can be used to keep it consistent, or this annotation can be used to emphasize when the parameter requirements are mandatory
    6. @PathVariable - used to get parameters from the path
    7. @Slf4j - used on the class. The whole class of the current class can use the log record
    8. @Resource - used to inject data types
    9. @Param - when there are more than two mybatis dao layer parameters, use this annotation to distinguish the parameters

Keywords: Mybatis Spring Spring MVC

Added by IwnfuM on Mon, 10 Jan 2022 21:41:09 +0200