码迷,mamicode.com
首页 > 编程语言 > 详细

SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)

时间:2017-10-17 15:47:20      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:maven   beans   引入   错误   ace   unit   防止   管理   自动生成   

 
 

 

本篇文章主要内容是介绍如何使用IntelliJ IDEA创建Spring + SpringMVC + MyBatis项目,下面会给出项目搭建的详细步骤以及相关的配置文件。

 

1. 创建maven项目

 
1.1  File -> New Module,进入创建项目窗口。
技术分享
 
 
 
1.2  点击Next,填写GroupId、ArtifactId和Version
技术分享
 
 
 
1.3  接着下一步,这里需要注在Properties中添加一个参数 archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢,有时候直接卡住。来自网上的解释:
archetypeCatalog表示插件使用的archetype元数据,不加这个参数时默认为remote,local,即中央仓库archetype元数据,由于中央
仓库的archetype太多了所以导致很慢,指定internal来表示仅使用内部元数据。
 
 
 
 
 
技术分享





1.4  填写Module name
技术分享




1.5  生成maven的项目骨架之后,我们还需要手动在 src/main 下创建 java目录。现在可以直接编写了,我把项目所需要的文件都编写完成之后,项目的工程结构如图。
技术分享
 
 
      到此为止,项目的框架基本搭建完成,下面重点说一下项目的配置文件

 

2.  项目配置文件

 
 2.1   这里使用maven来引入项目所需要的jar包,所以也就不需要手动来管理jar包了。
 
