SSM build notes!

1.SSM component Spring + SpringMvc + Mybatis
2. This tutorial implements the construction of SSM based on idea and maven

3. Start:
      1. Create a maven web project using idea Note: you must select the configured Maven environment {with alicloud}
      2. Check whether the project structure is normal and whether the word "build success" appears in the console. If not, it will be regarded as a creation failure
      3. Find POM. For project XML (project dependency management file) and write the following code, which must be in dependencies

      <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>
    <!-- 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>${spring.version}</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>
    <dependency>
      <groupId>org.hibernate.validator</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.0.13.Final</version>
    </dependency>
    <dependency>
      <groupId>org.jboss.logging</groupId>
      <artifactId>jboss-logging</artifactId>
      <version>3.4.2.Final</version>
    </dependency>
    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>2.0.1.Final</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-->
  </dependencies>

    4. Find POM XML and fill in the following code
        <spring.version>4.0.6.RELEASE</spring.version>
    <jackson.verson>2.8.7</jackson.verson>    
    5. Select the main folder to create java source files and resources configuration folders and mark them as corresponding types
    6. Create the following in the java source file

  cn
       kgc
            ssm
Common (store all public classes in the project, such as unified return objects)
dao (storing dao layer interface, named * Mapper.xml)
Service (store all business logic interfaces in the project)
Impl (store the specific implementation method of the interface)
Controller (the control layer is responsible for calling the service layer to implement business calls)
Util (tool class, such as pagination tool class and date format conversion)
Entity (entity class is mainly responsible for one-to-one correspondence between mysql fields and java attributes)
vo (view object view object. When the required fields in the page conflict with the entity class, vo can be used to solve it)
    7. Create the following in the resources folder:
    resources
mappers([folder] stores all mapper mapping files. The mapping files here should correspond to dao layer interface one by one)
        applicationContext. XML (spring mybatis configuration file, mainly responsible for annotation scanning, object creation and other services of the whole project)
        applicationContext-mvc. XML (configuration file in spring MVC direction, mainly responsible for file format, date format, message converter and other configuration contents)
        db.properties (database information configuration file)
        logback.xml (logging configuration file)

   8. Edit web XML [global maximum 1 configuration file, which will be loaded first when the project is started]
    1. Change the profile header information because el expressions are not supported in 2.3

<?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">

2. Write the following code

<!--  Update context startup-->
  <listener>
<!--    todo When the project starts, the container starts running-->
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
<!-- to configure spring The container loading configuration file must have the correct name written here-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>
<!--    to configure spring mvc Related content-->
  <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>
  <!--Configure the filter. If the page is garbled when transmitting data, configure this filter to be easy to use-->
  <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>
<!--    It must be written here/*Filter all requests-->
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  9.applicationContext.xml configuration file

<?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">

<!--    todo Enable annotation scanning-->
    <context:component-scan base-package="cn.kgc"/>
<!--    todo The profile is imported due to mvc Consolidation, so add classpath To determine the fixed location of the file-->
    <context:property-placeholder location="classpath*:db.properties"/>
<!--    todo Configure data sources-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
    </bean>
<!--    todo Configure session factory-->
    <!--by dao Layer services sql Statement query-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sessionFactory">
        <!--todo Request to configure the data source for the session factory-->
        <property name="dataSource" ref="dataSource"/>
        <!--todo Configure the alias of the entity class [Of little use]-->
        <property name="typeAliasesPackage" value="cn.kgc.ssm.entity"/>
<!--        todo to configure mapper The location of the mapping file. Here, the fixed location is also used to find the configuration file-->
        <property name="mapperLocations" value="classpath*:Mappers/*Mapper.xml"/>
    </bean>
<!--    todo scanning dao Layer interface-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.kgc.ssm.dao"/>
    </bean>
<!--    todo Configure transaction management-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
<!--    todo Since there is no database addition, deletion or modification in this example, declarative transactions are not written-->
</beans>

10.applicationContext-mvc.xml configuration file

<?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">

<!--    todo Start annotation scan-->
    <context:component-scan base-package="cn.kgc"/>
<!--    todo Dimension control layer-->
    <mvc:annotation-driven conversion-service="conversionService2"/>
<!--    todo Configure static resources-->
    <mvc:resources mapping="/statics/**" location="/statics/"/>

<!--&lt;!&ndash;todo Global exception handling&ndash;&gt;-->
<!--    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">-->
<!--&lt;!&ndash;        todo Parameter name for configuration error message&ndash;&gt;-->
<!--        <property name="exceptionAttribute" value="exception"/>-->
<!--&lt;!&ndash;        todo Configure a page that displays error messages&ndash;&gt;-->
<!--        <property name="defaultErrorView" value="error"/>-->
<!--    </bean>-->

<!--    todo Configure view parser-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

<!--    todo to configure date transformation-->
    <bean class="org.springframework.context.support.ConversionServiceFactoryBean" id="conversionService2">
        <property name="converters">
            <set>
                <bean class="cn.kgc.ssm.utils.DateConvert"/>
            </set>
        </property>
    </bean>

<!--    todo File upload configuration-->
    <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
<!--        Set the maximum file upload size-->
        <property name="maxUploadSize" value="40960000000"/>
<!--        Set file encoding format-->
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxInMemorySize" value="4096"/>
    </bean>

</beans>

The ssm configuration is now complete

4. Interpretation of notes in the project
    1.@Service injects the current class into the spring container as the service layer, which is used on the service implementation class
    2.@ The controller indicates that the current class is the control layer
    3.@RequestMapper is a powerful annotation that can modify a class or method. As long as it is marked, it can solve network requests and responses
    4.@Resource injects other classes [it is particularly easy to null pointer here, please pay attention to the scanning content]
    5.@ResponseBody indicates that the content returned by the current method does not go through the view parser
    6.@RequesrParam is mainly used to accept the parameters passed from the front end. It is mainly responsible for the mismatch between the current front-end parameter and the back-end parameter name. If a parameter in mvc is modified with this annotation, this parameter is considered to be required
    7.@PathVaribale is mainly used for restful style to obtain parameters from paths
 

Keywords: Java Maven Mybatis Spring SSM

Added by DJTim666 on Tue, 11 Jan 2022 02:24:20 +0200