SSM framework integration

Keywords: Big Data xml Mybatis Database JSON

SSM framework integration in general
Configure the web.xml file

<!-- read applicationContext-*.xml configuration file -->

  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext-jdbc.xml</param-value>
  </context-param>

<!-- Set encoding format -->

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

<!-- Distinguish springmvc.xml file -->

  <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>
<!-- Mark whether the container loads this at startup servlet , The higher the positive number, the higher the priority -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

<!-- org.springframework.web.context.ContextLoaderListener Class implements javax.servlet.ServletContextListener Interface. ServletContextListener Interface can monitor ServletContext Object's lifecycle, because each web Only one app ServletContext Object, so the interface actually listens to the whole web Application. -->

  <listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

Configure applicationContext.xml file

1. Read the dataBase.properties file

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="location">
		<value>classpath:database.properties</value>		
	</property>
</bean>

2. Connect to database

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${driver}"></property>
	<property name="url" value="${url}"></property>
	<property name="username" value="${username}"></property>
	<property name="password" value="${password}"></property>
</bean>

3. Declare the transaction and configure the property to connect to the dataSource database
Transaction: when operating on the database, it will be completed together if it is completed, and it will not be completed together if it is not completed
Four characteristics: atomicity, isolation, persistence and consistency

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" propagation="SUPPORTS"/>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="del*" propagation="REQUIRED"/>
			<tx:method name="up*" propagation="REQUIRED"/>
		</tx:attributes>
</tx:advice>

4. Configure sqlSession factory to reference data source component and load mybatis-config.xml file

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- Reference data source component -->
	<property name="dataSource" ref="dataSource"></property>
<!-- Quote mybatis configuration file -->
	<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

5. Scan mapper file

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.smbms.dao"></property>
</bean>

Configure mybatis-config.xml file

1. Declare entity class

<configuration>
	<typeAliases>
	   <package name="com.smbms.pojo"/>
	</typeAliases>
   </configuration>

Configure the springMvc.xml file

1. Configure < MVC: annotation driven / > tag to support annotation and configure message converter

 <mvc:annotation-driven>
    	<mvc:message-converters>
    		<bean class="org.springframework.http.converter.StringHttpMessageConverter">
    			<property name="supportedMediaTypes">
    				<list>
    					<value>application/json;charset=UTF-8</value>
    				</list>
    			</property>
    		</bean>
    	</mvc:message-converters>
    </mvc:annotation-driven>

2. Configure < MVC: resources mapping = "/ statistics / * *" location = "/ statistics / / / > tag to configure static file access

 <mvc:resources mapping="/statics/**" location="/statics/" />

3. Upload configuration support file

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   		 <property name="maxUploadSize" value="5000000"/>
   		 <property name="defaultEncoding" value="UTF-8"/>
    </bean>

4. Configure view resolver

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="favorParameter" value="true"></property>
	 	<property name="defaultContentType" value="text/html"></property>
	 	<property name="mediaTypes">
	 		<map>
	 			<entry key="html" value="text/html;charset=UTF-8"></entry>
	 			<entry key="json" value="application/json;charset=UTF-8"></entry>
	 			<entry key="xml" value="application/xml;charset=UTF-8"></entry>
	 		</map> 
	 	</property>
	 	<property name="viewResolvers">
	 		<list>
	 			<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 				<property name="prefix" value="/WEB-INF/jsp/"/>
					<property name="suffix" value=".jsp"/>
	 			</bean>
	 		</list>
	 	</property>
	</bean>

5. Configure date format conversion

<bean id="myConversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    	<property name="converters">
    		<list>
    			<bean class="com.smbms.tools.StringToDateConverter">
    				<constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
    			</bean>
    		</list>
    	</property>
    </bean>

6. Configure interceptors < MVC: interceptors >

<mvc:interceptors>
    	<mvc:interceptor>
    		<mvc:mapping path="/sys/**"/>
    		<bean class="com.smbms.interceptor.SysInterceptor"></bean>
    	</mvc:interceptor>
    </mvc:interceptors>

Posted by angel777 on Tue, 10 Dec 2019 09:52:43 -0800