码迷,mamicode.com
首页 > Web开发 > 详细

SSM的XML和WEB.XML的配置

时间:2018-04-23 21:42:36      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:orm   repos   sch   没有   拷贝   渲染   map   epo   class   

显示层(handler/controller):

  request请求到springmvc的前端控制器,从处理器映射器找相应的handler(用@RequestMapping("  ")标注,映射成功后,由Springmvc生成一个handler对象,该对象中有一个方法,即映射成功的该方法),由相应的处理器适配器去执行该handler,handler中调用的是业务控制层(service)的方法接口。然后返回jsp地址的字符串或有地址和请求参数的ModelAndView对象(其中装载着参数如name,id,和目标地址即相应的显示页面如jsp,看了部分源码,是用Map存储,然后放到request对象中)到前端控制器,然后前端控制器把ModelAndView传给视图解析器,加上解析器中设置的jsp地址的前缀和后缀,然后把视图返回给前端控制器,再进行视图的渲染(好像就是把map中数据填充的request对象中),返回给客户端。

业务控制层(service):

  一个service接口,还有其相应的实现类serviceImpl,这样做可以使业务控制层的开发和显示层的开发并行进行,因为只要给显示层一个service的接口即可。service层的实现类中用Spring的IOC(Autowired注解)自动注入了一个或多个mapper对象,即该对象是调用sqlSessionFactory的getSession的getBean方法获得的。然后再调用mapper对象的相应方法,需要的时候还应该加入适当的控制流程(比如BeanUtils.copyProperties()方法进行属性的拷贝或业务的校验)。

持久层(dao/mapper):

  用逆向工程生成了相应的mapper.java即相应的mapper.xml,还有和数据库表对应的pojo,这些可以实现比较简单的单表查询。

  如果有多表关联的查询,则需要自定义mapper,因为返回结果包括多个pojo中的属性,不建议直接在pojo中添加相应属性,而应该写一个继承某个pojo类的子类,然后在该子类中添加所需的其他pojo中的属性,这样返回类型即为该子类。其中查询的参数为QueryVo类,其中组合了该上述子类和其他子类的对象,需要什么参数就往里写什么对象。

 

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">
	
	<!-- 开启注解. 扫描 -->
	<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="com.***"></context:component-scan>
	
	
	<!-- 过滤掉js, jpg, png, css, 静态文件 -->
	<mvc:default-servlet-handler/>
	
	<!-- 开启mvc 
	<mvc:annotation-driven />
	-->
	
	 <!-- 第一步:  创建自定义日期转换规则 -->   
    <bean id="dateConvert" class="com.***.utils.DateConvert"/>
    
	
   
    <!-- 第二步: 创建convertion-Service ,并注入dateConvert-->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="dateConvert"/>
            </set>
        </property>
    </bean>
 
	
	<!-- 第三步:注册处理器映射器/处理器适配器 ,添加conversion-service属性-->
    <mvc:annotation-driven conversion-service="conversionService"  />
	
	
	
	<!--  
	开启mvc 
	<mvc:annotation-driven />
	<bean id="inter" class="com.***.inter.LoginInter"></bean>
	-->
	
	<!--  
	拦截器配置 
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>//拦截所有 
			<ref bean="inter"/>
		</mvc:interceptor>
	</mvc:interceptors>  
	-->
	       
	
	
	
	<!-- 地址解析器  -->
	<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 -->
	<!-- 用注解进行开发 -->
	<context:annotation-config></context:annotation-config>
	<!-- 注解扫描包 -->
	<context:component-scan base-package="com.***">
		<!-- 这里不加载Controller的bean -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<!-- 1. 数据源 -->
	<!-- 读取db.properties文件. 读取到数据库信息 -->
	<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.  创建sqlSessionFactory ==> mybatis核心配置文件的读取 -->
	<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.扫描mybatis的mapper接口路径 -->
	<!-- 这个bean可以把我们的mapper接口直接扫描到. 直接把接口扫描完. 注册到spring的bean中 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	 	<!-- 会在提供的base包下寻找interface .根据interface的名字. 将首字母小写生成这个接口所对应的bean -->
	 	<property name="basePackage" value="com.***.mapper"></property>
	 </bean>
	
	<!-- 4.事务处理 -->
	<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>
	
	<!-- 用注解处理事务
	<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><!-- mybatis的核心配置  -->
	<!-- mybatis的运行配置 -->
	<settings>
		<!-- 开启二级缓存 -->
		<setting name="cacheEnabled" value="true"/>
		<setting name="lazyLoadingEnabled" value="true"/>
		<!-- 如果这个属性是true,那么你的类中, 任何一个方法被执行.都要去加载属性, 
			这个时候懒加载是没有效果的.
		 -->
		<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">
	
	<!-- 读取除了mvc外的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
	<!-- 整个web容器的动向由这个监听器进行监听. 这个监听器可以监听项目的启动. 从而直接加载核心配置文件 -->
	<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>
	
	
	<!-- 自定义过滤器 -->
	
	 <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>
  	<!-- 给出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><!-- 当web容器加载的时候, 初始化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>

  

SSM的XML和WEB.XML的配置

标签:orm   repos   sch   没有   拷贝   渲染   map   epo   class   

原文地址:https://www.cnblogs.com/charlypage/p/8921913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!