https://www.cnblogs.com/xuyiqing/p/9569459.html
1.需要用到的jar包:由于很多的jar包不好下载,我直接上传到百度网盘:
关于SSM框架:以前的博客有介绍过
Spring框架:
https://www.cnblogs.com/xuyiqing/category/1164340.html
SpringMVC框架:
https://www.cnblogs.com/xuyiqing/category/1269718.html
MyBatis框架:
https://www.cnblogs.com/xuyiqing/category/1181308.html
第一步:创建Dynamic Web Project
先把所有的jar包复制入lib文件夹:不同于传统java项目,这里会自动build path,总之复制进去就完事了
配置mybatis
:mybatis-config.xml:
<?
xml
version
=
“1.0”
encoding
=
“UTF-8”
?>
<!
DOCTYPE
configuration
PUBLIC
“-//mybatis.org//DTD Config 3.0//EN”
“http://mybatis.org/dtd/mybatis-3-config.dtd”
>
<!–
mybatis 核心配置文件
–>
<
configuration
>
<
settings
>
<!– changes from the defaults –>
<
setting
name
=
“lazyLoadingEnabled”
value
=
“false”
/>
</
settings
>
<
typeAliases
>
<!– 这里给实体类取别名,方便在
mapper
配置文件中使用
配置全局别名
–>
<
package
name
=
“com.
demo
.
pojo
”
/>
</
typeAliases
>
<!– <typeAliases>
<typeAlias type=”com.easysec.entity.User” alias=”user”/>
</typeAliases> –>
</
configuration
>
配置Spring
:applicationContextcontext-mybatis.xml
<?
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:aop
=
“http://www.springframework.org/schema/aop”
xmlns:p
=
“http://www.springframework.org/schema/p”
xmlns:tx
=
“http://www.springframework.org/schema/tx”
xmlns:context
=
“http://www.springframework.org/schema/context”
xsi:schemaLocation
=
”
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd”
>
<!– 扫描包使spring扫描包下的所有类,让标注spring注解的类生效 若扫描到有
@Component @Controller@Service等这些注解的类,则把这些类注册为bean
–>
<!– 配置Service扫描 –>
<
context:component-scan
base-package
=
“cn.smbms.services”
/>
<
context:component-scan
base-package
=
“cn.smbms.mapper”
/>
<!– 配置 读取
数据库配置
文件 –>
<
context:property-placeholder
location
=
“classpath:database.properties”
/>
<!– 配置 数据源 –>
<!– JNDI获取数据源(使用
dbcp
连接池) –>
<
bean
id
=
“dataSource”
class
=
“org.apache.commons.dbcp.BasicDataSource”
destroy-method
=
“close”
scope
=
“singleton”
>
<
property
name
=
“driverClassName”
value
=
“${driver}”
/>
<
property
name
=
“url”
value
=
“${url}”
/>
<
property
name
=
“username”
value
=
“${user}”
/>
<
property
name
=
“password”
value
=
“${password}”
/>
<
property
name
=
“initialSize”
value
=
“${initialSize}”
/>
<
property
name
=
“maxActive”
value
=
“${maxActive}”
/>
<
property
name
=
“maxIdle”
value
=
“${maxIdle}”
/>
<
property
name
=
“minIdle”
value
=
“${minIdle}”
/>
<
property
name
=
“maxWait”
value
=
“${maxWait}”
/>
<
property
name
=
“removeAbandonedTimeout”
value
=
“${removeAbandonedTimeout}”
/>
<
property
name
=
“removeAbandoned”
value
=
“${removeAbandoned}”
/>
<!–
sql
心跳 –>
<
property
name
=
“testWhileIdle”
value
=
“true”
/>
<
property
name
=
“testOnBorrow”
value
=
“false”
/>
<
property
name
=
“testOnReturn”
value
=
“false”
/>
<
property
name
=
“validationQuery”
value
=
“select 1”
/>
<
property
name
=
“timeBetweenEvictionRunsMillis”
value
=
“60000”
/>
<
property
name
=
“numTestsPerEvictionRun”
value
=
“${maxActive}”
/>
</
bean
>
<!– 配置
mybatis
SqlSessionFactory –>
<
bean
id
=
“sqlSessionFactory”
class
=
“org.mybatis.spring.SqlSessionFactoryBean”
>
<
property
name
=
“dataSource”
ref
=
“dataSource”
/>
<
property
name
=
“configLocation”
value
=
“classpath:mybatis-config.xml”
/>
</
bean
>
<!–
事务管理
–>
<
bean
id
=
“transactionManager”
class
=
“org.springframework.jdbc.datasource.DataSourceTransactionManager”
>
<
property
name
=
“dataSource”
ref
=
“dataSource”
/>
</
bean
>
<!–
AOP事务处理
开始
–>
<
aop:aspectj-autoproxy
/>
<
aop:config
proxy-target-class
=
“true”
>
<
aop:pointcut
expression
=
“execution(* *cn.
demo
.services..*(..))”
id
=
“transService”
/>
<
aop:advisor
pointcut-ref
=
“transService”
advice-ref
=
“txAdvice”
/>
</
aop:config
>
<
tx:advice
id
=
“txAdvice”
transaction-manager
=
“transactionManager”
>
<
tx:attributes
>
<
tx:method
name
=
“smbms*”
propagation
=
“REQUIRED”
rollback-for
=
“Exception”
/>
</
tx:attributes
>
</
tx:advice
>
<!–
AOP事务处理
结束
–>
<!– 配置Mapper扫描 –>
<
bean
class
=
“org.mybatis.spring.mapper.MapperScannerConfigurer”
>
<!– 设置Mapper扫描包 –>
<
property
name
=
“basePackage”
value
=
“cn.smbms.mapper”
/>
</
bean
>
</
beans
>
配置SpringMVC
:springmvc-servlet.xml
<?
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:mvc
=
“http://www.springframework.org/schema/mvc”
xmlns:p
=
“http://www.springframework.org/schema/p”
xmlns:context
=
“http://www.springframework.org/schema/context”
xsi:schemaLocation
=
”
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
” >
http://www.springframework.org/schema/mvc/spring-mvc.xsd”
>
<!– 配置Controller扫描 –>
<
context:component-scan
base-package
=
“com.
demo
.controller”
/>
<!– 配置注解驱动 –>
<
mvc:annotation-driven
>
<
mvc:message-converters
>
<
bean
class
=
“org.springframework.http.converter.StringHttpMessageConverter”
>
<
property
name
=
“supportedMediaTypes”
>
<
list
>
<
value
>
application/
json
;
charset
=UTF-8
</
value
>
</
list
>
</
property
>
</
bean
>
<
bean
class
=
“com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter”
>
<
property
name
=
“supportedMediaTypes”
>
<
list
>
<
value
>
text/
html
;
charset
=UTF-8
</
value
>
<
value
>
application/
json
</
value
>
</
list
>
</
property
>
<
property
name
=
“features”
>
<
list
>
<!– Date的日期转换器 –>
<
value
>
WriteDateUseDateFormat
</
value
>
</
list
>
</
property
>
</
bean
>
</
mvc:message-converters
>
</
mvc:annotation-driven
>
<!– 对静态资源放行 –>
<!–不需要拦截的 资源映射 –>
<
mvc:resources
location
=
“/js/”
mapping
=
“/js/**”
></
mvc:resources
>
<
mvc:resources
location
=
“/css/”
mapping
=
“/css/**”
></
mvc:resources
>
<
mvc:resources
location
=
“/images/”
mapping
=
“/images/**”
></
mvc:resources
>
<
mvc:resources
location
=
“/Charts/”
mapping
=
“/Charts/**”
></
mvc:resources
>
<
mvc:resources
location
=
“/jasper/”
mapping
=
“/jasper/**”
></
mvc:resources
>
<!– 配置视图解析器 –>
<!– 配置多视图解析器:允许同样的内容数据呈现不同的view –>
<
bean
class
=
“org.springframework.web.servlet.view.ContentNegotiatingViewResolver”
>
<
property
name
=
“favorParameter”
value
=
“true”
/>
<
property
name
=
“defaultContentType”
value
=
“text/html”
/>
<
property
name
=
“mediaTypes”
>
<
map
>
<
entry
key
=
“html”
value
=
“text/html;charset=UTF-8”
/>
<
entry
key
=
“json”
value
=
“application/json;charset=UTF-8”
/>
<
entry
key
=
“xml”
value
=
“application/xml;charset=UTF-8”
/>
</
map
>
</
property
>
<
property
name
=
“viewResolvers”
>
<
list
>
<
bean
id
=
“aaa”
class
=
“org.springframework.web.servlet.view.InternalResourceViewResolver”
>
<
property
name
=
“prefix”
value
=
“/WEB-INF/jsp/”
/>
<
property
name
=
“suffix”
value
=
“.jsp”
/>
</
bean
>
</
list
>
</
property
>
</
bean
>
<!– 配置
intercepters
拦截器
–>
<
mvc:interceptors
>
<
mvc:interceptor
>
<
mvc:mapping
path
=
“/sys/**”
/>
<
bean
class
=
“com.
demo
.interceptor.SysInterceptor”
/>
</
mvc:interceptor
>
</
mvc:interceptors
>
<!– 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver –>
<
bean
id
=
“multipartResolver”
class
=
“org.springframework.web.multipart.commons.CommonsMultipartResolver”
>
<
property
name
=
“maxUploadSize”
value
=
“5000000”
/>
<
property
name
=
“defaultEncoding”
value
=
“UTF-8”
/>
</
bean
>
</
beans
>
配置web.xml
<?
xml
version
=
“1.0”
encoding
=
“UTF-8”
?>
<
web-app
version
=
“3.0”
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”
>
<
display-name
></
display-name
>
<!– 欢迎页面 –>
<
welcome-file-list
>
<
welcome-file
>
login.jsp
</
welcome-file
>
<!– 有可能要修改 –>
</
welcome-file-list
>
<!– 加载spring核心配置文件 –>
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
classpath:applicationContext-*.
xml
</
param-value
>
<!– 有可能
要修改 –>
</
context-param
>
<!– 字符编码过滤器
解决post的乱码问题
–>
<
filter
>
<
filter-name
>
encodingFilter
</
filter-name
>
<
filter-class
>
org.springframework.web.filter.CharacterEncodingFilter
</
filter-class
>
<
init-param
>
<
param-name
>
encoding
</
param-name
>
<
param-value
>
UTF-8
</
param-value
>
</
init-param
>
<
init-param
>
<
param-name
>
forceEncoding
</
param-name
>
<
param-value
>
true
</
param-value
>
</
init-param
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
encodingFilter
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
<!– 配置springMVC的核心控制器 DispatcherServlet–>
<
servlet
>
<
servlet-name
>
spring
</
servlet-name
>
<
servlet-class
>
org.springframework.web.servlet.DispatcherServlet
</
servlet-class
>
<
init-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
classpath:springmvc-servlet.xml
</
param-value
>
<!– 有可能要修改 –>
</
init-param
>
<!– 配置springmvc什么时候启动,参数必须为整数 –>
<!– 如果为0或者大于0,则springMVC随着TOMCAT容器启动而启动 –>
<!– 如果小于0,则在第一次请求进来的时候启动 –>
<
load-on-startup
>
1
</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
spring
</
servlet-name
>
<
url-pattern
>
/
</
url-pattern
>
</
servlet-mapping
>
<!– 监听器 –>
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<!– 配置日志 –>
<
context-param
>
<
param-name
>
log4jConfigLocation
</
param-name
>
<
param-value
>
classpath:log4j.properties
</
param-value
>
</
context-param
>
<
context-param
>
<
param-name
>
webAppRootKey
</
param-name
>
<
param-value
>
SMBMS_C12_09.root
</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>
org.springframework.web.util.Log4jConfigListener
</
listener-class
>
</
listener
>
</
web-app
>
数据库配置
:datebase.properties用户名,数据库名,连接配置等等
driver=
com.mysql.jdbc.Driver
url=
jdbc:mysql://127.0.0.1:3306/
smbms
?useUnicode=true&characterEncoding=
utf
-8
user=
root
password=
root
minIdle=
45
maxIdle=
50
initialSize=
5
maxActive=
100
maxWait=
100
removeAbandonedTimeout=
180
removeAbandoned=
true
日志文件配置
:log4j.properties:
log4j.rootLogger=
debug,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE
log4j.logger.cn.smbms=
debug
log4j.logger.org.apache.ibatis=
debug
log4j.logger.org.mybatis.spring=
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
######################################################################################
# Console Appender \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.CONSOLE=
org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=
debug
log4j.appender.CONSOLE.DatePattern=
yyyy
-MM-
dd
log4j.appender.CONSOLE.Target=
System.out
log4j.appender.CONSOLE.layout=
org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=
–
(%r
ms
)
–
%d{
yyyy
-M-d
HH:mm:ss}%x[%5p](%F:%L)
%m%n
######################################################################################
# Rolling File \u6587\u4ef6\u5927\u5c0f\u5230\u8fbe\u6307\u5b9a\u5c3a\u5bf8\u7684\u65f6\u5019\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6
######################################################################################
#log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
#log4j.appender.ROLLING_FILE.Threshold=INFO
#log4j.appender.ROLLING_FILE.File=${baojia.root}/logs/log.log
#log4j.appender.ROLLING_FILE.Append=true
#log4j.appender.ROLLING_FILE.MaxFileSize=5000KB
#log4j.appender.ROLLING_FILE.MaxBackupIndex=100
#log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
#log4j.appender.ROLLING_FILE.layout.ConversionPattern=%d{
yyyy
-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
######################################################################################
# DailyRolling File \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=
org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=
yyyy
-MM-
dd
log4j.appender.file.File=
${SMBMS_C12_10.root}/logs/log.log
log4j.appender.file.Append=
true
log4j.appender.file.Threshold=
debug
log4j.appender.file.layout=
org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=
–
(%r
ms
)
–
%d{
yyyy
-M-d
HH:mm:ss}%x[%5p](%F:%L)
%m%n
#DWR \u65e5\u5fd7
#log4j.logger.org.directwebremoting = ERROR
#\u663e\u793aHibernate\u5360\u4f4d\u7b26\u7ed1\u5b9a\u503c\u53ca\u8fd4\u56de\u503c
#log4j.logger.org.hibernate.type=DEBUG,CONSOLE
#log4j.logger.org.springframework.transaction=DEBUG
#log4j.logger.org.hibernate=DEBUG
#log4j.logger.org.acegisecurity=DEBUG
#log4j.logger.org.apache.myfaces=TRACE
#log4j.logger.org.quartz=DEBUG
#log4j.logger.com.opensymphony=INFO
#log4j.logger.org.apache.struts2=DEBUG
log4j.logger.com.opensymphony.xwork2=
debug
至于resource.properties是后边项目需要的,和SSM框架无关
最后:Get方式乱码修改方式:
打开Tomcat的配置文件修改一个URIEncoding属性
<
Connector
URIEncoding
=”UTF-8″
connectionTimeout
=”20000″
port
=”8080″
protocol
=”HTTP/1.1″
redirectPort
=”8443″/>