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

时间:2023-03-08 17:49:11

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

1. 创建maven项目

1.1  File -> New Module,进入创建项目窗口。
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
1.2  点击Next,填写GroupId、ArtifactId和Version
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
1.3  接着下一步,这里需要注在Properties中添加一个参数 archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢,有时候直接卡住。来自网上的解释:
archetypeCatalog表示插件使用的archetype元数据,不加这个参数时默认为remote,local,即*仓库archetype元数据,由于*
仓库的archetype太多了所以导致很慢,指定internal来表示仅使用内部元数据。
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
1.4  填写Module name
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
1.5  生成maven的项目骨架之后,我们还需要手动在 src/main 下创建 java目录。现在可以直接编写了,我把项目所需要的文件都编写完成之后,项目的工程结构如图。
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
      到此为止,项目的框架基本搭建完成,下面重点说一下项目的配置文件

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. <!-- spring版本号 -->
  14. <spring.version>4.2.5.RELEASE</spring.version>
  15. <!-- mybatis版本号 -->
  16. <mybatis.version>3.2.8</mybatis.version>
  17. <!-- mysql驱动版本号 -->
  18. <mysql-driver.version>5.1.29</mysql-driver.version>
  19. <!-- log4j日志包版本号 -->
  20. <slf4j.version>1.7.18</slf4j.version>
  21. <log4j.version>1.2.17</log4j.version>
  22. </properties>
  23. <dependencies>
  24. <!-- 添加jstl依赖 -->
  25. <dependency>
  26. <groupId>jstl</groupId>
  27. <artifactId>jstl</artifactId>
  28. <version>1.2</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>javax</groupId>
  32. <artifactId>javaee-api</artifactId>
  33. <version>7.0</version>
  34. </dependency>
  35. <!-- 添加junit4依赖 -->
  36. <dependency>
  37. <groupId>junit</groupId>
  38. <artifactId>junit</artifactId>
  39. <version>4.11</version>
  40. <!-- 指定范围,在测试时才会加载 -->
  41. <scope>test</scope>
  42. </dependency>
  43. <!-- 添加spring核心依赖 -->
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-core</artifactId>
  47. <version>${spring.version}</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.springframework</groupId>
  51. <artifactId>spring-web</artifactId>
  52. <version>${spring.version}</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework</groupId>
  56. <artifactId>spring-oxm</artifactId>
  57. <version>${spring.version}</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework</groupId>
  61. <artifactId>spring-tx</artifactId>
  62. <version>${spring.version}</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework</groupId>
  66. <artifactId>spring-jdbc</artifactId>
  67. <version>${spring.version}</version>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.springframework</groupId>
  71. <artifactId>spring-webmvc</artifactId>
  72. <version>${spring.version}</version>
  73. </dependency>
  74. <dependency>
  75. <groupId>org.springframework</groupId>
  76. <artifactId>spring-context</artifactId>
  77. <version>${spring.version}</version>
  78. </dependency>
  79. <dependency>
  80. <groupId>org.springframework</groupId>
  81. <artifactId>spring-context-support</artifactId>
  82. <version>${spring.version}</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>org.springframework</groupId>
  86. <artifactId>spring-aop</artifactId>
  87. <version>${spring.version}</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>org.springframework</groupId>
  91. <artifactId>spring-test</artifactId>
  92. <version>${spring.version}</version>
  93. </dependency>
  94. <!-- 添加mybatis依赖 -->
  95. <dependency>
  96. <groupId>org.mybatis</groupId>
  97. <artifactId>mybatis</artifactId>
  98. <version>${mybatis.version}</version>
  99. </dependency>
  100. <!-- 添加mybatis/spring整合包依赖 -->
  101. <dependency>
  102. <groupId>org.mybatis</groupId>
  103. <artifactId>mybatis-spring</artifactId>
  104. <version>1.2.2</version>
  105. </dependency>
  106. <!-- 添加mysql驱动依赖 -->
  107. <dependency>
  108. <groupId>mysql</groupId>
  109. <artifactId>mysql-connector-java</artifactId>
  110. <version>${mysql-driver.version}</version>
  111. </dependency>
  112. <!-- 添加数据库连接池依赖 -->
  113. <dependency>
  114. <groupId>commons-dbcp</groupId>
  115. <artifactId>commons-dbcp</artifactId>
  116. <version>1.2.2</version>
  117. </dependency>
  118. <!-- 添加fastjson -->
  119. <dependency>
  120. <groupId>com.alibaba</groupId>
  121. <artifactId>fastjson</artifactId>
  122. <version>1.1.41</version>
  123. </dependency>
  124. <!-- 添加日志相关jar包 -->
  125. <dependency>
  126. <groupId>log4j</groupId>
  127. <artifactId>log4j</artifactId>
  128. <version>${log4j.version}</version>
  129. </dependency>
  130. <dependency>
  131. <groupId>org.slf4j</groupId>
  132. <artifactId>slf4j-api</artifactId>
  133. <version>${slf4j.version}</version>
  134. </dependency>
  135. <dependency>
  136. <groupId>org.slf4j</groupId>
  137. <artifactId>slf4j-log4j12</artifactId>
  138. <version>${slf4j.version}</version>
  139. </dependency>
  140. <!-- log end -->
  141. <!-- 映入JSON -->
  142. <dependency>
  143. <groupId>org.codehaus.jackson</groupId>
  144. <artifactId>jackson-mapper-asl</artifactId>
  145. <version>1.9.13</version>
  146. </dependency>
  147. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
  148. <dependency>
  149. <groupId>com.fasterxml.jackson.core</groupId>
  150. <artifactId>jackson-core</artifactId>
  151. <version>2.8.0</version>
  152. </dependency>
  153. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  154. <dependency>
  155. <groupId>com.fasterxml.jackson.core</groupId>
  156. <artifactId>jackson-databind</artifactId>
  157. <version>2.8.0</version>
  158. </dependency>
  159. <dependency>
  160. <groupId>commons-fileupload</groupId>
  161. <artifactId>commons-fileupload</artifactId>
  162. <version>1.3.1</version>
  163. </dependency>
  164. <dependency>
  165. <groupId>commons-io</groupId>
  166. <artifactId>commons-io</artifactId>
  167. <version>2.4</version>
  168. </dependency>
  169. <dependency>
  170. <groupId>commons-codec</groupId>
  171. <artifactId>commons-codec</artifactId>
  172. <version>1.9</version>
  173. </dependency>
  174. </dependencies>
  175. <build>
  176. <finalName>web-ssm</finalName>
  177. </build>
  178. </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. initialSize=0
  7. #定义最大连接数
  8. maxActive=20
  9. #定义最大空闲
  10. maxIdle=20
  11. #定义最小空闲
  12. minIdle=1
  13. #定义最长等待时间
  14. maxWait=60000
