Maven 构建SSM框架配置

        对于maven的项目构建和依赖管理在这里先不做详细配置介绍,之后应该会做详细的补充,今天先写写在maven聚合工程中对于SSM的配置整合(提到的其他配置暂不做说明),这里也会贴出整合之后的配置代码,整合过程一笔带过:


整合思路:1、Dao层的整合   2、service层的整合   3、表现层的整合


1、对于Dao层的整合,无非就是对数据库的操作配置,整合之前,在mybatis操作数据库的配置文件中(SqlMapConfig.xml)可以配置通过db.propertie配置数据库连接池(JDBC DBCP C3P0 Druid等),扫描mapper配置文件,

配置SqlSessionFactory,对pojo起别名等(使用逆向工程生成pojo和mapper不必要再起别名)

在spring整合mybatis后,对于mybatis的配置,我们只需在构建项目的dao层中的resourses中配置SqlMapConfig.xml文件即可(是否起别名看个人喜好或者使用逆向工程没必要起),因为对于数据库的操作被spring接管,所以我们在dao层中的resourses中配置applicationContext-dao.xml文件,在此文件中配置dataSource(通过db.properties),sqlSessionFactory,扫描mapper。到此,Dao层的整合大致完善。

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

    <typeAliases>

          <package name="com.xxx.xxx.pojo"/>

    </typeAliases>

</configuration>

applicationContext-dao.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

            xmlns="http://www.springframework.org/schema/beans"

            xmlns:context="http://www.springframework.org/schema/context"

            xsi:schemaLocation="http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-4.2.xsd ">

<!-- db.properties -->

<!-- 数据源 -->

<context:property-placeholder location="classpath:mybatis/db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">

          <property name="driverClassName" value="${jdbc.driver}" />

          <property name="url" value="${jdbc.url}" />

         <property name="username" value="${jdbc.username}" />

         <property name="password" value="${jdbc.password}" />

</bean>

<!-- 配置SqlSessionfactory -->

<bean class="org.mybatis.spring.SqlSessionFactoryBean">

         <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>

         <property name="dataSource" ref="dataSource"></property>

</bean>

<!-- 配置Mapper扫描 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

           <property name="basePackage" value="com.xxx.xxx.mapper"></property>

</bean>

</beans>


2、对于Service层的整合,就是通过spring对service的扫描,但为了脉络的清晰,我们在聚合工程中service层的sources中配置applicationContext-service.xml,在此配置文件中除了约束就是我们对service层的扫描。

其外由于service层是我们操作CURD的层面,所以在service层我们需要一个事务,进而我们就再次配置applicationContext-tx.xml(配置事务管理器,通知,切面等)

applicationContext-service.xml文件

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

            xmlns="http://www.springframework.org/schema/beans"

            xmlns:context="http://www.springframework.org/schema/context"

            xsi:schemaLocation="http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

            http://www.springframework.org/schema/context

            http://www.springframework.org/schema/context/spring-context-4.2.xsd ">

<context:component-scan base-package="com.xxx.xxx.service"></context:component-scan>

</beans>

applicationContext-tx.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

            xmlns:context="http://www.springframework.org/schema/context"                                                                                     xmlns:p="http://www.springframework.org/schema/p"

         xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

          xsi:schemaLocation="http://www.springframework.org/schema/beans                                                                               http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

<!-- 事务管理器 -->

<bean id="transactionManager"    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

           <!-- 数据源 -->

           <property name="dataSource" ref="dataSource" />

</bean>

<!-- 通知 -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

          <tx:attributes>

                <!-- 传播行为 -->

              <tx:method name="save*" propagation="REQUIRED" />

               <tx:method name="insert*" propagation="REQUIRED" />

              <tx:method name="add*" propagation="REQUIRED" />

              <tx:method name="create*" propagation="REQUIRED" />

              <tx:method name="delete*" propagation="REQUIRED" />

              <tx:method name="update*" propagation="REQUIRED" />

              <tx:method name="find*" propagation="SUPPORTS" read-only="true" />

             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />

             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />

          </tx:attributes>

</tx:advice>

<!-- 切面 -->

<aop:config>

      < aop:advisor advice-ref="txAdvice"       pointcut="execution(* com.xxx.xxx.service.*.*(..))" />

</aop:config>

</beans>


3、对于表现层的整合,我们体现在springmvc.xml中,其中有对controller的扫描;注解驱动加载处理器映射器,处理器适配器,视图解析器(配置前后缀可简化action返回某个jsp代码),配置拦截与放行,异常处理器,上传文件解析器等

springmvc.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<beans

           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

           xmlns="http://www.springframework.org/schema/beans"

            xmlns:context="http://www.springframework.org/schema/context"

            xmlns:mvc="http://www.springframework.org/schema/mvc"

           xsi:schemaLocation="http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-4.2.xsd

           http://www.springframework.org/schema/mvc

            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">

<context:component-scan base-package="com.xxx.xxx.controller"></context:component-scan>

<!--这个是我上传文件的路径properties文件-->

<context:property-placeholder location="classpath:spring/conf.properties"/>

<!-- 注解驱动可以自动加载最新的处理器映射器和处理器适配器 -->

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        < property name="prefix" value="/WEB-INF/"></property>

        <property name="suffix" value=".jsp"></property>

</bean>

<!-- 释放被拦截的静态资源 -->

<mvc:default-servlet-handler/>

<mvc:interceptors>

         <mvc:interceptor>

                <mvc:mapping path="/**"/>

                 <mvc:exclude-mapping path="/user/**"/>(窄划路径下放行)

                  <mvc:exclude-mapping path="/xxx/**"/>(窄划路径下放行)

                   <mvc:exclude-mapping path="/css/**"/>

                  <mvc:exclude-mapping path="/js/**"/>

                    <mvc:exclude-mapping path="/img/**"/>

                    <mvc:exclude-mapping path="/fonts/**"/>

         <bean class="com.xxx.xxx.interceptor.xxxInterceptor"></bean>(interceptor拦截)

         </mvc:interceptor>

</mvc:interceptors>

<bean class="com.jnmd.exception.HandlerException"></bean>(异常处理)

<!--文件上传-->

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <property name="maxUploadSize" value="512000"></property>

</bean>

</beans>


最后就是web.xml的配置,作为项目的核心配置,需要配置springmvc前端控制器DispatcherServlet,再通过指定springmvc在classpath的路径加载springmvc;还需加载spring容器并指定applicationContext在classpath下的路径;

另外,web.xml中还需配置解决post乱码的filter

web.xml文件

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name></display-name>

<welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!-- 可以解决POST乱码问题 -->

<filter>

        <filter-name>encoding</filter-name>

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

       <!-- 设置编码参是UTF8 -->

        <init-param>

              <param-name>encoding</param-name>

              <param-value>UTF-8</param-value>

       </init-param>

</filter>

<filter-mapping>

         <filter-name>encoding</filter-name>

          <url-pattern>/*</url-pattern>

</filter-mapping>

<servlet>

        <servlet-name>dispatcherServlet</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

                <param-name>contextConfigLocation</param-name>

                <param-value>classpath:spring/springmvc.xml</param-value>

       </init-param>

      <load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

           <servlet-name>dispatcherServlet</servlet-name>

          <url-pattern>/</url-pattern>

</servlet-mapping>

<!-- 两者相结合,可以将Spring配置web容器中 -->

<listener>

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

</listener>

<context-param>

        <param-name>contextConfigLocation</param-name>

      <param-value>classpath:spring/applicationContext-*.xml</param-value>

</context-param>

</web-app>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容