SSM construction notes

Posted by GreenCore on Mon, 10 Jan 2022 22:57:34 +0100

SSM construction notes

1. The components of SSM spring + spring mvc + mybatis

2. The tutorial uses Java 8, idea and Maven

3. Start the tutorial:

​ 1. Use idea to create Maven project, note!!! You must select your configured Alibaba cloud maven, or the creation will fail

​ 2. If you see business success in the console, it is regarded as a success. If the creation fails, please refer to the first article. The loading speed of the first creation will be slow. 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>${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>

<!--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-->

<!-- File upload start-->
<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.3.1</version>
</dependency>
<!-- File upload end-->

​ 4. Fill in the following code in properties

<spring.version>4.0.6.RELEASE</spring.version>
<jackson.verson>2.9.1</jackson.verson>

​ 5. Create a Java source folder (blue) and a resources (gray with three bars) resource folder under the main file

​ 6. Create the following package under the Java resource file:

  • 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 tries to create an object. When the content displayed in the page does not match the implementation class, use vo class)
        • Entity (entity class, which is mainly responsible for the consistency of attribute and database field names in Java)

    7. Create the following files in 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 the data source and session factory of the project)

    ​ 3.applicationContext-mvc.xml (spring mvc configuration file, which mainly configures MVC direction related content, such as trying to parse, static resource configuration, etc.)

    ​ 4.db.properties (this is mainly used to store the resource configuration related to the database)

    ​ 5.logback.xml (here is the information about the configuration log)

    8. Open the web XML replaces its header information

    <?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>
    </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">
    
        <!-- todo Enable annotation scanning-->
        <context:component-scan base-package="cn.kgc"/>
    
        <!--todo Import external profile-->
        <context:property-placeholder location="classpath:db.properties"/>
    
        <!-- todo When reading the configuration file, be sure to add it when reading the configuration file path classpath-->
        <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.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
        <!-- todo Configure session factory-->
        <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- todo Reading the mapping file here also needs to be added classpath-->
            <property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"/>
            <!-- todo Set alias for entity class-->
            <property name="typeAliasesPackage" value="cn.kgc.entity"/>
        </bean>
    
        <!-- todo scanning dao layer-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="cn.kgc.dao"/>
        </bean>
    
        <!-- todo spring In charge of affairs management-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!-- todo Since this exercise does not involve addition, deletion and modification, declarative transactions are not written-->
    </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">
    
        <!-- todo Enable annotation scanning-->
        <context:component-scan base-package="cn.kgc"/>
    
        <!-- todo Configuration attempt parser-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!--todo Dimension control layer-->
        <mvc:annotation-driven conversion-service="conversionService2"/>
    
        <!--todo Configure static resources-->
        <mvc:resources mapping="/statics/**" location="/statics/"/>
    
        <bean id="conversionService2" class="org.springframework.context.support.ConversionServiceFactoryBean">
            <property name="converters">
                <set>
                    <bean class="cn.kgc.util.DataConverter"/>
                </set>
            </property>
        </bean>
    
        <!-- Profile upload-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="defaultEncoding" value="utf-8"/>
            <property name="maxUploadSize" value="5201314"/>
            <property name="maxInMemorySize" value="520"/>
        </bean>
    </beans>
    

    12.db.properties

    jdbc.driver=com.mysql.jdbc.Driver If it is mysql 8 Version needs to be added cj as com.msql.cj.jdbc.Driver
    jdbc.username=Own database account
    jdbc.password=Own database password
    jdbc.url=jdbc:mysql://localhost:3306 / database name? Characterencoding = utf-8&usessl = false --- followed by the time zone
    

4. Introduction of notes in the project

​ 1.@Service - mainly used on the implementation class of the service layer to mark that the current layer is a service

​ 2.@Controller - mainly used in the controller layer, marking the current class as the control layer

​ 3.@RequestMapping -- these knots can be used on classes or methods. The function is that the marked methods can be used to process network requests

​ 4.@ResponseBody - mainly used in methods to tell the front-end controller to return data directly to the front-end without going to the parser

​ 5.@RequestParam - used to mark the control layer parameters. If the parameter name passed by the front end is inconsistent with the control layer parameter name, this annotation can be used to keep it consistent, or if the parameter requirements are mandatory, this annotation can also be used to emphasize it

​ 6.@PathVariable - used to get parameters from the path

​ 7.@Slf4j -- used on classes. log can be used for all classes of the current class

​ 8.@Resource - used to inject data types

​ 9.@Param - use this annotation to distinguish parameters when there are more than two parameters in the mybatis dao layer

Topics: Java Maven Spring SSM