美文网首页
SSM(Spring/SpringMVC/MyBatis)整合

SSM(Spring/SpringMVC/MyBatis)整合

作者: 我是邱邱 | 来源:发表于2018-09-11 19:29 被阅读0次

1.在pom.xml中引入所需要的spring、springmvc、MyBatis依赖包

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.qianfeng</groupId>
  <artifactId>empsys_myBatis</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>empsys_myBatis Maven Webapp</name>
  <url>http://maven.apache.org</url>
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>4.3.5.RELEASE</spring.version>
    </properties>
  <dependencies>
    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- 添加sevlet支持 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <!-- 必须使用provided 发布时不提供该包 -->
            <scope>provided</scope>
        </dependency>
        <!-- 添加jsp支持 -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- 添加jstl支持 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- 添加log4j日志 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency> -->

        <!-- 添加spring支持 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- 添加mybatis支持 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>

        <!-- jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.2</version>
        </dependency>


        <!-- c3p0 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>


  </dependencies>
  <build>
    <finalName>empsys_myBatis</finalName>
    <plugins>
            <!--引入一个tomcat插件,  -->
            <plugin>
              <groupId>org.apache.tomcat.maven</groupId>
              <artifactId>tomcat7-maven-plugin</artifactId>
              <version>2.2</version>
              <configuration>
                <uriEncoding>UTF-8</uriEncoding>
                <path>/empsys_myBatis</path>
              </configuration>
            </plugin>
            
            <!--通过该插件指定使用编译的版本  -->
            <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version> 
            <configuration>
                <!--默认编译是1.5,所以要设置编译的版本  编译的时候使用1.8  --> 
                <source>1.8</source> 
                <target>1.8</target> 
                <compilerVersion>1.8</compilerVersion>
                <encoding>UTF-8</encoding> 
            </configuration> 
         </plugin>
        </plugins>
  </build>
</project>

2.其次同时也是最重要的web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<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" id="WebApp_ID" version="3.1">
  <display-name>rrr</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>
 
    <!-- post 方式的中文乱码解决 -->
  <filter>
    <filter-name>characterEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- springMVC的核心配置 -->
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--为了加载springMVC的配置文件  -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!-- classpath指的是WEB-INF/classes -->
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <!--随着tomcat服务器启动的时候创建对象  -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <!--访问静态资源时,因为路径可以匹配上,就会访问DispatcherServlet,但是DispatcherServlet他默认情况下不处理静态资源,所以报404  -->
    <!--所以使用/之后,要处理静态资源无法访问的问题  -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 引入spring的配置文件  -->
    <!-- spring的核心配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 设置spring的配置文件的路径 -->
        <param-value>classpath:spring-bean.xml</param-value>
        <!-- <param-value>/WEB-INF/classes/bean.xml</param-value> -->
    </context-param>

    <!-- 配置HiddenHttpMethodFilter,将 表单中的post转为 put or delete提交方式 -->
  <filter>
      <filter-name>HiddenHttpMethodFilter</filter-name>
      <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>HiddenHttpMethodFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>


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

3.其三,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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
        <!--配置访问路径 调用默认的映射器HandlerMapping -->
        <!--springmvc只扫描controller层的包 比如@Controller这些注解  -->
        <context:component-scan base-package="com.qianfeng.controller">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
        
        <!--
            默认的适配器已经过时,需要重新载入新的适配器以及映射器    使用一下代码会默认加载
            RequestMappingHandlerMapping 映射器
            和
            RequestMappingHandlerAdapter  适配器
            默认的为DefaultAnnotionHandlerMapping和AnnotationMethodHandlerAdapter
         -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!--对于资源的url路径的配置   -->
        <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
        <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
        <mvc:resources location="/" mapping="/**"></mvc:resources>
        
        
        <!--视图解析器  跳转到相关资源的时候,只写资源的名字就可以了 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
   
   </bean>
</beans>

4.spring-bean.xml配置文件,数据库的操作交给spring-bean.xml中的MyBatis操作:

<?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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
  
    <!-- 注解扫描  扫描指定包及其子包下的注解 -->
   <context:component-scan base-package="com.qianfeng">
        <!-- 排除扫描的注解 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   </context:component-scan>
  
   <!-- 数据源的配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/employeesys"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="5"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxIdleTime" value="1000"></property>
    </bean>
    
    <!-- 创建mybatis的会话工厂对象 -->
   <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 设置数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载mybatis主配置文件 -->
        <property name="configLocation" value="classpath:mybatis.xml"/>
        <!-- 加载映射文件 -->
        <property name="mapperLocations" value="classpath:com/qianfeng/mapper/*.xml"/>
        
   </bean>
   
   <!-- 扫描映射代理的接口类,注入到spring容器中 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
        <property name="basePackage" value="com.qianfeng.dao"></property>
   </bean>
   
    <!-- 1配置事务管理类 -->
    <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="dataSource"></property>
    </bean>
   
    <!-- 2配置事务的特性 -->
   <tx:advice id="txAdvice" transaction-manager="txManage">
        <tx:attributes>
            <!-- 针对使用事务的add开头的方法 
            read-only 是否只读,true 是,false 可读可写
            如果有插入等操作,设为为true,运行程序会报异常-->
            <tx:method name="add*" read-only="false" propagation="REQUIRED"/>
            <tx:method name="delete*" read-only="false"  propagation="REQUIRED"/>
            <tx:method name="update*" read-only="false"  propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true"  propagation="REQUIRED"/>
            <!-- 针对其余的方法 -->
            <tx:method name="*" propagation="NOT_SUPPORTED"/>
            
        </tx:attributes>
   </tx:advice>
   
   <!-- 3AOP配置 -->
   <aop:config>
        <!-- 切入点 -->
        <aop:pointcut expression="execution(* com.qianfeng.service.*.*(..))" id="pc"/>
   
        <!-- 通知 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
   
   </aop:config>
   
</beans>

5.还有MyBatis的配置:

<?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>
    
    <settings>
        <!-- 两个一块设置才会起作用 
        启用懒加载,必须按照下面方式配置 -->
        <setting name="lazyLoadingEnabled" value="true"/>
        <!-- 将积极加载改为消极加载即按需加载。必须写,且为false才会懒加载   -->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    
    <!--设置别名  alias 唯一  可以随便写-->
    <typeAliases>
        <typeAlias type="com.qianfeng.entity.Employee" alias="Employee"/>
    </typeAliases>
    <!-- 数据库连接的操作已经交给spring-bean.xml配置了,所以这里就不用进行配置了 -->
</configuration>

配置好之后,然后就可以写dao层、service层、controller层 ,dao层用MyBatis、service层用spring框架以及事务,controller层用springmvc

相关文章

网友评论

      本文标题:SSM(Spring/SpringMVC/MyBatis)整合

      本文链接:https://www.haomeiwen.com/subject/irnegftx.html