pom.xml
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>com.heitian.web</groupId>  
  5.   <artifactId>web-ssm</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>1.0-SNAPSHOT</version>  
  8.   <name>web-ssm Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <properties>  
  11.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  12.     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  13.   
  14.     <!-- spring版本号 -->  
  15.     <spring.version>4.2.5.RELEASE</spring.version>  
  16.   
  17.     <!-- mybatis版本号 -->  
  18.     <mybatis.version>3.2.8</mybatis.version>  
  19.   
  20.     <!-- mysql驱动版本号 -->  
  21.     <mysql-driver.version>5.1.29</mysql-driver.version>  
  22.   
  23.     <!-- log4j日志包版本号 -->  
  24.     <slf4j.version>1.7.18</slf4j.version>  
  25.     <log4j.version>1.2.17</log4j.version>  
  26.   
  27.   </properties>  
  28.   
  29.   
  30.   <dependencies>  
  31.     <!-- 添加jstl依赖 -->  
  32.     <dependency>  
  33.       <groupId>jstl</groupId>  
  34.       <artifactId>jstl</artifactId>  
  35.       <version>1.2</version>  
  36.     </dependency>  
  37.   
  38.     <dependency>  
  39.       <groupId>javax</groupId>  
  40.       <artifactId>javaee-api</artifactId>  
  41.       <version>7.0</version>  
  42.     </dependency>  
  43.   
  44.     <!-- 添加junit4依赖 -->  
  45.     <dependency>  
  46.       <groupId>junit</groupId>  
  47.       <artifactId>junit</artifactId>  
  48.       <version>4.11</version>  
  49.       <!-- 指定范围,在测试时才会加载 -->  
  50.       <scope>test</scope>  
  51.     </dependency>  
  52.   
  53.     <!-- 添加spring核心依赖 -->  
  54.     <dependency>  
  55.       <groupId>org.springframework</groupId>  
  56.       <artifactId>spring-core</artifactId>  
  57.       <version>${spring.version}</version>  
  58.     </dependency>  
  59.     <dependency>  
  60.       <groupId>org.springframework</groupId>  
  61.       <artifactId>spring-web</artifactId>  
  62.       <version>${spring.version}</version>  
  63.     </dependency>  
  64.     <dependency>  
  65.       <groupId>org.springframework</groupId>  
  66.       <artifactId>spring-oxm</artifactId>  
  67.       <version>${spring.version}</version>  
  68.     </dependency>  
  69.     <dependency>  
  70.       <groupId>org.springframework</groupId>  
  71.       <artifactId>spring-tx</artifactId>  
  72.       <version>${spring.version}</version>  
  73.     </dependency>  
  74.     <dependency>  
  75.       <groupId>org.springframework</groupId>  
  76.       <artifactId>spring-jdbc</artifactId>  
  77.       <version>${spring.version}</version>  
  78.     </dependency>  
  79.     <dependency>  
  80.       <groupId>org.springframework</groupId>  
  81.       <artifactId>spring-webmvc</artifactId>  
  82.       <version>${spring.version}</version>  
  83.     </dependency>  
  84.     <dependency>  
  85.       <groupId>org.springframework</groupId>  
  86.       <artifactId>spring-context</artifactId>  
  87.       <version>${spring.version}</version>  
  88.     </dependency>  
  89.     <dependency>  
  90.       <groupId>org.springframework</groupId>  
  91.       <artifactId>spring-context-support</artifactId>  
  92.       <version>${spring.version}</version>  
  93.     </dependency>  
  94.     <dependency>  
  95.       <groupId>org.springframework</groupId>  
  96.       <artifactId>spring-aop</artifactId>  
  97.       <version>${spring.version}</version>  
  98.     </dependency>  
  99.   
  100.     <dependency>  
  101.       <groupId>org.springframework</groupId>  
  102.       <artifactId>spring-test</artifactId>  
  103.       <version>${spring.version}</version>  
  104.     </dependency>  
  105.   
  106.     <!-- 添加mybatis依赖 -->  
  107.     <dependency>  
  108.       <groupId>org.mybatis</groupId>  
  109.       <artifactId>mybatis</artifactId>  
  110.       <version>${mybatis.version}</version>  
  111.     </dependency>  
  112.   
  113.     <!-- 添加mybatis/spring整合包依赖 -->  
  114.     <dependency>  
  115.       <groupId>org.mybatis</groupId>  
  116.       <artifactId>mybatis-spring</artifactId>  
  117.       <version>1.2.2</version>  
  118.     </dependency>  
  119.   
  120.     <!-- 添加mysql驱动依赖 -->  
  121.     <dependency>  
  122.       <groupId>mysql</groupId>  
  123.       <artifactId>mysql-connector-java</artifactId>  
  124.       <version>${mysql-driver.version}</version>  
  125.     </dependency>  
  126.     <!-- 添加数据库连接池依赖 -->  
  127.     <dependency>  
  128.       <groupId>commons-dbcp</groupId>  
  129.       <artifactId>commons-dbcp</artifactId>  
  130.       <version>1.2.2</version>  
  131.     </dependency>  
  132.   
  133.     <!-- 添加fastjson -->  
  134.     <dependency>  
  135.       <groupId>com.alibaba</groupId>  
  136.       <artifactId>fastjson</artifactId>  
  137.       <version>1.1.41</version>  
  138.     </dependency>  
  139.   
  140.     <!-- 添加日志相关jar包 -->  
  141.     <dependency>  
  142.       <groupId>log4j</groupId>  
  143.       <artifactId>log4j</artifactId>  
  144.       <version>${log4j.version}</version>  
  145.     </dependency>  
  146.     <dependency>  
  147.       <groupId>org.slf4j</groupId>  
  148.       <artifactId>slf4j-api</artifactId>  
  149.       <version>${slf4j.version}</version>  
  150.     </dependency>  
  151.     <dependency>  
  152.       <groupId>org.slf4j</groupId>  
  153.       <artifactId>slf4j-log4j12</artifactId>  
  154.       <version>${slf4j.version}</version>  
  155.     </dependency>  
  156.   
  157.     <!-- log end -->  
  158.     <!-- 映入JSON -->  
  159.     <dependency>  
  160.       <groupId>org.codehaus.jackson</groupId>  
  161.       <artifactId>jackson-mapper-asl</artifactId>  
  162.       <version>1.9.13</version>  
  163.     </dependency>  
  164.     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->  
  165.     <dependency>  
  166.       <groupId>com.fasterxml.jackson.core</groupId>  
  167.       <artifactId>jackson-core</artifactId>  
  168.       <version>2.8.0</version>  
  169.     </dependency>  
  170.     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->  
  171.     <dependency>  
  172.       <groupId>com.fasterxml.jackson.core</groupId>  
  173.       <artifactId>jackson-databind</artifactId>  
  174.       <version>2.8.0</version>  
  175.     </dependency>  
  176.   
  177.     <dependency>  
  178.       <groupId>commons-fileupload</groupId>  
  179.       <artifactId>commons-fileupload</artifactId>  
  180.       <version>1.3.1</version>  
  181.     </dependency>  
  182.   
  183.     <dependency>  
  184.       <groupId>commons-io</groupId>  
  185.       <artifactId>commons-io</artifactId>  
  186.       <version>2.4</version>  
  187.     </dependency>  
  188.   
  189.     <dependency>  
  190.       <groupId>commons-codec</groupId>  
  191.       <artifactId>commons-codec</artifactId>  
  192.       <version>1.9</version>  
  193.     </dependency>  
  194.   </dependencies>  
  195.   
  196.   <build>  
  197.     <finalName>web-ssm</finalName>  
  198.   </build>  
  199. </project>  