2.3  新建log4j 配置文件
log4j.properties
  1. log4j.rootLogger=INFO,Console,File
  2. #控制台日志
  3. log4j.appender.Console=org.apache.log4j.ConsoleAppender
  4. log4j.appender.Console.Target=System.out
  5. log4j.appender.Console.layout=org.apache.log4j.PatternLayout
  6. log4j.appender.Console.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH\:mm\:ss}][%C] - %m%n
  7. #普通文件日志
  8. log4j.appender.File=org.apache.log4j.RollingFileAppender
  9. log4j.appender.File.File=logs/ssm.log
  10. log4j.appender.File.MaxFileSize=10MB
  11. #输出日志,如果换成DEBUG表示输出DEBUG以上级别日志
  12. log4j.appender.File.Threshold=ALL
  13. log4j.appender.File.layout=org.apache.log4j.PatternLayout
  14. 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. <!-- 自动扫描 @Controller-->
  13. <context:component-scan base-package="com.heitian.ssm.controller"/>
  14. <!--避免IE执行AJAX时,返回JSON出现下载文件 -->
  15. <bean id="mappingJacksonHttpMessageConverter"
  16. class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  17. <property name="supportedMediaTypes">
  18. <list>
  19. <value>text/html;charset=UTF-8</value>
  20. </list>
  21. </property>
  22. </bean>
  23. <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
  24. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  25. <property name="messageConverters">
  26. <list>
  27. <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON转换器 -->
  28. </list>
  29. </property>
  30. </bean>
  31. <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
  32. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  33. <property name="prefix" value="/WEB-INF/jsp/" />
  34. <property name="suffix" value=".jsp"/>
  35. </bean>
  36. <!-- 文件上传配置 -->
  37. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  38. <!-- 默认编码 -->
  39. <property name="defaultEncoding" value="UTF-8"/>
  40. <!-- 上传文件大小限制为31M,31*1024*1024 -->
  41. <property name="maxUploadSize" value="32505856"/>
  42. <!-- 内存中的最大值 -->
  43. <property name="maxInMemorySize" value="4096"/>
  44. </bean>
  45. </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. <context:component-scan base-package="com.heitian.ssm"/>
  13. <!-- 第一种方式:加载一个properties文件 -->
  14. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  15. <property name="location" value="classpath:jdbc.properties"/>
  16. </bean>
  17. <!-- 第二种方式:加载多个properties文件
  18. <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  19. <property name="locations">
  20. <list>
  21. <value>classpath:jdbc.properties</value>
  22. <value>classpath:common.properties</value>
  23. </list>
  24. </property>
  25. <property name="fileEncoding" value="UTF-8"/>
  26. </bean>
  27. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
  28. <property name="properties" ref="configProperties"/>
  29. </bean>
  30. -->
  31. <!-- 配置数据源 -->
  32. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  33. destroy-method="close">
  34. <property name="driverClassName" value="${driverClasss}"/>
  35. <property name="url" value="${jdbcUrl}"/>
  36. <property name="username" value="${username}"/>
  37. <property name="password" value="${password}"/>
  38. <!-- 初始化连接大小 -->
  39. <property name="initialSize" value="${initialSize}"></property>
  40. <!-- 连接池最大数量 -->
  41. <property name="maxActive" value="${maxActive}"></property>
  42. <!-- 连接池最大空闲 -->
  43. <property name="maxIdle" value="${maxIdle}"></property>
  44. <!-- 连接池最小空闲 -->
  45. <property name="minIdle" value="${minIdle}"></property>
  46. <!-- 获取连接最大等待时间 -->
  47. <property name="maxWait" value="${maxWait}"></property>
  48. </bean>
  49. <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 -->
  50. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  51. <property name="dataSource" ref="dataSource"/>
  52. <!-- 自动扫描mapping.xml文件 -->
  53. <property name="mapperLocations" value="classpath:mapping/*.xml"></property>
  54. </bean>
  55. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  56. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  57. <property name="basePackage" value="com.heitian.ssm.dao"/>
  58. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  59. </bean>
  60. <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
  61. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  62. <property name="dataSource" ref="dataSource"/>
  63. </bean>
  64. <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
  65. <tx:annotation-driven transaction-manager="transactionManager"/>
  66. </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. <display-name>web-ssm</display-name>
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:spring-mybatis.xml</param-value>
  11. </context-param>
  12. <context-param>
  13. <param-name>log4jConfigLocation</param-name>
  14. <param-value>classpath:log4j.properties</param-value>
  15. </context-param>
  16. <!-- 编码过滤器 -->
  17. <filter>
  18. <filter-name>encodingFilter</filter-name>
  19. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  20. <init-param>
  21. <param-name>encoding</param-name>
  22. <param-value>UTF-8</param-value>
  23. </init-param>
  24. </filter>
  25. <filter-mapping>
  26. <filter-name>encodingFilter</filter-name>
  27. <url-pattern>/*</url-pattern>
  28. </filter-mapping>
  29. <!-- spring监听器 -->
  30. <listener>
  31. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  32. </listener>
  33. <!-- 防止spring内存溢出监听器,比如quartz -->
  34. <listener>
  35. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  36. </listener>
  37. <!-- spring mvc servlet-->
  38. <servlet>
  39. <servlet-name>SpringMVC</servlet-name>
  40. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  41. <init-param>
  42. <param-name>contextConfigLocation</param-name>
  43. <param-value>classpath:spring-mvc.xml</param-value>
  44. </init-param>
  45. <load-on-startup>1</load-on-startup>
  46. <async-supported>true</async-supported>
  47. </servlet>
  48. <servlet-mapping>
  49. <servlet-name>SpringMVC</servlet-name>
  50. <!-- 此处也可以配置成 *.do 形式 -->
  51. <url-pattern>/</url-pattern>
  52. </servlet-mapping>
  53. <welcome-file-list>
  54. <welcome-file>/index.jsp</welcome-file>
  55. </welcome-file-list>
  56. <!-- session配置 -->
  57. <session-config>
  58. <session-timeout>15</session-timeout>
  59. </session-config>
  60. </web-app>

3.  项目配置和部署

3.1   File -> Project Structure,进入创建项目配置窗口。
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
3.2  创建一个Tomcat容器实例,并把项目部署进去
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)

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

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

3.4  在浏览器地址栏中输入:http://localhost:8080/web-ssm/user/showUser
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)

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

4.  值得注意的地方

搭建好项目的框架之后,启动Tomcat,如果访问 http://localhost:8080/user/showUser 出现如下错误:
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
出现这个错误是因为少了依赖的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>

5. 项目下载地址