SSM integration configuration

Posted by mobtex on Tue, 08 Mar 2022 15:52:52 +0100

1. Guide Package

Spring related packages

  • Spring MVC related (core related packages will be imported together)
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    
  • Spring section
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    
  • Spring JDBC
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    
  • Spring testing
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.2.10.RELEASE</version>
        <scope>test</scope>
    </dependency>
    
  • Log package used by spring 5
    <!--    Log package    -->
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.14.1</version>
        <scope>test</scope>
    </dependency>
    

MyBatis related packages

  • MyBatis
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis</artifactId>
       <version>3.5.3</version>
    </dependency>
    
  • MyBatis Spring is used to integrate with Spring
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.3</version>
    </dependency>
    
  • mysql driver
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    
  • Druid database connection pool
    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.10</version>
    </dependency>
    
  • MyBatis reverse engineering
    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.7</version>
    </dependency>
    

Java Web related

  • servlet api
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    
  • jstl
    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>jstl</artifactId>
       <version>1.2</version>
    </dependency>
    

Test package

  • junit 5
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.7.0</version>
        <scope>test</scope>
    </dependency>
    

2. Configure web XML file

  • Register the IOC container, which is used for management work other than Spring MVC. Context param tells Java Web the path of xml file as a parameter; Add a listener to load the IOC container during initialization. Detailed description
    <!--  start-up Spring container  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!--  Simply put, it's in java web During initialization, the IOC Container loading in  -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
  • Configure DispatcherServlet. All requests are intercepted and uniformly deployed by DispatcherServlet. contextConfigLocation gives the IoC container configuration file address of Spring MVC. You can also put a springmvc servlet in the webapp\WEB-INF folder XML configuration file, so there is no need to set the address.
    <!--  Configure front-end controller  -->
    <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:springmvc-servlet.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 two filters. The first filter sets the byte encoding of all incoming requests and returned responses to utf-8; The second filter treats requests in advance as REST style requests.
    <!--filter-->
    <!--Chinese character encoding filter-->
    <filter>
        <filter-name>CharacterEncodingFilter</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>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--REST Style request processing filter-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

Configuration file of Spring IoC container

  • It is mainly configured in three parts: component scanning, MyBatis configuration and transaction configuration

Component scan

  • All bean s except the IoC container have a clear division of labor
    <!-- Scan all in the package bean,Except marked as Controller of   -->
    <context:component-scan base-package="com.du.ssm">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    

MyBatis configuration

  • First, configure the database connection pool, which uses Druid's database connection pool. Import information through external configuration file.
    <!--Configure database connection pool-->
    <context:property-placeholder location="classpath:dbconfig.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.jdbcUrl}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
  • Configure sqlSessionFactoryBean, Spring will recognize that it is the interface of FactoryBean, and then assemble the bean of sqlSession
    <!--  register sqlSessionFactory  -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <!--    to configure mapper File location    -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    
  • Configure mapper's scanner to automatically scan out all mapper interfaces under the package
    <!--Scan all under the classpath mapper Interface-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.du.ssm.mapper"/>
    </bean>
    
  • Open an implementation class of sqlSession to facilitate use when necessary. (generally, you don't need to use it. Just use Mapper directly. It has helped to inject automatically)
    <!--Configure a sqlSession Implement the class for a rainy day-->
    <bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSession">
        <constructor-arg ref="sqlSessionFactory"/>
        <constructor-arg name="executorType" value="BATCH"/>
    </bean>
    

Transaction configuration

  • Registration transaction manager
    <!--Configure transaction manager-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
  • Open transaction annotation
    <!--Open transaction annotation-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
  • Open the annotation based transaction and use the transaction in the form of xml configuration (the main necessary is to use the configuration type)
    <aop:config>
        <!-- Pointcut expression -->
        <aop:pointcut id="txPoint" expression="execution(* com.du.ssm.service..*(..))"/>
        <!-- Configure transaction enhancements -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    
    <!--How to configure transaction enhancement and transaction entry  -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- All methods are transaction methods -->
            <tx:method name="*"/>
            <!--with get All ways to start  -->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    

Spring MVC configuration file

  • Turn on component scanning and only scan Controller
    <!-- Scan all in the package bean,Except marked as Controller of   -->
    <context:component-scan base-package="com.du.ssm" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
  • Standard configuration
    <!--Two standard configurations  -->
    <!-- take springmvc Requests that cannot be processed are handed over to tomcat -->
    <mvc:default-servlet-handler/>
    <!-- Can support springmvc More advanced features, JSR303 Verification, fast ajax...Mapping dynamic requests -->
    <mvc:annotation-driven/>
    
  • Configure view parser
    <!--Configure the view parser to facilitate page return  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    

Topics: Mybatis Spring Spring MVC SSM