ssm整合

基于maven的ssm(Spring,springMVC,Mybatis)整合

pom.xml配置文件 添加相应jar包
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.springmaven</groupId>
  <artifactId>springMybatis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springMybatis Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>


  <dependencies>


    <!-- 引入servlet-api -->
    <dependency>  
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
</dependency>
    <!-- 引入jsp-api -->
    <dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.2</version>
</dependency>

   <!-- 添加spring-context 的依赖 -->
    <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.3.9.RELEASE</version>
</dependency>
    <!-- 添加spring-JDBC -->
     <dependency> 
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>4.3.9.RELEASE</version>
</dependency>

  <!-- 添加阿里数据源 -->
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.5</version>
    </dependency>

    <!-- 添加oracle数据库驱动包 -->
    <dependency>
    <groupId>ojdbc</groupId>
    <artifactId>ojdbc</artifactId>
    <version>6.0</version>
    </dependency>
   <!-- 添加mybatis依赖 --> 
    <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.5</version>
</dependency>
<!-- 添加mybatis-spring 整合依赖 -->
    <dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.1</version>
</dependency>

<!-- 添加aop支持 -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.8.10</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.10</version>
</dependency>

<!-- 添加测试与spring的整合 -->

 <!-- 添加单元测试的依赖 -->
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
</dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>4.3.13.RELEASE</version>
   <scope>test</scope>
</dependency> 



<!-- 添加spring-mvc的支持 -->

 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.3.9.RELEASE</version>
</dependency>


<dependency>
  <groupId>javax.servlet.jsp.jstl</groupId>
  <artifactId>jstl-api</artifactId>
  <version>1.2</version>
</dependency>


  </dependencies>

  <build>
    <finalName>springMybatis</finalName>

    <!-- maven编译插件  解决jdk版本问题 -->
<!-- <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
</plugins> -->

  </build>
</project>
spring-*.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"
    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
    ">


    <!-- 包扫描   扫描除controller层的所有注解-->
     <context:component-scan base-package="com.soft.proxyTest">
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>
       <!-- 启用事务注解 -->
       <tx:annotation-driven transaction-manager="transactionManager"/>
      <!-- 数据源第一种方式-->
     <!-- 配置数据源dataSource     C3P0, DBCP, DRUID(阿里数据源),JNDI 

     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
     <property name="url" value="jdbc:oracle:thin:@192.168.2.247:1521:orcl"/>
     <property name="username" value="shop"/>
     <property name="password" value="shop"/>
     </bean>
   -->
     <!-- 数据源第二种方式-->
   <!-- 配置数据源   使用properties文件 
    <context:property-placeholder file-encoding="UTF-8" location="classpath:jdbc.properties"/>
      <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
     <property name="driverClassName" value="${dataSource.driverClassName}"/>
     <property name="url" value="${dataSource.url}"/>
     <property name="username" value="${dataSource.username}"/>
     <property name="password" value="${dataSource.password}"/>
     </bean>  
    -->
      <!-- 数据源第三种方式-->
    <!-- 配置数据源  使用properties文件       使用context标签的properties-overvide -->
    <context:property-override file-encoding="UTF-8" location="classpath:jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" />


     <!-- 整合SqlSessionFactory -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"/>
     <!-- 管理配置文件 -->
    <!--  <property name="configLocation" value="classpath:mybatis-config.xml"/> -->

     <!-- 管理映射文件 -->
     <property name="mapperLocations" value="classpath:com/soft/proxyTest/mapper/*.xml"/>
     </bean>

  <!-- 把持久层交给spring管理,扫描持久层接口 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <!-- <property name="basePackage" value="com.soft.springmybatis.dao"/>  -->
     <property name="basePackage" value="com.soft.proxyTest.dao"/> 
     <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
     </bean>

    <!-- 基于xml方式配置aop切面 -->
    <bean id="poi" class="com.soft.proxyTest.proxy.ProxyAll" /> 
    <aop:config>               
        <aop:pointcut expression="execution(* com.soft.proxyTest.servicesimpl.*.*(..))" id="p"/>
        <aop:aspect ref="poi">
            <!-- <aop:before method="beforeTest" pointcut-ref="p"/>
            <aop:after-returning method="afterReturning" pointcut-ref="p"/> -->
            <aop:around method="around" pointcut-ref="p" />
            <aop:after-throwing method="afterthrowing" throwing="th" pointcut-ref="p"/>
        </aop:aspect>   
    </aop:config>    


   <!-- 配置事务管理器     xml配置文件方式 -->
   <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="insert*" propagation="REQUIRED" rollback-for="java.long.Exception"/>
      <tx:method name="query*" propagation="REQUIRED"/>
      <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>
    <!-- 配置事务切面 -->
    <aop:config>
      <aop:pointcut expression="execution(* com.soft.proxyTest.servicesimpl.*.*(..))" id="pointadvice"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="pointadvice"/>
    </aop:config>


    <!-- 使用注解方式配置事务
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"/>
    </bean>
     -->
</beans>
spring-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:context="http://www.springframework.org/schema/context"
    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
    ">

    <!-- 扫描controller层 -->
    <context:component-scan base-package="com.soft.proxyTest.controller"/>

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

</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
  version="3.0">

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

  <!-- 添加spring容器-->
   <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring-*.xml</param-value>
   </context-param>

  <!-- 配置监听器 -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


   <!--配置spring-mvc-->
   <servlet>
   <servlet-name>spring</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring-mvc.xml</param-value>
   </init-param>
   <!-- 容器加载时启动 -->
   <load-on-startup>1</load-on-startup>
   </servlet>

  <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping> 


</web-app>

猜你喜欢

转载自blog.csdn.net/Cool_breeze_Rainy/article/details/79991621