SSM完结之整合

2021-08-16 17:10:20 浏览数 (1)

一、SSM整合

1.1、引入依赖

1.2、整体步骤

1.3、spring.xml

1.3.1、注解声明事务

代码语言:javascript复制
  <context:property-placeholder location="classpath:db.properties"/>

  <context:component-scan base-package="cn.linstudy"/>


  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >

    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
  bean>

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

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

    <property name="typeAliasesPackage" value="cn.linstudy.domain"/>

    <property name="mapperLocations" value="classpath:cn/linstudy/mapper/*.xml"/>
  bean>


  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.linstudy.dao"/>
  bean>


  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
  bean>


  <tx:annotation-driven transaction-manager="transactionManager" />

1.3.2、不使用注解

代码语言:javascript复制
  <context:property-placeholder location="classpath:db.properties"/>

  <context:component-scan base-package="cn.linstudy"/>


  <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >

    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
  bean>

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

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

    <property name="typeAliasesPackage" value="cn.linstudy.domain"/>

    <property name="mapperLocations" value="classpath:cn/linstudy/mapper/*.xml"/>
  bean>


  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.linstudy.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="get*" read-only="true"/>
      <tx:method name="select*" read-only="true"/>
      <tx:method name="query*" read-only="true"/>
      <tx:method name="count*" read-only="true"/>
      <tx:method name="list*" read-only="true"/>
      <tx:method name="*"/>
    tx:attributes>
  tx:advice>
  
  <aop:config>
    
    <aop:pointcut id="txPointcut" expression="execution(*cn.linstudy.service.impl.*ServiceImpl.*(..))"/>
    
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
  aop:config>

二、SS整合

    SS整合是Spring SpringMVC的无缝整合。

2.1、整合步骤

2.2、springmvc.xml

代码语言:javascript复制
  <context:component-scan base-package="cn.linstudy.controller"/>


  <mvc:annotation-driven/>

  <mvc:default-servlet-handler/>

  <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp"/>
  bean>


  <import resource="classpath:spring.xml"/>

2.3、web.xml

代码语言:javascript复制
  <filter>
    <filter-name>characterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>characterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>


  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
        
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

0 人点赞