Spring、SpringMVC和Mybatis已经是非常成熟的框架了,使用该框架开发已有些时日了,从今天开始呢就对用此框架开发的过程进行一系列的总结,旨在个人总结中提升自己,同时能帮到需要的人就更好了(大牛可忽略此文)。本文将着重介绍项目的搭建整合。
项目git地址:https://github.com/gitcaiqing/SSM_DEMO.git
1 Maven Web项目创建
2 SSM整合
项目结构如下图:
2.1 web.xml配置
webapp下创建WEB-INF然后创建web.xml文件,文件内容如下:
SSM整合项目Demo
contextConfigLocation
classpath*:spring/*.xml,classpath*:servlet/*.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
forceEncoding
true
encodingFilter
/*
springMvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:servlet/*.xml
1
springMvc
/
30
index.jsp
2.2 Maven引入需要的JAR包
4.0.0
com.ssm
SSM_DEMO
0.0.1-SNAPSHOT
war
SSM整合项目Demo
UTF-8
4.1.4.RELEASE
2.5.0
1.2.3
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-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
3.2.8
org.mybatis
mybatis-spring
1.2.2
mysql
mysql-connector-java
5.1.34
com.alibaba
druid
1.0.11
org.aspectj
aspectjweaver
1.8.4
log4j
log4j
1.2.17
org.slf4j
slf4j-api
1.7.7
org.slf4j
slf4j-log4j12
1.7.7
javax.servlet
javax.servlet-api
3.0.1
provided
javax.servlet
jstl
1.2
org.codehaus.jackson
jackson-mapper-asl
1.9.13
com.fasterxml.jackson.core
jackson-annotations
${jackson.version}
com.fasterxml.jackson.core
jackson-core
${jackson.version}
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
com.fasterxml.jackson.dataformat
jackson-dataformat-xml
${jackson.version}
com.google.code.gson
gson
2.8.0
commons-io
commons-io
2.4
commons-fileupload
commons-fileupload
1.2.2
commons-collections
commons-collections
3.2.1
commons-codec
commons-codec
1.6
org.hibernate
hibernate-validator
5.2.1.Final
net.sf.json-lib
json-lib
2.3
jdk15
net.sf.ezmorph
ezmorph
1.0.5
org.apache.commons
commons-lang3
3.3.2
org.apache.maven.plugins
maven-surefire-plugin
true
2.18.1
maven-war-plugin
WebRoot
WebRoot
WEB-INF/lib/javax.annotation-api-1.2.jar
2.4
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.7
2.3、数据库连接和Log4j配置
src-main-resource下创建config文件夹,然后创建jdbc.properties文件,文件内容如下:
#连接驱动
jdbc.driverClassName=com.mysql.jdbc.Driver
#主库
jdbc.url = jdbc\:mysql\://localhost\:3306/db_ssmdemo?useUnicode\=true&characterEncoding\=UTF-8&allowMultiQueries\=true
jdbc.username = root
jdbc.password = 123456
jdbc.initialSize=2
jdbc.maxActive=5
jdbc.maxIdle=5
jdbc.minIdle=1
jdbc.validationQuery=select 1
src-main-resource创建文件log4j.properties,文件内容如下:
log4j.rootLogger = DEBUG,stdout,A1,A2
### STDOUT
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss}][%p] %m %n
### INFO
log4j.appender.A1 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File =../logs/info.log
log4j.appender.A1.Append = true
log4j.appender.A1.Threshold =INFO
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss}][%p] %m %n
###ERROR
log4j.appender.A2 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.File =../logs/error.log
log4j.appender.A2.Append = true
log4j.appender.A2.Threshold =ERROR
log4j.appender.A2.layout = org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss}][%p] %m %n
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
2.4Spring配置
src-main-resource下创建spring文件夹,然后创建applicationContext.xml文件
2.5数据源配置
spring文件加下创建dataAccessContext.xml文件
数据库、事务配置
2.6 Mybatis配置
spring文件加下创建mybatis.xml文件,该文件为spring和mybatis整合内容
classpath:sql/*.xml
2.7 整合SpringMVC
src-main-resource下创建servlet文件夹,然后创建spring-mvc.xml文件
UTF-8
104857600
4096
2.8创建index.jsp页面
在webapp创建index.jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Insert title here
index.jsp
2.9 创建一个测试Controller,类为HelloController
package com.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("test")
public class HelloController {
@RequestMapping("hello")
@ResponseBody
public String hello() {
return "hello";
}
}
访问http://IP地址:端口号/SSM_DEMO/test/hello,即可查看返回值,正常返回,说明项目搭建成功