<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.heitian.web</groupId>
  <artifactId>web-ssm</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>web-ssm Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <!-- spring版本号 -->
    <spring.version>4.2.5.RELEASE</spring.version>

    <!-- mybatis版本号 -->
    <mybatis.version>3.2.8</mybatis.version>

    <!-- mysql驱动版本号 -->
    <mysql-driver.version>5.1.29</mysql-driver.version>

    <!-- log4j日志包版本号 -->
    <slf4j.version>1.7.18</slf4j.version>
    <log4j.version>1.2.17</log4j.version>

  </properties>


  <dependencies>
    <!-- 添加jstl依赖 -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
    </dependency>

    <!-- 添加junit4依赖 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <!-- 指定范围,在测试时才会加载 -->
      <scope>test</scope>
    </dependency>

    <!-- 添加spring核心依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</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-oxm</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-webmvc</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-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- 添加mybatis依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>

    <!-- 添加mybatis/spring整合包依赖 -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.2</version>
    </dependency>

    <!-- 添加mysql驱动依赖 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql-driver.version}</version>
    </dependency>
    <!-- 添加数据库连接池依赖 -->
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.2.2</version>
    </dependency>

    <!-- 添加fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.1.41</version>
    </dependency>

    <!-- 添加日志相关jar包 -->
    <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>

    <!-- log end -->
    <!-- 映入JSON -->
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.8.0</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.0</version>
    </dependency>

    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>

    <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.9</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>web-ssm</finalName>
  </build>
</project>


 
2.2   新建jdbc.properties文件
 
jdbc.properties
 
  1. driverClasss=com.mysql.jdbc.Driver  
  2. jdbcUrl=jdbc:mysql://localhost:3306/db_ssm?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull  
  3. username=root  
  4. password=root  
  5.   
  6. #定义初始连接数  
  7. initialSize=0  
  8. #定义最大连接数  
  9. maxActive=20  
  10. #定义最大空闲  
  11. maxIdle=20  
  12. #定义最小空闲  
  13. minIdle=1  
  14. #定义最长等待时间  
  15. maxWait=60000  
driverClasss=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/db_ssm?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username=root
password=root

#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
 
2.3  新建log4j 配置文件
 
log4j.properties
 
  1. log4j.rootLogger=INFO,Console,File  
  2.   
  3. #控制台日志  
  4. log4j.appender.Console=org.apache.log4j.ConsoleAppender  
  5. log4j.appender.Console.Target=System.out  
  6. log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
  7. log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n  
  8.   
  9. #普通文件日志  
  10. log4j.appender.File=org.apache.log4j.RollingFileAppender  
  11. log4j.appender.File.File=logs/ssm.log  
  12. log4j.appender.File.MaxFileSize=10MB  
  13. #输出日志,如果换成DEBUG表示输出DEBUG以上级别日志  
  14. log4j.appender.File.Threshold=ALL  
  15. log4j.appender.File.layout=org.apache.log4j.PatternLayout  
  16. log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n  
log4j.rootLogger=INFO,Console,File

#控制台日志
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n

#普通文件日志
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.MaxFileSize=10MB
#输出日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
 
2.4  新建springmvc配置文件
 
 
spring-mvc.xml
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  8.                         http://www.springframework.org/schema/context  
  9.                         http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  10.                         http://www.springframework.org/schema/mvc  
  11.                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
  12.   
  13.     <!-- 自动扫描  @Controller-->  
  14.     <context:component-scan base-package="com.heitian.ssm.controller"/>  
  15.   
  16.     <!--避免IE执行AJAX时,返回JSON出现下载文件 -->  
  17.     <bean id="mappingJacksonHttpMessageConverter"  
  18.           class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
  19.         <property name="supportedMediaTypes">  
  20.             <list>  
  21.                 <value>text/html;charset=UTF-8</value>  
  22.             </list>  
  23.         </property>  
  24.     </bean>  
  25.     <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->  
  26.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  27.         <property name="messageConverters">  
  28.             <list>  
  29.                 <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON转换器 -->  
  30.             </list>  
  31.         </property>  
  32.     </bean>  
  33.   
  34.   
  35.     <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->  
  36.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  37.         <property name="prefix" value="/WEB-INF/jsp/" />  
  38.         <property name="suffix" value=".jsp"/>  
  39.     </bean>  
  40.   
  41.     <!-- 文件上传配置 -->  
  42.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  43.         <!-- 默认编码 -->  
  44.         <property name="defaultEncoding" value="UTF-8"/>  
  45.         <!-- 上传文件大小限制为31M,31*1024*1024 -->  
  46.         <property name="maxUploadSize" value="32505856"/>  
  47.         <!-- 内存中的最大值 -->  
  48.         <property name="maxInMemorySize" value="4096"/>  
  49.     </bean>  
  50. </beans>  
