Spring+SpringMvc+Mybatis框架集成搭建教程二(依赖配置及框架整合)

依赖导入以及框架整合

(1).打开项目的pom.xml文件,声明依赖

<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.hafiz.www</groupId>
  <artifactId>ssm</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ssm Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <spring.version>4.2.6.RELEASE</spring.version>
    <mybatis.version>3.2.8</mybatis.version>
    <mybatis-spring.version>1.2.2</mybatis-spring.version>
    <servlet.version>3.0.1</servlet.version>
    <druid.version>1.0.13</druid.version>
    <jsp-api.version>2.1</jsp-api.version>
    <jstl.version>1.2</jstl.version>
    <mysql.connector.version>5.1.38</mysql.connector.version>
    <aspectj.version>1.8.6</aspectj.version>
    <commons.fileupload.version>1.3.1</commons.fileupload.version>
    <jackson.version>2.5.4</jackson.version>
    <log4j.version>1.2.17</log4j.version>
    <slf4j.version>1.7.7</slf4j.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</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-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>${mybatis-spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>${aspectj.version}</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>${commons.fileupload.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>${servlet.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>${jsp-api.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>${jstl.version}</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>${druid.version}</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.connector.version}</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>ssm</finalName>
  </build>
</project>

(2).在项目的resources文件夹下新建jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.20.196:3306/dev?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=30
jdbc.minIdle=5

(3).在项目的resources文件夹下新建log4j.properties文件

### set log levels ###
log4j.rootLogger = ERROR ,  stdout , D , E , F

### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %l-%t:%r ] - [ %X{ip} %X{userName}]  %m%n

## 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.DatePattern = '.'yyyyMMdd
log4j.appender.D.File = logs/wap/warn.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = WARN
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.encoding=UTF-8
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %l-%t:%r ] - [ %X{ip} %X{userName}]  %m%n

## 保存异常信息到单独文件 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = logs/wap/error.log
log4j.appender.E.DatePattern = '.'yyyyMMdd
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.encoding=UTF-8
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %l-%t:%r ] - [ %X{ip} %X{userName}]  %m%n

## 保存异常信息到单独文件 ###
log4j.appender.F = org.apache.log4j.DailyRollingFileAppender
log4j.appender.F.File = logs/wap/debug.log
log4j.appender.F.DatePattern = '.'yyyyMMdd
log4j.appender.F.Append = true
log4j.appender.F.Threshold = DEBUG
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.encoding=UTF-8
log4j.appender.F.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %l-%t:%r ] - [ %X{ip} %X{userName}]  %m%n

(4).在项目的resources文件夹下新建mapper、spring、mybatis三个文件夹分别用来存放mybatis的mapper xml文件以及spring的配置文件以及mybatis的配置文件。

(5).在项目的resources文件夹下的mybatis文件夹下新建mybatis-config.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>

    <!-- 全局配置setting,根据需要进行配置 -->
    <!--<settings>
        <setting name="" value=""/>
    </settings>-->

    <!-- 设置别名 -->
    <typeAliases>
        <!-- 批量进行设置别名 -->
        <package name="com.hafiz.www.po"/>
    </typeAliases>


    <!-- 使用自动扫描器时,mapper.xml文件如果和mapper.java接口在一个目录则此处不用定义mappers -->
    <!-- <mappers>
        <package name="com.hafiz.www.mapper" />
    </mappers> -->

</configuration>

(6).在项目的resources文件夹下的spring文件夹下新建以下xml文件并进行整合配置

  创建spring的配置集中文件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"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-transaction.xml"/>
</beans>

  创建spring的dao层配置文件spring-dao.xml并进行以下配置

<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-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">
        
    <!-- 加载配置文件jdbc.properties中的内容,jdbc.properties中key的命名要有一定的特殊规则 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置数据源,数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <property name="minIdle" value="${jdbc.minIdle}" />
    </bean>
    
    <!-- 配置sqlSessionFactory交由spring进行管理 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 设置数据源引用 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载mybatis的全局配置文件路径 -->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    
    <!-- 配置mapper接口扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 要扫描的包的名称,有多个包时,包名之间用半角逗号隔开 -->
        <property name="basePackage" value="com.hafiz.www.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
   
</beans>

  创建spring的service层配置文件spring-service.xml并进行以下配置

<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-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!--配置组件扫描,只扫描Controller注解之外的组件-->
    <context:component-scan base-package="com.hafiz.www">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>

  创建spring的transaction事务相关配置文件spring-transaction.xml并进行以下配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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-3.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
        
        <!-- 事务管理器
            对mybatis操作数据库进行事务控制,spring使用jdbc的事务控制类
         -->
         <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
             <!-- dataSource在spring-dao.xml中进行了配置 -->
             <property name="dataSource" ref="dataSource"></property>
         </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="create*" propagation="REQUIRED"/>
                     <tx:method name="delete*" propagation="REQUIRED"/>
                     <tx:method name="update*" propagation="REQUIRED"/>
                     <tx:method name="find*" propagation="SUPPORTS"/>
                     <tx:method name="get*" propagation="SUPPORTS"/>
                     <tx:method name="select*" propagation="SUPPORTS"/>
                 </tx:attributes>
         </tx:advice>
         
         <!-- 定义aop切面 -->
         <aop:config>
             <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hafiz.www.service.impl.*.*(..))"/>
         </aop:config>
