目前项目大都开始采用SSM结构进行搭建,因为涉及项目比较多,新来的需求都是从现有项目中迁移一份出来进行修改,有的时候两个项目差别还是比较大,并不完全需要原有项目的东西,进行删减也是一项费神费时的事情,这里拉一个简单的demo,后续新的需求直接改用该项目。
具体如下几个步骤,搭建一个简单的基于maven的SSM Demo
1、创建maven项目
而后按照next下一步设置相关的目录等,搭建完的初始项目结构如下图所示:
2、增加java、resources等目录
增加test、java、resources目录,并设置成对应的资源文件,操作如下图:
3、修改pom文件,引入spring、mybatis等jar文件
这里根据以往项目的需求,引入了常用的jar,具体如下:
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"> <modelVersion>4.0.0modelVersion> <groupId>com.modulegroupId> <artifactId>com.moduleartifactId> <version>1.0-SNAPSHOTversion> <packaging>warpackaging> <name>com.module Maven Webappname> <url>http://www.example.comurl> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <maven.compiler.source>1.7maven.compiler.source> <maven.compiler.target>1.7maven.compiler.target> <spring.version>4.3.12.RELEASEspring.version> <mybatis.version>3.2.6mybatis.version> <slf4j.version>1.7.7slf4j.version> <log4j.version>1.2.17log4j.version> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-oxmartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-txartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aopartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-context-supportartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>${mybatis.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>1.2.2version> dependency> <dependency> <groupId>javaxgroupId> <artifactId>javaee-apiartifactId> <version>7.0version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.30version> dependency> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-dbcp2artifactId> <version>2.1.1version> dependency> <dependency> <groupId>jstlgroupId> <artifactId>jstlartifactId> <version>1.2version> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>${log4j.version}version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>fastjsonartifactId> <version>1.2.23version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-apiartifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-log4j12artifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>com.fasterxml.jackson.coregroupId> <artifactId>jackson-databindartifactId> <version>2.5.3version> dependency> <dependency> <groupId>commons-fileuploadgroupId> <artifactId>commons-fileuploadartifactId> <version>1.3.1version> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> <version>2.4version> dependency> <dependency> <groupId>commons-codecgroupId> <artifactId>commons-codecartifactId> <version>1.9version> dependency> <dependency> <groupId>commons-beanutilsgroupId> <artifactId>commons-beanutilsartifactId> <version>1.9.3version> dependency> <dependency> <groupId>com.google.code.gsongroupId> <artifactId>gsonartifactId> <version>2.8.0version> dependency> <dependency> <groupId>org.jsoupgroupId> <artifactId>jsoupartifactId> <version>1.10.2version> dependency> <dependency> <groupId>com.github.pengglegroupId> <artifactId>kaptchaartifactId> <version>2.3.2version> dependency> <dependency> <groupId>org.apache.httpcomponentsgroupId> <artifactId>httpclientartifactId> <version>4.5.2version> dependency> <dependency> <groupId>org.apache.httpcomponentsgroupId> <artifactId>httpcoreartifactId> <version>4.4.5version> dependency> <dependency> <groupId>org.apache.httpcomponentsgroupId> <artifactId>httpmimeartifactId> <version>4.5.2version> dependency> <dependency> <groupId>org.apache.httpcomponentsgroupId> <artifactId>httpclient-cacheartifactId> <version>4.5.2version> dependency> dependencies> <build> <finalName>com.modulefinalName> <resources> <resource> <directory>src/main/javadirectory> <includes> <include>**/*.xmlinclude> includes> resource> resources> build> project>
4、添加日志文件、jdbc、spring-mvc.xml及spring-mybatis.xml等文件
log4j.properties文件内容为:
#日志输出级别,控制台,debug文件,error文件
log4j.rootLogger = info,stdout,D,E
#定义日志输出目的地为控制台
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 = [%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
#指定输出目录 到tomcat/logs下
log4j.appender.D.File = ../logs/webtest-debug.log
#定义文件最大大小
log4j.appender.D.MaxFileSize = 10MB
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
#指定输出目录 到tomcat/logs下
log4j.appender.E.File = ../logs/webtest-error.log
#定义文件最大大小
log4j.appender.E.MaxFileSize = 10MB
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.logger.org.springframework=ERROR
#显示SQL语句部分
#log4j.logger.com.mybatis=DEBUG
#log4j.logger.com.mybatis.common.jdbc.SimpleDataSource=DEBUG
#log4j.logger.com.mybatis.common.jdbc.ScriptRunner=DEBUG
#log4j.logger.com.mybatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG,stdout
log4j.logger.java.sql.Statement=DEBUG,stdout
log4j.logger.java.sql.PreparedStatement=DEBUG,stdout
jdbc.properties文件内容为:
driver=com.mysql.jdbc.Driver #开发环境库 url-test=jdbc:mysql://IP(替换成数据库ip地址):3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true username-test=root password-test=pass123 #定义初始连接数 initialSize=0 #定义最大连接数 maxTotal=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWaitMillis=60000 #空闲回收期运行周期(60秒) timeBetweenEvictionRunsMillis=60000
spring-mvc.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: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-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="com.zhouyh.controller" /> <context:component-scan base-package="com.zhouyh.service" /> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8value> list> property> bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJackson2HttpMessageConverter" /> list> property> bean> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/pages/" /> <property name="suffix" value=".jsp" /> bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="1" /> bean> <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha"> <property name="config"> <bean class="com.google.code.kaptcha.util.Config"> <constructor-arg type="java.util.Properties"> <props> <prop key="kaptcha.border">noprop> <prop key="kaptcha.image.width">100prop> <prop key="kaptcha.image.height">42prop> <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoiseprop> <prop key="kaptcha.obscurificator.impl">com.boryou.yuqing.util.CaptchaRenderprop> <prop key="kaptcha.background.clear.from">215,235,241prop> <prop key="kaptcha.background.clear.to">215,235,241prop> <prop key="kaptcha.textproducer.char.string">0123456789prop> <prop key="kaptcha.textproducer.char.length">5prop> <prop key="kaptcha.textproducer.font.color">0,161,233prop> <prop key="kaptcha.textproducer.font.size">30prop> props> constructor-arg> bean> property> bean> beans>
spring-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: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-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> bean> <bean id="dataSource-base" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="initialSize" value="${initialSize}"/> <property name="maxTotal" value="${maxTotal}"/> <property name="maxIdle" value="${maxIdle}"/> <property name="minIdle" value="${minIdle}"/> <property name="maxWaitMillis" value="${maxWaitMillis}"/> <property name="testWhileIdle" value="true"/> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/> <property name="validationQuery" value="SELECT 1" /> <property name="testOnBorrow" value="false"/> bean> <bean id="dataSource-test" parent="dataSource-base"> <property name="url" value="${url-test}" /> <property name="username" value="${username-test}" /> <property name="password" value="${password-test}" /> bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource-test" /> <property name="configLocation" value="classpath:sqlMapConfig.xml">property> <property name="mapperLocations" value="classpath:com/zhouyh/mapper/*.xml">property> bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zhouyh.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property> bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource-test" /> bean> beans>
5、根据需要添加controller、dao、mapper、service等文件,整合web.xml文件
web.xml文件内容为
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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-name>Archetype Created Web Applicationdisplay-name> <welcome-file-list> <welcome-file>index.jspwelcome-file> welcome-file-list> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:spring-mybatis.xmlparam-value> context-param> <servlet> <servlet-name>SpringMVCservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:spring-mvc.xmlparam-value> init-param> <load-on-startup>1load-on-startup> <async-supported>trueasync-supported> servlet> <servlet-mapping> <servlet-name>SpringMVCservlet-name> <url-pattern>/url-pattern> servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class> listener> web-app>
6、最后完成项目整体结构如图所示:
至此,简单的Demo已经完成。
代码上传到了GitHub上,链接为:MavenWebDemo