<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 自动扫描  @Controller-->
    <context:component-scan base-package="com.heitian.ssm.controller"/>

    <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
    <bean id="mappingJacksonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON转换器 -->
            </list>
        </property>
    </bean>


    <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 文件上传配置 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="UTF-8"/>
        <!-- 上传文件大小限制为31M,31*1024*1024 -->
        <property name="maxUploadSize" value="32505856"/>
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="4096"/>
    </bean>
</beans>
 
2.5  新建spring和mybatis整合所需的配置文件
 
 
spring-mybatis.xml 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  7.                         http://www.springframework.org/schema/context  
  8.                         http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  9.                         http://www.springframework.org/schema/tx  
  10.                         http://www.springframework.org/schema/tx/spring-tx.xsd">  
  11.   
  12.     <!-- 自动扫描 -->  
  13.     <context:component-scan base-package="com.heitian.ssm"/>  
  14.   
  15.     <!-- 第一种方式:加载一个properties文件 -->  
  16.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  17.         <property name="location" value="classpath:jdbc.properties"/>  
  18.     </bean>  
  19.   
  20.   
  21.     <!-- 第二种方式:加载多个properties文件  
  22.     <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
  23.         <property name="locations">  
  24.             <list>  
  25.                 <value>classpath:jdbc.properties</value>  
  26.                 <value>classpath:common.properties</value>  
  27.             </list>  
  28.         </property>  
  29.         <property name="fileEncoding" value="UTF-8"/>  
  30.     </bean>  
  31.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
  32.         <property name="properties" ref="configProperties"/>  
  33.     </bean>  
  34.     -->  
  35.   
  36.     <!-- 配置数据源 -->  
  37.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  38.           destroy-method="close">  
  39.         <property name="driverClassName" value="${driverClasss}"/>  
  40.         <property name="url" value="${jdbcUrl}"/>  
  41.         <property name="username" value="${username}"/>  
  42.         <property name="password" value="${password}"/>  
  43.         <!-- 初始化连接大小 -->  
  44.         <property name="initialSize" value="${initialSize}"></property>  
  45.         <!-- 连接池最大数量 -->  
  46.         <property name="maxActive" value="${maxActive}"></property>  
  47.         <!-- 连接池最大空闲 -->  
  48.         <property name="maxIdle" value="${maxIdle}"></property>  
  49.         <!-- 连接池最小空闲 -->  
  50.         <property name="minIdle" value="${minIdle}"></property>  
  51.         <!-- 获取连接最大等待时间 -->  
  52.         <property name="maxWait" value="${maxWait}"></property>  
  53.     </bean>  
  54.   
  55.     <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->  
  56.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  57.         <property name="dataSource" ref="dataSource"/>  
  58.         <!-- 自动扫描mapping.xml文件 -->  
  59.         <property name="mapperLocations" value="classpath:mapping/*.xml"></property>  
  60.     </bean>  
  61.   
  62.     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
  63.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  64.         <property name="basePackage" value="com.heitian.ssm.dao"/>  
  65.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
  66.     </bean>  
  67.   
  68.   
  69.     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
  70.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  71.         <property name="dataSource" ref="dataSource"/>  
  72.     </bean>  
  73.   
  74.     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
  75.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  76. </beans>  
<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.heitian.ssm"/>

    <!-- 第一种方式:加载一个properties文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>


    <!-- 第二种方式:加载多个properties文件
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:common.properties</value>
            </list>
        </property>
        <property name="fileEncoding" value="UTF-8"/>
    </bean>
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties"/>
    </bean>
    -->

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${driverClasss}"/>
        <property name="url" value="${jdbcUrl}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:mapping/*.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.heitian.ssm.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>


    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
 
2.6  web.xml配置文件
 
