[转] SSM框架整合(IntelliJ IDEA + maven + 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
 
[html]  view plain  copy
 
  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.0modelVersion>  
  4.   <groupId>com.heitian.webgroupId>  
  5.   <artifactId>web-ssmartifactId>  
  6.   <packaging>warpackaging>  
  7.   <version>1.0-SNAPSHOTversion>  
  8.   <name>web-ssm Maven Webappname>  
  9.   <url>http://maven.apache.orgurl>  
  10.   <properties>  
  11.     <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>  
  12.     <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>  
  13.   
  14.       
  15.     <spring.version>4.2.5.RELEASEspring.version>  
  16.   
  17.       
  18.     <mybatis.version>3.2.8mybatis.version>  
  19.   
  20.       
  21.     <mysql-driver.version>5.1.29mysql-driver.version>  
  22.   
  23.       
  24.     <slf4j.version>1.7.18slf4j.version>  
  25.     <log4j.version>1.2.17log4j.version>  
  26.   
  27.   properties>  
  28.   
  29.   
  30.   <dependencies>  
  31.       
  32.     <dependency>  
  33.       <groupId>jstlgroupId>  
  34.       <artifactId>jstlartifactId>  
  35.       <version>1.2version>  
  36.     dependency>  
  37.   
  38.     <dependency>  
  39.       <groupId>javaxgroupId>  
  40.       <artifactId>javaee-apiartifactId>  
  41.       <version>7.0version>  
  42.     dependency>  
  43.   
  44.       
  45.     <dependency>  
  46.       <groupId>junitgroupId>  
  47.       <artifactId>junitartifactId>  
  48.       <version>4.11version>  
  49.         
  50.       <scope>testscope>  
  51.     dependency>  
  52.   
  53.       
  54.     <dependency>  
  55.       <groupId>org.springframeworkgroupId>  
  56.       <artifactId>spring-coreartifactId>  
  57.       <version>${spring.version}version>  
  58.     dependency>  
  59.     <dependency>  
  60.       <groupId>org.springframeworkgroupId>  
  61.       <artifactId>spring-webartifactId>  
  62.       <version>${spring.version}version>  
  63.     dependency>  
  64.     <dependency>  
  65.       <groupId>org.springframeworkgroupId>  
  66.       <artifactId>spring-oxmartifactId>  
  67.       <version>${spring.version}version>  
  68.     dependency>  
  69.     <dependency>  
  70.       <groupId>org.springframeworkgroupId>  
  71.       <artifactId>spring-txartifactId>  
  72.       <version>${spring.version}version>  
  73.     dependency>  
  74.     <dependency>  
  75.       <groupId>org.springframeworkgroupId>  
  76.       <artifactId>spring-jdbcartifactId>  
  77.       <version>${spring.version}version>  
  78.     dependency>  
  79.     <dependency>  
  80.       <groupId>org.springframeworkgroupId>  
  81.       <artifactId>spring-webmvcartifactId>  
  82.       <version>${spring.version}version>  
  83.     dependency>  
  84.     <dependency>  
  85.       <groupId>org.springframeworkgroupId>  
  86.       <artifactId>spring-contextartifactId>  
  87.       <version>${spring.version}version>  
  88.     dependency>  
  89.     <dependency>  
  90.       <groupId>org.springframeworkgroupId>  
  91.       <artifactId>spring-context-supportartifactId>  
  92.       <version>${spring.version}version>  
  93.     dependency>  
  94.     <dependency>  
  95.       <groupId>org.springframeworkgroupId>  
  96.       <artifactId>spring-aopartifactId>  
  97.       <version>${spring.version}version>  
  98.     dependency>  
  99.   
  100.     <dependency>  
  101.       <groupId>org.springframeworkgroupId>  
  102.       <artifactId>spring-testartifactId>  
  103.       <version>${spring.version}version>  
  104.     dependency>  
  105.   
  106.       
  107.     <dependency>  
  108.       <groupId>org.mybatisgroupId>  
  109.       <artifactId>mybatisartifactId>  
  110.       <version>${mybatis.version}version>  
  111.     dependency>  
  112.   
  113.       
  114.     <dependency>  
  115.       <groupId>org.mybatisgroupId>  
  116.       <artifactId>mybatis-springartifactId>  
  117.       <version>1.2.2version>  
  118.     dependency>  
  119.   
  120.       
  121.     <dependency>  
  122.       <groupId>mysqlgroupId>  
  123.       <artifactId>mysql-connector-javaartifactId>  
  124.       <version>${mysql-driver.version}version>  
  125.     dependency>  
  126.       
  127.     <dependency>  
  128.       <groupId>commons-dbcpgroupId>  
  129.       <artifactId>commons-dbcpartifactId>  
  130.       <version>1.2.2version>  
  131.     dependency>  
  132.   
  133.       
  134.     <dependency>  
  135.       <groupId>com.alibabagroupId>  
  136.       <artifactId>fastjsonartifactId>  
  137.       <version>1.1.41version>  
  138.     dependency>  
  139.   
  140.       
  141.     <dependency>  
  142.       <groupId>log4jgroupId>  
  143.       <artifactId>log4jartifactId>  
  144.       <version>${log4j.version}version>  
  145.     dependency>  
  146.     <dependency>  
  147.       <groupId>org.slf4jgroupId>  
  148.       <artifactId>slf4j-apiartifactId>  
  149.       <version>${slf4j.version}version>  
  150.     dependency>  
  151.     <dependency>  
  152.       <groupId>org.slf4jgroupId>  
  153.       <artifactId>slf4j-log4j12artifactId>  
  154.       <version>${slf4j.version}version>  
  155.     dependency>  
  156.   
  157.       
  158.       
  159.     <dependency>  
  160.       <groupId>org.codehaus.jacksongroupId>  
  161.       <artifactId>jackson-mapper-aslartifactId>  
  162.       <version>1.9.13version>  
  163.     dependency>  
  164.       
  165.     <dependency>  
  166.       <groupId>com.fasterxml.jackson.coregroupId>  
  167.       <artifactId>jackson-coreartifactId>  
  168.       <version>2.8.0version>  
  169.     dependency>  
  170.       
  171.     <dependency>  
  172.       <groupId>com.fasterxml.jackson.coregroupId>  
  173.       <artifactId>jackson-databindartifactId>  
  174.       <version>2.8.0version>  
  175.     dependency>  
  176.   
  177.     <dependency>  
  178.       <groupId>commons-fileuploadgroupId>  
  179.       <artifactId>commons-fileuploadartifactId>  
  180.       <version>1.3.1version>  
  181.     dependency>  
  182.   
  183.     <dependency>  
  184.       <groupId>commons-iogroupId>  
  185.       <artifactId>commons-ioartifactId>  
  186.       <version>2.4version>  
  187.     dependency>  
  188.   
  189.     <dependency>  
  190.       <groupId>commons-codecgroupId>  
  191.       <artifactId>commons-codecartifactId>  
  192.       <version>1.9version>  
  193.     dependency>  
  194.   dependencies>  
  195.   
  196.   <build>  
  197.     <finalName>web-ssmfinalName>  
  198.   build>  
  199. project>  


 
2.2   新建jdbc.properties文件
 
jdbc.properties
 
[html]  view plain  copy
 
  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  
 
2.3  新建log4j 配置文件
 
log4j.properties
 
[html]  view plain  copy
 
  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  
 
2.4  新建springmvc配置文件
 
 
spring-mvc.xml
 
 
[html]  view plain  copy
 
  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.       
  14.     <context:component-scan base-package="com.heitian.ssm.controller"/>  
  15.   
  16.       
  17.     <bean id="mappingJacksonHttpMessageConverter"  
  18.           class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
  19.         <property name="supportedMediaTypes">  
  20.             <list>  
  21.                 <value>text/html;charset=UTF-8value>  
  22.             list>  
  23.         property>  
  24.     bean>  
  25.       
  26.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  27.         <property name="messageConverters">  
  28.             <list>  
  29.                 <ref bean="mappingJacksonHttpMessageConverter"/>   
  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.           
  46.         <property name="maxUploadSize" value="32505856"/>  
  47.           
  48.         <property name="maxInMemorySize" value="4096"/>  
  49.     bean>  
  50. beans>  
 
2.5  新建spring和mybatis整合所需的配置文件
 
 
spring-mybatis.xml 
 
[html]  view plain  copy
 
  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.       
  16.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  17.         <property name="location" value="classpath:jdbc.properties"/>  
  18.     bean>  
  19.   
  20.   
  21.       
  22.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  23.           destroy-method="close">  
  24.         <property name="driverClassName" value="${driverClasss}"/>  
  25.         <property name="url" value="${jdbcUrl}"/>  
  26.         <property name="username" value="${username}"/>  
  27.         <property name="password" value="${password}"/>  
  28.           
  29.         <property name="initialSize" value="${initialSize}">property>  
  30.           
  31.         <property name="maxActive" value="${maxActive}">property>  
  32.           
  33.         <property name="maxIdle" value="${maxIdle}">property>  
  34.           
  35.         <property name="minIdle" value="${minIdle}">property>  
  36.           
  37.         <property name="maxWait" value="${maxWait}">property>  
  38.     bean>  
  39.   
  40.       
  41.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  42.         <property name="dataSource" ref="dataSource"/>  
  43.           
  44.         <property name="mapperLocations" value="classpath:mapping/*.xml">property>  
  45.     bean>  
  46.   
  47.       
  48.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  49.         <property name="basePackage" value="com.heitian.ssm.dao"/>  
  50.         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>  
  51.     bean>  
  52.   
  53.   
  54.       
  55.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  56.         <property name="dataSource" ref="dataSource"/>  
  57.     bean>  
  58.   
  59.       
  60.     <tx:annotation-driven transaction-manager="transactionManager"/>  
  61. beans>  
 
2.6  web.xml配置文件
 
web.xml
 
[html]  view plain  copy
 
  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-ssmdisplay-name>  
  9.       
  10.     <context-param>  
  11.         <param-name>contextConfigLocationparam-name>  
  12.         <param-value>classpath:spring-mybatis.xmlparam-value>  
  13.     context-param>  
  14.   
  15.     <context-param>  
  16.         <param-name>log4jConfigLocationparam-name>  
  17.         <param-value>classpath:log4j.propertiesparam-value>  
  18.     context-param>  
  19.   
  20.       
  21.     <filter>  
  22.         <filter-name>encodingFilterfilter-name>  
  23.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
  24.         <init-param>  
  25.             <param-name>encodingparam-name>  
  26.             <param-value>UTF-8param-value>  
  27.         init-param>  
  28.     filter>  
  29.     <filter-mapping>  
  30.         <filter-name>encodingFilterfilter-name>  
  31.         <url-pattern>/*url-pattern>  
  32.     filter-mapping>  
  33.   
  34.       
  35.     <listener>  
  36.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  37.     listener>  
  38.   
  39.       
  40.     <listener>  
  41.         <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>  
  42.     listener>  
  43.   
  44.   
  45.       
  46.     <servlet>  
  47.         <servlet-name>SpringMVCservlet-name>  
  48.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  49.         <init-param>  
  50.             <param-name>contextConfigLocationparam-name>  
  51.             <param-value>classpath:spring-mvc.xmlparam-value>  
  52.         init-param>  
  53.         <load-on-startup>1load-on-startup>  
  54.         <async-supported>trueasync-supported>  
  55.     servlet>  
  56.     <servlet-mapping>  
  57.         <servlet-name>SpringMVCservlet-name>  
  58.           
  59.         <url-pattern>/url-pattern>  
  60.     servlet-mapping>  
  61.   
  62.     <welcome-file-list>  
  63.         <welcome-file>/index.jspwelcome-file>  
  64.     welcome-file-list>  
  65.   
  66.       
  67.     <session-config>  
  68.         <session-timeout>15session-timeout>  
  69.     session-config>  
  70.   
  71. 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文件中添加如下依赖即可。
[html]  view plain  copy
 
  1.   
  2.     <dependency>  
  3.       <groupId>com.fasterxml.jackson.coregroupId>  
  4.       <artifactId>jackson-coreartifactId>  
  5.       <version>2.8.0version>  
  6.     dependency>  
  7.       
  8.     <dependency>  
  9.       <groupId>com.fasterxml.jackson.coregroupId>  
  10.       <artifactId>jackson-databindartifactId>  
  11.       <version>2.8.0version>  
  12.     dependency>  

转载于:https://www.cnblogs.com/hjy1017/p/6022858.html

你可能感兴趣的:([转] SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis))