ssm框架集成详解

快来打我* 2022-06-13 05:24 396阅读 0赞

1. 创建maven项目

1.1 File -> New Module,进入创建项目窗口。

Center

1.2 点击Next,填写GroupId、ArtifactId和Version

Center 1

1.3 接着下一步,这里需要注在Properties中添加一个参数 archetypeCatalog=internal,不加这个参数,在maven生成骨架的时候将会非常慢,有时候直接卡住。 来自网上的解释:

archetypeCatalog表示插件使用的archetype元数据,不加这个参数时默认为remote,local,即中央仓库archetype元数据,由于中央

仓库的archetype太多了所以导致很慢,指定internal来表示仅使用内部元数据。

Center 2

1 .4 填写Module name

Center 3

1.5 生成maven的项目骨架之后,我们还需要手动在 src/main 下创建 java目录。现在可以直接编写了,我把项目所需要的文件都编写完成之后,项目的工程结构如图。

Center 4

  1. 到此为止,项目的框架基本搭建完成,下面重点说一下项目的配置文件

#

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. 4.0.0
  4. com.heitian.web
  5. web-ssm
  6. war
  7. 1.0-SNAPSHOT
  8. web-ssm Maven Webapp
  9. http://maven.apache.org
  10. UTF-8
  11. UTF-8
  12. 4.2.5.RELEASE
  13. 3.2.8
  14. 5.1.29
  15. 1.7.18
  16. 1.2.17
  17. jstl
  18. jstl
  19. 1.2
  20. javax
  21. javaee-api
  22. 7.0
  23. junit
  24. junit
  25. 4.11
  26. test
  27. org.springframework
  28. spring-core
  29. ${spring.version}
  30. org.springframework
  31. spring-web
  32. ${spring.version}
  33. org.springframework
  34. spring-oxm
  35. ${spring.version}
  36. org.springframework
  37. spring-tx
  38. ${spring.version}
  39. org.springframework
  40. spring-jdbc
  41. ${spring.version}
  42. org.springframework
  43. spring-webmvc
  44. ${spring.version}
  45. org.springframework
  46. spring-context
  47. ${spring.version}
  48. org.springframework
  49. spring-context-support
  50. ${spring.version}
  51. org.springframework
  52. spring-aop
  53. ${spring.version}
  54. org.springframework
  55. spring-test
  56. ${spring.version}
  57. org.mybatis
  58. mybatis
  59. ${mybatis.version}
  60. org.mybatis
  61. mybatis-spring
  62. 1.2.2
  63. mysql
  64. mysql-connector-java
  65. ${mysql-driver.version}
  66. commons-dbcp
  67. commons-dbcp
  68. 1.2.2
  69. com.alibaba
  70. fastjson
  71. 1.1.41
  72. log4j
  73. log4j
  74. ${log4j.version}
  75. org.slf4j
  76. slf4j-api
  77. ${slf4j.version}
  78. org.slf4j
  79. slf4j-log4j12
  80. ${slf4j.version}
  81. org.codehaus.jackson
  82. jackson-mapper-asl
  83. 1.9.13
  84. com.fasterxml.jackson.core
  85. jackson-core
  86. 2.8.0
  87. com.fasterxml.jackson.core
  88. jackson-databind
  89. 2.8.0
  90. commons-fileupload
  91. commons-fileupload
  92. 1.3.1
  93. commons-io
  94. commons-io
  95. 2.4
  96. commons-codec
  97. commons-codec
  98. 1.9
  99. web-ssm

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. initialSize=0
  7. #定义最大连接数
  8. maxActive=20
  9. #定义最大空闲
  10. maxIdle=20
  11. #定义最小空闲
  12. minIdle=1
  13. #定义最长等待时间
  14. maxWait=60000

2.3 新建log4j 配置文件

log4j.properties

[html] view plain copy

  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

[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. <bean id=”mappingJacksonHttpMessageConverter”
  13. class=”org.springframework.http.converter.json.MappingJackson2HttpMessageConverter”>
  14. text/html;charset=UTF-8

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. <!— 第二种方式:加载多个properties文件
  12. classpath:jdbc.properties
  13. classpath:common.properties
  14. —>
  15. <bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”
  16. destroy-method=”close”>

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. web-ssm
  8. contextConfigLocation
  9. classpath:spring-mybatis.xml
  10. log4jConfigLocation
  11. classpath:log4j.properties
  12. encodingFilter
  13. org.springframework.web.filter.CharacterEncodingFilter
  14. encoding
  15. UTF-8
  16. encodingFilter
  17. /*
  18. org.springframework.web.context.ContextLoaderListener
  19. org.springframework.web.util.IntrospectorCleanupListener
  20. SpringMVC
  21. org.springframework.web.servlet.DispatcherServlet
  22. contextConfigLocation
  23. classpath:spring-mvc.xml
  24. 1
  25. true
  26. SpringMVC
  27. /
  28. /index.jsp
  29. 15

#

3. 项目配置和部署

3.1 File -> Project Structure,进入创建项目配置窗口。

SouthEast

3.2 创建一个Tomcat容器实例,并把项目部署进去

Center 5

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

Center 6

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

Center 7

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

4. 值得注意的地方

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

Center 8

出现这个错误是因为少了依赖的jar包,只要在pom文件中添加如下依赖即可。

[html] view plain copy

  1. com.fasterxml.jackson.core
  2. jackson-core
  3. 2.8.0
  4. com.fasterxml.jackson.core
  5. jackson-databind
  6. 2.8.0

5. 项目下载地址

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

发表评论

表情:
评论列表 (有 0 条评论,396人围观)

还没有评论,来说两句吧...

相关阅读