Configuration of SSM XML and WEB.XML

Display layer (handler/controller):

  request requests to the front controller of springmvc, find the corresponding handler from the handler mapper (marked with @RequestMapping(" "), after the mapping is successful, a handler object is generated by Springmvc, and there is a method in this object, that is, the mapping is successful This method), the handler is executed by the corresponding handler adapter, and the method interface of the service control layer (service) is called in the handler. Then return the string of the jsp address or the ModelAndView object with the address and request parameters (which is loaded with parameters such as name, id, and the target address, that is, the corresponding display page such as jsp, after reading some source code, it is stored in Map, and then placed in request object) to the front controller, then the front controller passes the ModelAndView to the view parser, adds the prefix and suffix of the jsp address set in the parser, then returns the view to the front controller, and then renders the view ( It seems to be filling the request object with the data in the map) and returning it to the client.

Business control layer (service):

  A service interface, as well as its corresponding implementation class serviceImpl, can make the development of the business control layer and the development of the display layer in parallel, because only one service interface can be given to the display layer. The implementation class of the service layer automatically injects one or more mapper objects with Spring's IOC (Autowired annotation), that is, the object is obtained by calling the getBean method of getSession of sqlSessionFactory. Then call the corresponding method of the mapper object, and add appropriate control flow when necessary (for example, the BeanUtils.copyProperties() method for copying properties or verifying business).

Persistence layer (dao/mapper):

  The corresponding mapper.java, that is, the corresponding mapper.xml, and the pojo corresponding to the database table are generated by reverse engineering, which can realize a relatively simple single-table query.

  If there is a multi-table related query, you need to customize the mapper, because the returned result includes attributes in multiple pojos. It is not recommended to add the corresponding attributes directly to the pojo. Instead, you should write a subclass that inherits a pojo class, and then Add the required properties in other pojos to this subclass, so that the return type is the subclass. The parameter of the query is the QueryVo class, in which the objects of the above subclass and other subclasses are combined, and whatever parameters are needed, write whatever object into it.

 

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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	
	<!-- Enable annotation. Scan-->
	<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="com.***"></context:component-scan>
	
	
	<!-- Filter out js, jpg, png, css, static files-->
	<mvc:default-servlet-handler/>
	
	<!-- enable mvc
	<mvc:annotation-driven />
	-->
	
	 <!-- Step 1: Create a custom date conversion rule-->   
    <bean id="dateConvert" class="com.***.utils.DateConvert"/>
    
	
   
    <!-- Step 2: Create conversion-Service and inject dateConvert-->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="dateConvert"/>
            </set>
        </property>
    </bean>
 
	
	<!-- Step 3: Register the processor mapper/processor adapter, add the conversion-service attribute-->
    <mvc:annotation-driven conversion-service="conversionService"  />
	
	
	
	<!--  
	open mvc
	<mvc:annotation-driven />
	<bean id="inter" class="com.***.inter.LoginInter"></bean>
	-->
	
	<!--  
	Interceptor configuration
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>//Intercept all
			<ref bean="inter"/>
		</mvc:interceptor>
	</mvc:interceptors>  
	-->
	       
	
	
	
	<!-- Geocoder -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	

</beans>

  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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.3.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<!-- @Component, @Repository, @Service, @Controller, @Autowired, @Resources -->
	<!-- Develop with annotations-->
	<context:annotation-config></context:annotation-config>
	<!-- Annotation scan package-->
	<context:component-scan base-package="com.***">
		<!-- Controller's beans are not loaded here -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<!-- 1. Data source -->
	<!-- Read the db.properties file. Read the database information-->
	<context:property-placeholder location="classpath:db.properties"/>
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- 2. Create sqlSessionFactory ==> read mybatis core configuration file -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
	</bean>
	
	<!-- 3. Scan the mapper interface path of mybatis-->
	<!-- This bean can scan our mapper interface directly. Scan the interface directly. Register it in spring's bean -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	 	<!-- It will look for the interface under the provided base package. According to the name of the interface. Lowercase the first letter to generate the bean corresponding to this interface -->
	 	<property name="basePackage" value="com.***.mapper"></property>
	 </bean>
	
	<!-- 4. Transaction processing -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<tx:advice id="txManager" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
			<tx:method name="search*" read-only="true"/>
			<tx:method name="select*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="*"  isolation="DEFAULT" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut expression="execution(* com.***.*.*.*.*(..))" id="cut"/>
		<aop:advisor advice-ref="txManager" pointcut-ref="cut"/>
	</aop:config>
	
	<!-- Handle transactions with annotations
	<tx:annotation-driven transaction-manager="transactionManager"/>
 -->
</beans>

  mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- the core configuration of mybatis -->
	<!-- Mybatis running configuration -->
	<settings>
		<!-- Enable L2 cache-->
		<setting name="cacheEnabled" value="true"/>
		<setting name="lazyLoadingEnabled" value="true"/>
		<!-- If this property is true, then any method in your class is executed. To load the property,
			In this case, lazy loading has no effect.
		 -->
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>
	
</configuration>

  web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
	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_2_5.xsd">
	
	<!-- Read configuration files except mvc -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- The movement of the entire web container is monitored by this listener. This listener can monitor the startup of the project. To directly load the core configuration file -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
  
  <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>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	
	<!-- Custom Filters-->
	
	 <filter>
	
	<filter-name>LoginFilter</filter-name>
	<filter-class>com.***.filter.LoginFilter</filter-class>
	
	</filter>
	
	<filter-mapping>
	<filter-name>LoginFilter</filter-name>
	<url-pattern>/*</url-pattern>
	
	</filter-mapping>
	

	
	
  <servlet>
  	<servlet-name>springMVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- Give the path to spring -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext-mvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup><!-- When the web container loads, initialize spring -->
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>/</url-pattern><!-- 所有 -->
  </servlet-mapping>
  
  <listener>
  	<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
 
</web-app>

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324724526&siteId=291194637