您的位置:首页 > 其它

Maven整合SSM框架相关的配置文件

2018-02-07 15:53 681 查看
        关于Spring、SpringMVC、Mybatis三大框架各自所需要的框架配置文件有Spring的applicationContext、SpringMVC的Springmvc.xml以及Mybatis的mybatis.xml。本文讲述的SSM框架整合时,springmvc配置文件依然不变,而将mybatis配置文件整合到applicationContext.xml之中,这样可以节省配置文件,当然也可以采用多个配置文件,只需要最终都加载进applicationContext.xml文件中即可。最后这些配置文件都需要加载到web.xml文件,作为项目的一个全局配置。

1.配置applicationContext.xml

         当进行整合时,首先要理清各层之间的关系,Mybatis属于持久层获取数据库操作,这里涉及了DAO层,之前已经讲过Mybatis是通过mapper接口与mapper.xml映射实现接口的原子操作的。因此,需要对mapper接口与mapper.xml文件的映射需要进行控制。而applicationContext需要配置Mybatis的参数以及与底层数据库有关联的文件,比如mapper接口以及mapper.xml文件位置。1.配置扫描service层2.配置数据源3.配置扫描dao层4.配置扫描相应的映射机制以及扫描相应的mapper文件。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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!--配置扫描service层-->

<context:annotation-config />
<context:component-scan base-package="service包" />

<!-- 将jdbc.properties文件读取进来 -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 指定jdbc.properties存放的位置 -->
<property name="location" value="classpath:jdbc.properties"></property>
</bean>

4000
<!-- 配置数据源 -->
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 配置driver/url/username/password -->
<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>

<!-- 扫描保存sql语句的xml文件,相当于将原来mybatis配置文件中的数据源通过Spring容器的依赖注入直接与Spring完美整合 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="basicDataSource"></property>
<!-- 指定mapper.xml文件所在的包路径 -->
<property name="mapperLocations" value="classpath*:xx/xx/xx/*.xml"></property>
</bean>

<!-- 扫描dao层中的java接口类 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper接口所在的具体包"></property>
</bean>

</beans>        通过看以上配置文件,然后逐条进行分析:1.扫描service层,并支持注释机制,相当于在spring容器中开启了注释机制2.配置数据源,原来mybatis单独开发时,这部分是mybatis.xml文件的内容,但是框架整合时,将该内容配置到了applicationContext.xml文件中并通过Spring容器的依赖注入的特点将读取原本读取配置文件的步骤进行解耦。原来需要通过硬编码获取mybatis.xml的配置内容,而整合之后,没有硬编码耦合,而是通过Spring容器的依赖注入实现的。InputStream inputStream=Resources.getResourceAsStream("mybatis-config.xml");
//初始化mybatis,创建SqlSessionFactory类的实例。
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//创建Session实例
SqlSession session=sqlSessionFactory.openSession();3.并在配置数据源中指定mapper.xml文件的所在位置,原本mapper.xml文件的获取是通过在mybatis.xml文件中配置,并通过sqlSession的getMapper()方法可以获取相应的mapper接口。而这里还是通过Spring的优势避免了硬编码的耦合。以下是mapper文件在mybatis.xml中的配置。<mappers>
<mapper resource="mapper/UserMapper.xml"/>
<mapper resource="mapper/BookOfUserMapper.xml"/>
</mappers>4.扫描DAO层,将Service层和DAO层通过配置文件整合,这里扫描DAO层之后,通过在相应类中使用注释,然后Spring会实现自动装配,而无需硬编码传递对象关系。
        如上可以看出,applicationContext除了对自己spring框架相应的特性,还整合了mybatis框架的内容,比如数据源、事务机制等等。

2.springmvc.xml

        Springmvc.xml则是和之前springmvc层单独开发时的内容基本相同,springmvc需要扫描控制层,而前面讲述的spring扫描service层和dao层。另外如果controller层中的文件使用了注释的,则需要开启注释驱动。        springmvc的核心是DispatcherServlet,它负责为控制层返回的ModelAndView进行调度寻找相应的视图文件,所以配置视图解析器,这样才能将返回的model与相应的视图文件相联系。同时,springmvc中的其它一些内容也可以在这里进行配置,比如编码、文件上传、数据校验等等。只需要通过<bean/>元素进行添加配置内容即可。
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 
<!-- 配置controll层包扫描 -->
<context:component-scan base-package="controller层包" />

<!-- 配置springMVC特有的驱动 -->
<mvc:annotation-driven />

<!-- 配置视图解析器 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/pages/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=""></property>
</bean>

<!-- 配置json注解 -->
<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=utf-8</value>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>

<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置默认编码 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 上传文件的总大小 -->
<property name="maxUploadSize" value="10485760"></property>
</bean>

<!-- 数据校验 -->

<!-- 任务调度 -->

</beans>

3.web.xml

        其实,web.xml文件相当于一个统筹全局的作用,当applicationContext.xml文件和springmvc.xml文件完成之后,都需要加载到web.xml文件中,加载文件时需要配置一些类,比如先将application.xml文件加载进web.xml,并且需要配置监听器;接着配置springmvc核心控制器类即DispatcherServlet,并将spring和springmvc的配置文件都加载进去,相当于让DispatcherServlet作为一个调度的作用。最后需要将核心控制器类优先加载。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>dt36_ssm1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!-- 配置中文乱码问题 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 将编码改变为utf-8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 将applicationContext.xml文件 交给web.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置springMVC的核心控制器类 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 读取springmvc.xml文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 配置DispatcherServlet优先被启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
</web-app>

        以上便是ssm框架整合中的三大主要配置文件,其实这三个文件的格式比较固定,内容分工明确,所以只要掌握了一个项目的配置文件,其它项目的都可以以这个为模板。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: