A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 。泉哥哥 初级黑马   /  2019-3-21 15:40  /  843 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

随着web案例的结束,传统的java工程已经过去。辅导老师的一句忘记让大家进入许些轻松的时刻。同时学习也已经进入到全新的模式。SSM整合工程。对于我们来说,这是一个全新的概念。当然同时也让以后的编程更加简单。到发帖为止,我们学习SSM已经差不多20几天,现在学习到SSM综合练习。下面就把这段时间学习的个人感受跟大家分享一下。首先,SSM整合是指用spring整合web层的springmvc框架和dao层的mybaties框架,(原谅我记性不好,好像叫控制层和业务层)。利用spring的配置文件整合mybaties的核心配置文件(这里mybaties如果用注解开发就可以完全舍去他的配置文件,当然如果要使用xml开发还是需要的)。最后还有最重要的一点就是,在web的工程的xml配置在,一定要配置 listener 监听器来监听,启动项目加载spring的配置文件applicationcontext.xml文件(老师说一般标准就是这么叫,如有不同,见谅),并且还要配置service加载控制器DispatcherServlet。用来加载springmvc的核心配置文件springmvc.xml(这个不知道该怎么取名,反正我就这么叫)。最后还需要注意,如果需要用到spring-security,那么在listener那里需要一起配置出来,具体配置方法见下:
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:applicationcontext.xml,classpath*:spring-security.xml</param-value>
</context-param>这里需要提示,classpath*上面的*号好像不能省去(大家可以这叫试一下)。就下来就是事务的配置,需要注意,事务配置的时候,controller包不能扫两次,不然配置好的事务不会生效。最好的做法是在配置spring配置文件的时候,扫描dao的包,(这里老师的说法是加上有个配置,不扫描controller),如下:<!--配置扫描的包-->
<context:component-scan base-package="com.qul">
    <!--配置不扫描的包-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>然后在springmvc中,配置扫描直接配置到controller就可以:<!--开启扫描-->
<context:component-scan base-package="com.qul.web">
    <!--扫描service-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>这里注意,上面的包是com.qul,而下面的包是com.qul.web。意思是说这下面的包只会到web里面去扫描,这是不会再次扫描qul下的service和dao,所以不会存在重复扫描,这样配置的话,springmvc中的    <!--扫描service--><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>这段配置可以不要。当然,如果加了中间这段配置,那么springmvc中的包也可以写成com.qul(语文不好,字里的意思如果大家不明白可以自己试一下)。接下来是常用的spring配置文件:包含事务,mybaties用的注解:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置扫描的包-->
<context:component-scan base-package="com.qul">
        <!--配置不扫描的包-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--读取连接池配置文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <!--配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置sqlsession对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 传入PageHelper的插件 -->
<property name="plugins">
            <array>
                <!-- 传入插件的对象 -->
<bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">oracle</prop>
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!--配置扫描dao-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qul.dao"/>
    </bean>

    <!--配置事务-->
<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="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!--配置切面-->
<aop:config>
        <!--配置切点-->
<aop:pointcut id="p" expression="execution(* com.qul.service.impl.*.*(..))"/>
        <!--建立连接-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="p"/>
    </aop:config>

</beans>
然后是springmvc配置:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!--开启扫描-->
<context:component-scan base-package="com.qul.web">
        <!--扫描service-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

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

    <!--过滤静态资源-->
<mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/img/**" location="/img/"/>
    <mvc:resources mapping="/plugins/**" location="/plugins/"/>
    <!--开启注解支持-->
<mvc:annotation-driven/>
</beans>同时还需要在web.xml中配置:这里包含spring-security<!DOCTYPEweb-appPUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationcontext.xml,classpath*:spring-security.xml</param-value>
  </context-param>

  <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>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

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

  <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:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
个人感受:SSM确实让编程相对比较规范和简单,只要把配置文件写好就问题,后续就是一些逻辑判断。至于怎么去逻辑判断,每个人的具体方法不一样,这里不作发言。

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马