ssm框架集成详解
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
- <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">
4.0.0 com.heitian.web web-ssm war 1.0-SNAPSHOT web-ssm Maven Webapp http://maven.apache.org UTF-8 UTF-8 4.2.5.RELEASE 3.2.8 5.1.29 1.7.18 1.2.17 jstl jstl 1.2 javax javaee-api 7.0 junit junit 4.11 test org.springframework spring-core ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-oxm ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-context-support ${spring.version} org.springframework spring-aop ${spring.version} org.springframework spring-test ${spring.version} org.mybatis mybatis ${mybatis.version} org.mybatis mybatis-spring 1.2.2 mysql mysql-connector-java ${mysql-driver.version} commons-dbcp commons-dbcp 1.2.2 com.alibaba fastjson 1.1.41 log4j log4j ${log4j.version} org.slf4j slf4j-api ${slf4j.version} org.slf4j slf4j-log4j12 ${slf4j.version} org.codehaus.jackson jackson-mapper-asl 1.9.13 com.fasterxml.jackson.core jackson-core 2.8.0 com.fasterxml.jackson.core jackson-databind 2.8.0 commons-fileupload commons-fileupload 1.3.1 commons-io commons-io 2.4 commons-codec commons-codec 1.9 web-ssm
2.2 新建jdbc.properties文件
jdbc.properties
[html] view plain copy
- driverClasss=com.mysql.jdbc.Driver
- jdbcUrl=jdbc
//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
[html] view plain copy
- 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
[html] view plain copy
- <?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">
- <bean id=”mappingJacksonHttpMessageConverter”
- class=”org.springframework.http.converter.json.MappingJackson2HttpMessageConverter”>
text/html;charset=UTF-8
2.5 新建spring和mybatis整合所需的配置文件
spring-mybatis.xml
[html] view plain copy
- <?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">
- <!— 第二种方式:加载多个properties文件
classpath:jdbc.properties classpath:common.properties - —>
- <bean id=”dataSource” class=”org.apache.commons.dbcp.BasicDataSource”
- destroy-method=”close”>
2.6 web.xml配置文件
web.xml
[html] view plain copy
- <?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”>
web-ssm contextConfigLocation classpath:spring-mybatis.xml log4jConfigLocation classpath:log4j.properties encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 encodingFilter /* org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorCleanupListener SpringMVC org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-mvc.xml 1 true SpringMVC / /index.jsp 15
#
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
com.fasterxml.jackson.core jackson-core 2.8.0 com.fasterxml.jackson.core jackson-databind 2.8.0
5. 项目下载地址
项目下载的地址为:http://download.csdn.net/detail/gallenzhang/9580035
还没有评论,来说两句吧...