1.基本概念
SSM框架指:Spring MVC + Spring + MyBatis
Spring MVC是一种web层mvc框架,用于替代servlet,处理|响应请求,获取表单参数,表单校验等
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。 (摘自百度百科)。
2.开发环境
安装java jdk,MySQL,IntelliJ IDEA,Tomcat,Maven
这是我电脑上的java和MySQL版本
Tomcat 我用的
IntelliJ IDEA我用的
Maven我用的3.6.0
3.搭建SSM框架
打开IntelliJ IDEA,选择 File》Project》New Project ,左侧栏选择 Maven ,右侧Project SDK 会是默认的java,选择Create from archetype,选择下图中的2
点击上图Next,进入下图,GroupId和ArtifactId是Maven项目的坐标,保证项目的唯一性。如果项目上传到了maven本地仓库去,必须根据这两个id去查找项目。
GroupId 一般分为多段,这里只说两段,第一段为域,第二段为公司名称。域包括:org、com、cn等,其中org为非营利组织,com为商业组织。
ArtifactId实际对应项目的名称,就是项目根目录的名称。这样说起来比较抽象,下面举例说明:
例如下图中:GroupId是cn.only,它的域是cn(因为cn表示中国),个人项目所以为only,ArtifactId是helloworld。 GroupId实际对应JAVA的包的结构,是main目录里java的目录结构。ArtifactId实际对应项目的名称。
点击上图Next,进入下图,选择本地Maven的位置,我的Maven版本是3.6.0
点击上图Next,进入下图,选择项目的存放地址,如下图,注意Project location中一定要有项目名字,项目名称默认
点击上图Finish,IntelliJ IDEA右下角弹窗如下图:
点击Enable Auto-Import,以后更改pom文件后就会自动下载依赖包了,所以点击Enable Auto-Import。
等待几分钟,项目正在下载相关依赖包,下载完毕以后,项目的目录结构如下图:
.idea:IntelliJ IDEA 在本项目中的配置,可以删掉,删掉后打开项目要重新配置。
src:存放项目代码
pom.xml:maven的配置文件,需要什么jar包,在里面添加依赖,maven会自动下到本地仓库。
现在构建SSM项目结构:
在src下建test文件夹,用来放测试文件,在test文件夹下建java和resources文件夹:
选择test的下的java文件夹,鼠标右键选择Mark Directory as》Test Sources Root
选择test的下的resources文件夹,鼠标右键选择Mark Directory as》Test Resources Root
在main文件夹下建java和resources文件夹:
选择main的下的java文件夹,鼠标右键选择Mark Directory as》Sources Root
选择main的下的resources文件夹,鼠标右键选择Mark Directory as》Resources Root
在src》main》java文件夹下建package,鼠标选择java文件夹,右键选择New》Package,建Package,名字就是上面GroupId,为cn.only
在cn.only下建4个Package:controller:用来放前台url映射文件,dao用来放与数据库的接口文件,entity用来放实体类文件,service用来放自己定义的接口
鼠标选择java,右键选择New》Package,建Package,名字为cn.only.controller,controller Package添加成功如下图
继续鼠标选择java,右键选择New》Package,建Package,名字为cn.only.dao,dao Package添加成功如下图
entity和service的添加方法一样如上,名字为cn.only.entity和cn.only.service,添加成功,如下图
src》main》resources下添加两个文件夹: spring和sqlmap,spring下添加spring的配置文件applicationContext.xml
src》main》resources下再添加四个配置文件:generator.properties ,generatorConfig.xml ,jdbc.properties 和 log4j.properties
generator.properties和generatorConfig.xml :用来配置mybatis
jdbc.properties :配置数据库相关信息
log4j.properties:配置日志
resources的目录结构如下图:
src》main》webapp下新建文件夹:js ,css,images 用来存放前端资源
src》main》webapp》WEB-INF下新建views文件夹,jsp页面放在这里。
webapp 的目录结构如下图:
整个项目的目录已经构建好,如下图:
下面开始配置项目
根目录下的pom.xml:POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示,名称叫做pom.xml。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等。在Maven中,project可以什么都没有,甚至没有代码,但是必须包含pom.xml文件。
打开pom.xml,会发现已经默认有一些配置了,下面是我的pom.xml配置文件:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
?xml version="1.0" encoding="UTF-8"? 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/xsd/maven-4.0.0.xsd" modelVersion4.0.0/modelVersion groupIdcn.only/groupId artifactIdhelloworld/artifactId version1.0-SNAPSHOT/version packagingwar/packaging namehelloworld Maven Webapp/name !-- FIXME change it to the project's website -- urlhttp://www.example.com/url properties project.build.sourceEncodingUTF-8/project.build.sourceEncoding maven.compiler.source1.7/maven.compiler.source maven.compiler.target1.7/maven.compiler.target !-- spring版本号 -- spring.version4.2.5.RELEASE/spring.version !-- mybatis版本号 -- mybatis.version3.2.4/mybatis.version !-- log4j日志文件管理包版本 -- slf4j.version1.6.6/slf4j.version log4j.version1.2.12/log4j.version /properties dependencies dependency groupIdjunit/groupId artifactIdjunit/artifactId version4.11/version scopetest/scope /dependency !-- spring核心包 -- !-- springframe start -- dependency groupIdorg.springframework/groupId artifactIdspring-core/artifactId version${spring.version}/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-web/artifactId version${spring.version}/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-oxm/artifactId version${spring.version}/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-tx/artifactId version${spring.version}/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-jdbc/artifactId version${spring.version}/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-webmvc/artifactId version${spring.version}/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-aop/artifactId version${spring.version}/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-context-support/artifactId version${spring.version}/version /dependency dependency groupIdorg.springframework/groupId artifactIdspring-test/artifactId version${spring.version}/version /dependency !-- springframe end -- !-- mybatis核心包 -- dependency groupIdorg.mybatis/groupId artifactIdmybatis/artifactId version${mybatis.version}/version /dependency !-- mybatis/spring包 -- dependency groupIdorg.mybatis/groupId artifactIdmybatis-spring/artifactId version1.2.2/version /dependency !-- mysql驱动包 -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version5.0.8/version /dependency !-- jstl -- dependency groupIdjavax.servlet/groupId artifactIdjstl/artifactId version1.2/version /dependency dependency groupIdjavax.servlet.jsp/groupId artifactIdjsp-api/artifactId version2.2.1-b03/version scopeprovided/scope /dependency !-- servlet -- dependency groupIdjavax.servlet/groupId artifactIdservlet-api/artifactId version2.5/version scopeprovided/scope /dependency !-- json数据 -- dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-core/artifactId version2.7.1/version /dependency dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-annotations/artifactId version2.7.1/version /dependency dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-databind/artifactId version2.7.1/version /dependency dependency groupIdnet.sf.json-lib/groupId artifactIdjson-lib/artifactId version2.4/version classifierjdk15/classifier /dependency !-- commons -- dependency groupIdcommons-lang/groupId artifactIdcommons-lang/artifactId version2.4/version /dependency dependency groupIdcommons-logging/groupId artifactIdcommons-logging/artifactId version1.1/version /dependency dependency groupIdcommons-pool/groupId artifactIdcommons-pool/artifactId version1.5.6/version /dependency dependency groupIdcommons-dbcp/groupId artifactIdcommons-dbcp/artifactId version1.4/version /dependency dependency groupIdcommons-beanutils/groupId artifactIdcommons-beanutils/artifactId version1.8.3/version /dependency dependency groupIdcommons-httpclient/groupId artifactIdcommons-httpclient/artifactId version3.1/version /dependency dependency groupIdcommons-collections/groupId artifactIdcommons-collections/artifactId version3.1/version /dependency dependency groupIdcommons-codec/groupId artifactIdcommons-codec/artifactId version1.9/version /dependency dependency groupIdjavax.annotation/groupId artifactIdjsr250-api/artifactId version1.0/version /dependency dependency groupIdnet.sf.ezmorph/groupId artifactIdezmorph/artifactId version1.0.6/version /dependency dependency groupIdjavax.activation/groupId artifactIdactivation/artifactId version1.1/version /dependency dependency groupIdtaglibs/groupId artifactIdstandard/artifactId version1.1.2/version /dependency !-- 日志文件管理包 -- !-- log start -- dependency groupIdlog4j/groupId artifactIdlog4j/artifactId version${log4j.version}/version /dependency dependency groupIdorg.slf4j/groupId artifactIdslf4j-api/artifactId version${slf4j.version}/version /dependency dependency groupIdorg.slf4j/groupId artifactIdslf4j-log4j12/artifactId version${slf4j.version}/version /dependency !--上传-- dependency groupIdcommons-fileupload/groupId artifactIdcommons-fileupload/artifactId version1.3.1/version /dependency dependency groupIdcommons-io/groupId artifactIdcommons-io/artifactId version2.4/version /dependency !-- log end -- /dependencies build finalNamehello/finalName pluginManagement!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -- plugins plugin artifactIdmaven-clean-plugin/artifactId version3.1.0/version /plugin !-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -- plugin artifactIdmaven-resources-plugin/artifactId version3.0.2/version /plugin plugin artifactIdmaven-compiler-plugin/artifactId version3.8.0/version /plugin plugin artifactIdmaven-surefire-plugin/artifactId version2.22.1/version /plugin plugin artifactIdmaven-war-plugin/artifactId version3.2.2/version /plugin plugin artifactIdmaven-install-plugin/artifactId version2.5.2/version /plugin plugin artifactIdmaven-deploy-plugin/artifactId version2.8.2/version /plugin /plugins /pluginManagement plugins !-- mybatis-generator -- plugin groupIdorg.mybatis.generator/groupId artifactIdmybatis-generator-maven-plugin/artifactId version1.3.2/version configuration !-- mybatis-generator的配置文件,根据情况调整位置 -- configurationFilesrc/main/resources/generatorConfig.xml/configurationFile verbosetrue/verbose overwritetrue/overwrite /configuration executions execution idGenerate MyBatis Artifacts/id goals goalgenerate/goal /goals /execution /executions dependencies dependency groupIdorg.mybatis.generator/groupId artifactIdmybatis-generator-core/artifactId version1.3.2/version /dependency /dependencies /plugin /plugins /build/project
?xml version=”1.0” encoding=”UTF-8”?
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/xsd/maven-4.0.0.xsd"
modelVersion4.0.0/modelVersion
groupIdcn.only/groupId
artifactIdhelloworld/artifactId
version1.0-SNAPSHOT/version
packagingwar/packaging
namehelloworld Maven Webapp/name
!– FIXME change it to the project’s website –
urlhttp://www.example.com/url
properties
project.build.sourceEncodingUTF-8/project.build.sourceEncoding
maven.compiler.source1.7/maven.compiler.source
maven.compiler.target1.7/maven.compiler.target
!– spring版本号 –
spring.version4.2.5.RELEASE/spring.version
!– mybatis版本号 –
mybatis.version3.2.4/mybatis.version
!– log4j日志文件管理包版本 –
slf4j.version1.6.6/slf4j.version
log4j.version1.2.12/log4j.version
/properties
dependencies
dependency
groupIdjunit/groupId
artifactIdjunit/artifactId
version4.11/version
scopetest/scope
/dependency
!– spring核心包 –
!– springframe start –
dependency
groupIdorg.springframework/groupId
artifactIdspring-core/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-web/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-oxm/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-tx/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-jdbc/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-webmvc/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-aop/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-context-support/artifactId
version${spring.version}/version
/dependency
dependency
groupIdorg.springframework/groupId
artifactIdspring-test/artifactId
version${spring.version}/version
/dependency
!-- springframe end --
!-- mybatis核心包 --
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis/artifactId
version${mybatis.version}/version
/dependency
!-- mybatis/spring包 --
dependency
groupIdorg.mybatis/groupId
artifactIdmybatis-spring/artifactId
version1.2.2/version
/dependency
!-- mysql驱动包 --
dependency
groupIdmysql/groupId
artifactIdmysql-connector-java/artifactId
version5.0.8/version
/dependency
!-- jstl --
dependency
groupIdjavax.servlet/groupId
artifactIdjstl/artifactId
version1.2/version
/dependency
dependency
groupIdjavax.servlet.jsp/groupId
artifactIdjsp-api/artifactId
version2.2.1-b03/version
scopeprovided/scope
/dependency
!-- servlet --
dependency
groupIdjavax.servlet/groupId
artifactIdservlet-api/artifactId
version2.5/version
scopeprovided/scope
/dependency
!-- json数据 --
dependency
groupIdcom.fasterxml.jackson.core/groupId
artifactIdjackson-core/artifactId
version2.7.1/version
/dependency
dependency
groupIdcom.fasterxml.jackson.core/groupId
artifactIdjackson-annotations/artifactId
version2.7.1/version
/dependency
dependency
groupIdcom.fasterxml.jackson.core/groupId
artifactIdjackson-databind/artifactId
version2.7.1/version
/dependency
dependency
groupIdnet.sf.json-lib/groupId
artifactIdjson-lib/artifactId
version2.4/version
classifierjdk15/classifier
/dependency
!-- commons --
dependency
groupIdcommons-lang/groupId
artifactIdcommons-lang/artifactId
version2.4/version
/dependency
dependency
groupIdcommons-logging/groupId
artifactIdcommons-logging/artifactId
version1.1/version
/dependency
dependency
groupIdcommons-pool/groupId
artifactIdcommons-pool/artifactId
version1.5.6/version
/dependency
dependency
groupIdcommons-dbcp/groupId
artifactIdcommons-dbcp/artifactId
version1.4/version
/dependency
dependency
groupIdcommons-beanutils/groupId
artifactIdcommons-beanutils/artifactId
version1.8.3/version
/dependency
dependency
groupIdcommons-httpclient/groupId
artifactIdcommons-httpclient/artifactId
version3.1/version
/dependency
dependency
groupIdcommons-collections/groupId
artifactIdcommons-collections/artifactId
version3.1/version
/dependency
dependency
groupIdcommons-codec/groupId
artifactIdcommons-codec/artifactId
version1.9/version
/dependency
dependency
groupIdjavax.annotation/groupId
artifactIdjsr250-api/artifactId
version1.0/version
/dependency
dependency
groupIdnet.sf.ezmorph/groupId
artifactIdezmorph/artifactId
version1.0.6/version
/dependency
dependency
groupIdjavax.activation/groupId
artifactIdactivation/artifactId
version1.1/version
/dependency
dependency
groupIdtaglibs/groupId
artifactIdstandard/artifactId
version1.1.2/version
/dependency
!-- 日志文件管理包 --
!-- log start --
dependency
groupIdlog4j/groupId
artifactIdlog4j/artifactId
version${log4j.version}/version
/dependency
dependency
groupIdorg.slf4j/groupId
artifactIdslf4j-api/artifactId
version${slf4j.version}/version
/dependency
dependency
groupIdorg.slf4j/groupId
artifactIdslf4j-log4j12/artifactId
version${slf4j.version}/version
/dependency
!--上传--
dependency
groupIdcommons-fileupload/groupId
artifactIdcommons-fileupload/artifactId
version1.3.1/version
/dependency
dependency
groupIdcommons-io/groupId
artifactIdcommons-io/artifactId
version2.4/version
/dependency
!-- log end --
/dependencies
build
finalNamehello/finalName
pluginManagement!– lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) –
plugins
plugin
artifactIdmaven-clean-plugin/artifactId
version3.1.0/version
/plugin
!– see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging –
plugin
artifactIdmaven-resources-plugin/artifactId
version3.0.2/version
/plugin
plugin
artifactIdmaven-compiler-plugin/artifactId
version3.8.0/version
/plugin
plugin
artifactIdmaven-surefire-plugin/artifactId
version2.22.1/version
/plugin
plugin
artifactIdmaven-war-plugin/artifactId
version3.2.2/version
/plugin
plugin
artifactIdmaven-install-plugin/artifactId
version2.5.2/version
/plugin
plugin
artifactIdmaven-deploy-plugin/artifactId
version2.8.2/version
/plugin
/plugins
/pluginManagement
plugins
!– mybatis-generator –
plugin
groupIdorg.mybatis.generator/groupId
artifactIdmybatis-generator-maven-plugin/artifactId
version1.3.2/version
configuration
!– mybatis-generator的配置文件,根据情况调整位置 –
configurationFilesrc/main/resources/generatorConfig.xml/configurationFile
verbosetrue/verbose
overwritetrue/overwrite
/configuration
executions
execution
idGenerate MyBatis Artifacts/id
goals
goalgenerate/goal
/goals
/execution
/executions
dependencies
dependency
groupIdorg.mybatis.generator/groupId
artifactIdmybatis-generator-core/artifactId
version1.3.2/version
/dependency
/dependencies
/plugin
/plugins
/build
/project
配置applicationContext.xml,我的配置如下:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
?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:beans="http://www.springframework.org/schema/beans" 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.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd" !-- ①:对com.springmvc包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -- context:component-scan base-package="com.springmvc"/ mvc:annotation-driven / !-- 静态资源访问 -- !--如果webapp下你新建了文件夹,想访问里面的静态资源,那么就要在这配置一下-- mvc:resources location="/images/" mapping="/images/**"/ mvc:resources location="/css/" mapping="/css/**"/ mvc:resources location="/js/" mapping="/js/**"/ !-- Configures the @Controller programming model mvc:annotation-driven /-- !-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -- bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" property name="messageConverters" list ref bean="mappingJacksonHttpMessageConverter"/ /list /property /bean bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" property name="supportedMediaTypes" list valueapplication/json;charset=UTF-8/value /list /property /bean !-- 配置视图解析器,把控制器的逻辑视频映射为真正的视图 -- !-- /WEB-INF/jsp/start.jsp -- bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/ property name="prefix" value="/WEB-INF/views/" / property name="suffix" value=".jsp" / /bean !-- 配置dbcp数据库连接池 -- !-- context:property-placeholder location="classpath:db.properties"/ -- !--数据库配置 -- bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" property name="locations" list valueclasspath:jdbc.properties/value /list /property /bean !-- 数据库连接池 -- bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" property name="driverClassName" value="${jdbc.driver}"/ property name="url" value="${jdbc.url}"/ property name="username" value="${username}"/ property name="password" value="shiotc123"/ property name="initialSize" value="1"/ property name="maxActive" value="100"/ property name="maxIdle" value="5"/ property name="maxWait" value="80000"/ /bean !-- 配置事务管理器 -- bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" property name="dataSource" ref="dataSource" / /bean !-- 拦截器 -- mvc:interceptors !-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 -- bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" / /mvc:interceptors !-- 定义无Controller的path-view直接映射 -- !-- mvc:view-controller path="/" view-name="redirect:/" / -- /beans
?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:beans=”http://www.springframework.org/schema/beans"
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.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"
!-- ①:对com.springmvc包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --
context:component-scan base-package="com.springmvc"/
mvc:annotation-driven /
!-- 静态资源访问 --
!--如果webapp下你新建了文件夹,想访问里面的静态资源,那么就要在这配置一下--
mvc:resources location="/images/" mapping="/images/**"/
mvc:resources location="/css/" mapping="/css/**"/
mvc:resources location="/js/" mapping="/js/**"/
!-- Configures the @Controller programming model
mvc:annotation-driven /--
!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --
bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
property name="messageConverters"
list
ref bean="mappingJacksonHttpMessageConverter"/
/list
/property
/bean
bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"
property name="supportedMediaTypes"
list
valueapplication/json;charset=UTF-8/value
/list
/property
/bean
!-- 配置视图解析器,把控制器的逻辑视频映射为真正的视图 --
!-- /WEB-INF/jsp/start.jsp --
bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/
property name="prefix" value="/WEB-INF/views/" /
property name="suffix" value=".jsp" /
/bean
!-- 配置dbcp数据库连接池 --
!-- context:property-placeholder location="classpath:db.properties"/ --
!--数据库配置 --
bean id = "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
property name="locations"
list
valueclasspath:jdbc.properties/value
/list
/property
/bean
!-- 数据库连接池 --
bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
property name="driverClassName" value="${jdbc.driver}"/
property name="url" value="${jdbc.url}"/
property name="username" value="${username}"/
property name="password" value="shiotc123"/
property name="initialSize" value="1"/
property name="maxActive" value="100"/
property name="maxIdle" value="5"/
property name="maxWait" value="80000"/
/bean
!-- 配置事务管理器 --
bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
property name="dataSource" ref="dataSource" /
/bean
!-- 拦截器 --
mvc:interceptors
!-- 国际化操作拦截器 如果采用基于(请求/Session/Cookie)则必需配置 --
bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /
/mvc:interceptors
!-- 定义无Controller的path-view直接映射 --
!-- mvc:view-controller path="/" view-name="redirect:/" / --
/beans
配置jdbc.properties
12345678910111213141516
jdbc.driver=com.mysql.jdbc.Driver#test为我本地的数据库名jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTCusername=root#下面输入自己数据库的密码password=*****#定义初始连接数initialSize=0#定义最大连接数maxActive=20#定义最大空闲maxIdle=20#定义最小空闲minIdle=1#定义最长等待时间maxWait=60000
jdbc.driver=com.mysql.jdbc.Driver
#test为我本地的数据库名
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username=root
#下面输入自己数据库的密码
password=*****
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
配置log4j.properties
123456789101112131415161718192021222324252627
### set log levels ###log4j.rootLogger = INFO,D ###输出到控制台###log4j.logger.toConsole=debug,stdoutlog4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %c -%F(%L) -%m%n ### 输出到日志文件 ##### 其中File 可以采用绝对路径 亦可以采用相对路径 catalina.home 是tomcat目录 如果没有logs文件夹 将会报错 #### 更加倾向于将日志文件放在 tomcat的日志目录下${catalina.home}/logs/salesmanb2bError.log #### 绝对路径如:e:/salesmanb2bError.log ##log4j.logger.daily=INFO,Dlog4j.appender.D = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.File = ${catalina.home}/logs/helloworld/helloworld.loglog4j.appender.D.Append = truelog4j.appender.D.Threshold = INFOlog4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern =%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %c -%F(%L) -%m%n ##log4j.logger.org.apache.ibatis=debug,stdout##log4j.logger.java.sql=debug,stdout
set log levels
log4j.rootLogger = INFO,D
###输出到控制台###
log4j.logger.toConsole=debug,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} [%5p] - %c -%F(%L) -%m%n
输出到日志文件
其中File 可以采用绝对路径 亦可以采用相对路径 catalina.home 是tomcat目录 如果没有logs文件夹 将会报错
更加倾向于将日志文件放在 tomcat的日志目录下${catalina.home}/logs/salesmanb2bError.log
绝对路径如:e:/salesmanb2bError.log
log4j.logger.daily=INFO,D
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${catalina.home}/logs/helloworld/helloworld.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern =%d{yyyy-MM-dd HH:mm:ss} [%5p] - %c -%F(%L) -%m%n
##log4j.logger.org.apache.ibatis=debug,stdout
##log4j.logger.java.sql=debug,stdout
配置web.xml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
?xml version="1.0" encoding="UTF-8"?web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" display-namehelloworld/display-name !-- 配置编码方式-- filter filter-nameencodingFilter/filter-name filter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-class init-param param-nameencoding/param-name param-valueUTF-8/param-value /init-param init-param param-nameforceEncoding/param-name param-valuetrue/param-value /init-param /filter filter-mapping filter-nameencodingFilter/filter-name url-pattern/*/url-pattern /filter-mapping !-- 配置springmvc的前端控制器 指向spring-mvc.xml 程序在启动的时候就加载springmvc 可以接受所有请求 load-on-startup:表示启动容器时初始化该Servlet; -- servlet servlet-namespringServlet/servlet-name servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class !-- 可以自定义servlet.xml配置文件的位置和名称, 默认为WEB-INF目录下,名称为[servlet-name]-servlet.xml,如spring-servlet.xml -- init-param param-namecontextConfigLocation/param-name param-value classpath:spring/applicationContext.xml/param-value /init-param load-on-startup1/load-on-startup /servlet !-- 将前端URL请求和后台处理方法controller建立对应关系-- servlet-mapping servlet-namespringServlet/servlet-name url-pattern//url-pattern /servlet-mapping !-- Spring配置 -- listener listener-class org.springframework.web.context.ContextLoaderListener /listener-class /listener !-- 取消对某一类文件的拦截-- servlet-mapping servlet-namedefault/servlet-name url-pattern*.md/url-pattern /servlet-mapping context-param param-namecontextConfigLocation/param-name param-value classpath:spring/applicationContext.xml/param-value /context-param !-- 欢迎页面-- welcome-file-list welcome-file/WEB-INF/index.jsp/welcome-file /welcome-file-list !--404错误展示页面,可自行配置-- !--error-page-- !--error-code404/error-code-- !--location/WEB-INF/views/404.jsp/location-- !--/error-page-- !--设置session失效时间为30分钟 -- session-config session-timeout600/session-timeout /session-config/web-app
?xml version=”1.0” encoding=”UTF-8”?
web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance" xmlns=”http://java.sun.com/xml/ns/javaee" xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version=”3.0”
display-namehelloworld/display-name
!– 配置编码方式–
filter
filter-nameencodingFilter/filter-name
filter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-class
init-param
param-nameencoding/param-name
param-valueUTF-8/param-value
/init-param
init-param
param-nameforceEncoding/param-name
param-valuetrue/param-value
/init-param
/filter
filter-mapping
filter-nameencodingFilter/filter-name
url-pattern/*/url-pattern
/filter-mapping
!– 配置springmvc的前端控制器 指向spring-mvc.xml 程序在启动的时候就加载springmvc 可以接受所有请求 load-on-startup:表示启动容器时初始化该Servlet; –
servlet
servlet-namespringServlet/servlet-name
servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class
!– 可以自定义servlet.xml配置文件的位置和名称, 默认为WEB-INF目录下,名称为[servlet-name]-servlet.xml,如spring-servlet.xml –
init-param
param-namecontextConfigLocation/param-name
param-value classpath:spring/applicationContext.xml/param-value
/init-param
load-on-startup1/load-on-startup
/servlet
!– 将前端URL请求和后台处理方法controller建立对应关系–
servlet-mapping
servlet-namespringServlet/servlet-name
url-pattern//url-pattern
/servlet-mapping
!– Spring配置 –
listener
listener-class
org.springframework.web.context.ContextLoaderListener
/listener-class
/listener
!– 取消对某一类文件的拦截–
servlet-mapping
servlet-namedefault/servlet-name
url-pattern*.md/url-pattern
/servlet-mapping
context-param
param-namecontextConfigLocation/param-name
param-value classpath:spring/applicationContext.xml/param-value
/context-param
!– 欢迎页面–
welcome-file-list
welcome-file/WEB-INF/index.jsp/welcome-file
/welcome-file-list
!–404错误展示页面,可自行配置–
!–error-page–
!–error-code404/error-code–
!–location/WEB-INF/views/404.jsp/location–
!–/error-page–
!–设置session失效时间为30分钟 –
session-config
session-timeout600/session-timeout
/session-config
/web-app
最后配置Tomcat,配置好Tomcat代码就可以跑起来了。选择Add Configuration…》弹出配置窗口,如下图
点击上图红框中的+,选择Tomcat Server》Local,弹出窗口,如下图,
配置Application server,在上图中选择红框中的configure…,弹出窗口如下图,选择Tomcat的安装路径,配置好Tomcat。
配置Deployment,如下图,
上图中,点击+,选择Artifact…,弹窗如下图,
上图中选择红框中内容,点击OK,如下图:
点击上图OK,Tomcat配置完毕。
如上图,点击绿色三角形,运行程序。
运行结果如下:
程序正常启动,spring mvc已经搭建好,下一篇文章配置mybatis-geneator,这是一款mybatis自动代码生成工具。