web.xml
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  5.           http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  6.          version="3.0">  
  7.   
  8.     <display-name>web-ssm</display-name>  
  9.       
  10.     <context-param>  
  11.         <param-name>contextConfigLocation</param-name>  
  12.         <param-value>classpath:spring-mybatis.xml</param-value>  
  13.     </context-param>  
  14.   
  15.     <context-param>  
  16.         <param-name>log4jConfigLocation</param-name>  
  17.         <param-value>classpath:log4j.properties</param-value>  
  18.     </context-param>  
  19.   
  20.     <!-- 编码过滤器 -->  
  21.     <filter>  
  22.         <filter-name>encodingFilter</filter-name>  
  23.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  24.         <init-param>  
  25.             <param-name>encoding</param-name>  
  26.             <param-value>UTF-8</param-value>  
  27.         </init-param>  
  28.     </filter>  
  29.     <filter-mapping>  
  30.         <filter-name>encodingFilter</filter-name>  
  31.         <url-pattern>/*</url-pattern>  
  32.     </filter-mapping>  
  33.   
  34.     <!-- spring监听器 -->  
  35.     <listener>  
  36.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  37.     </listener>  
  38.   
  39.     <!-- 防止spring内存溢出监听器,比如quartz -->  
  40.     <listener>  
  41.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  42.     </listener>  
  43.   
  44.   
  45.     <!-- spring mvc servlet-->  
  46.     <servlet>  
  47.         <servlet-name>SpringMVC</servlet-name>  
  48.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  49.         <init-param>  
  50.             <param-name>contextConfigLocation</param-name>  
  51.             <param-value>classpath:spring-mvc.xml</param-value>  
  52.         </init-param>  
  53.         <load-on-startup>1</load-on-startup>  
  54.         <async-supported>true</async-supported>  
  55.     </servlet>  
  56.     <servlet-mapping>  
  57.         <servlet-name>SpringMVC</servlet-name>  
  58.         <!-- 此处也可以配置成 *.do 形式 -->  
  59.         <url-pattern>/</url-pattern>  
  60.     </servlet-mapping>  
  61.   
  62.     <welcome-file-list>  
  63.         <welcome-file>/index.jsp</welcome-file>  
  64.     </welcome-file-list>  
  65.   
  66.     <!-- session配置 -->  
  67.     <session-config>  
  68.         <session-timeout>15</session-timeout>  
  69.     </session-config>  
  70.   
  71. </web-app>  
<?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">

    <display-name>web-ssm</display-name>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>

    <!-- 编码过滤器 -->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <!-- 防止spring内存溢出监听器,比如quartz -->
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>


    <!-- spring mvc servlet-->
    <servlet>
        <servlet-name>SpringMVC</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>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- 此处也可以配置成 *.do 形式 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

    <!-- session配置 -->
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

</web-app>
 
 
 

3.  项目配置和部署

 
3.1   File -> Project Structure,进入创建项目配置窗口。
技术分享
 
 
 
3.2  创建一个Tomcat容器实例,并把项目部署进去
技术分享

    

 

3.3  项目所需配置好项目访问的根路径,然后启动Tomcat。

技术分享

 
 
3.4  在浏览器地址栏中输入:http://localhost:8080/web-ssm/user/showUser
技术分享

 

3.5  项目所需看到图中显示效果,则表示项目搭建成功。

 

 

4.  值得注意的地方

 
搭建好项目的框架之后,启动Tomcat,如果访问 http://localhost:8080/user/showUser 出现如下错误:
技术分享




出现这个错误是因为少了依赖的jar包,只要在pom文件中添加如下依赖即可。
  1. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->  
  2.     <dependency>  
  3.       <groupId>com.fasterxml.jackson.core</groupId>  
  4.       <artifactId>jackson-core</artifactId>  
  5.       <version>2.8.0</version>  
  6.     </dependency>  
  7.     <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->  
  8.     <dependency>  
  9.       <groupId>com.fasterxml.jackson.core</groupId>  
  10.       <artifactId>jackson-databind</artifactId>  
  11.       <version>2.8.0</version>  
  12.     </dependency>  
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.8.0</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.0</version>
    </dependency>


5. 项目下载地址

 
  项目下载的地址为:http://download.csdn.net/detail/gallenzhang/9580035


原文转自 http://blog.csdn.net/gallenzhang/article/details/51932152

SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)

标签:maven   beans   引入   错误   ace   unit   防止   管理   自动生成   

原文地址:http://www.cnblogs.com/honey01/p/7680693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!