</beans>

  创建springMvc的配置文件spring-mvc.xml并进行以下配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
    <!-- 配置视图解析器
        解析jsp视图,默认使用jstl标签,classpath下得有jstl包
     -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 配置jsp文件的前缀和后缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!--配置注解控制器映射器-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
    <!--配置注解控制器映射器-->
    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
    
    <!-- 使用mvc:annotation-driven代替上边的 注解控制器适配器 和 注解控制器映射器 配置,
        mvc:annotation-driven默认加载了很多参数绑定方法,比如:
        json转换器就默认加载了,如果使用mvc:annotation-driven就不用再进行上面RequestMappingHandlerAdapter和RequestMappingHandlerMapping的配置
        实际开发过程中往往使用mvc:annotation-driven
    -->
    <!-- 扫描注解驱动,如:@Required, @Autowired, @PostConstruct, @PersistenceContext, @ Resource, @ PreDestroy.... -->
    <!--<context:annotation-config />-->
    
    <!-- 扫描mvc注解驱动,如:@RequestMapping, @Controller,@RequestBody,@ResponseBody.... -->
    <mvc:annotation-driven />
    
    <!-- 配置组件扫描,springmvc容器中只扫描Controller注解 -->
    <context:component-scan base-package="com.hafiz.www" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 配置全局异常处理器
        只要实现了HandlerExceptionResolver接口就是全局异常处理器类,每个系统只能有一个全局异常处理器类
     -->
     <!--<bean class="com.hafiz.www.exception.CustomExceptionResolver"/>-->
     
     <!-- 配置文件上传 -->
     <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置上传文件的最大尺寸为5MB -->
        <property name="maxUploadSize">
            <value>5242880</value>
        </property>
    </bean>
     
     <!-- 配置静态资源文件的解析,包括js、css、img.... -->
     <mvc:resources location="/js/" mapping="/js/**"/><!-- /**表示拦截文件夹下的所有文件,包括子路径 -->
     <mvc:resources location="/css/" mapping="/css/**"/>
     <mvc:resources location="/img/" mapping="/img/**"/>

</beans>

(6).在web.xml文件中配置spring listener 和springmv的DispatcherServlet以及log4j listener以及配置文件路径和POST 乱码Filter

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <display-name>pegasus-scm</display-name>

  <!-- param -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/spring.xml</param-value>
  </context-param>

  <!-- 配置log4j配置文件路径 -->
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>
  <!-- 60s 检测日志配置 文件变化 -->
  <context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>60000</param-value>
  </context-param>

  <!-- EncodingFilter -->
  <filter>
    <filter-name>encodingFilter</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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- listener -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <!-- 配置Log4j监听器 -->
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>

  <!-- spring mvc -->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <description>spring mvc config file</description>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

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

(7).在webapp下面创建js、css、img三个文件夹分别用来存放网站的脚本文件、样式文件以及图片文件

至此,Spring、SpringMvc、Mybatis的整合完毕!

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏java闲聊

Jib爬坑

我大天朝的网络还真不是盖的,没法子,使用VPN发现蓝灯这个梯子不怎么好使,还是没辙,大家梯子推荐一下吗?谢谢哈 github jib地址

17220
来自专栏Samego开发资源

web性能测压工具 | siege

30380
来自专栏JAVA技术站

log4j的使用配置 原

1.log4j.rootLogger = [level],appenderName,appenderName,...

10120
来自专栏程序猿DD

简化Swagger使用的自制Starter:spring-boot-starter-swagger,欢迎使用和吐槽

项目简介 该项目主要利用Spring Boot的自动化配置特性来实现快速的将swagger2引入spring boot应用来生成API文档,简化原生使用swag...

26560
来自专栏蓝天

编译hbase-1.2.3源代码

确保机器可以正常访问Internet,如能正常访问https://repo.maven.apache.org等,如果是代理方式则需要设置好eclipse和m...

24320
来自专栏weixuqin 的专栏

springMVC 学习笔记(一):spring 入门

springMVC 是 spring 框架的一个模块,springMVC 和 spring 无需通过中间整合层进行整合。springMVC 是一个基于 mvc ...

14620
来自专栏菩提树下的杨过

log4j2 与 spring mvc整合

log4j2不仅仅是log4j的简单升级,而是整个项目的重构,官网地址:http://logging.apache.org/log4j/2.x/,大家可以从官网...

41210
来自专栏乐沙弥的世界

Linux 6 下编译安装 PHP 5.6

24620
来自专栏专注研发

IDEA环境下SSM整合------环境配置

一、做不出详细的概念叙述和文本设计,本文主要以实战步骤为主,少量解释为辅助,下面请大家牢记两幅图:

17010
来自专栏三杯水

ELKB5.2.2集群环境部署配置优化终极文档

3,logstash filter 加入urldecode支持url、reffer、agent中文显示

37020

扫码关注云+社区

领取腾讯云代金券