这里使用 Maven
项目管理工具构建项目
初始化项目
打开 Intellij IDEA,点击 Create New Project
,使用 Maven 构建项目,选择 JDK 版本和 maven-archetype-webapp
模板(Java Web
项目)
填写项目在 Maven 仓库中的坐标
选择 Maven 安装路径和 Maven 配置文件路径以及 Maven 本地仓库路径
填写项目名,选择工作目录
创建目录
在 src
> main
目录下分别新建 java
源码目录 和 resource
配置文件目录
在 java
目录下创建基本的源码目录结构
在 webapp
目录下创建 static
目录,用于存放静态资源文件(css, js, img 等)
在 webapp
> WEB-INF
目录下创建 views
目录,用于存放视图页面(jsp, html 等)
pom.xml
完整的 pom.xml
配置如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 <?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.0</modelVersion > <groupId > com.antoniopeng</groupId > <artifactId > ssm-example</artifactId > <version > 1.0-SNAPSHOT</version > <packaging > war</packaging > <name > hello-ssm Maven Webapp</name > <url > https://antoniopeng.com</url > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <project.reporting.outputEncoding > UTF-8</project.reporting.outputEncoding > <java.version > 1.8</java.version > <alibaba-druid.version > 1.1.6</alibaba-druid.version > <apache-httpclient.version > 4.5.5</apache-httpclient.version > <commons-email.version > 1.5</commons-email.version > <commons-fileupload.version > 1.3.2</commons-fileupload.version > <commons-lang3.version > 3.5</commons-lang3.version > <hibernate-validator.version > 5.3.4.Final</hibernate-validator.version > <jackson.version > 2.9.5</jackson.version > <jstl.version > 1.2</jstl.version > <junit.version > 4.12</junit.version > <kaptcha.version > 2.3</kaptcha.version > <log4j.version > 1.2.17</log4j.version > <lombok.version > 1.16.18</lombok.version > <mybaits-spring.version > 1.3.1</mybaits-spring.version > <mybatis.version > 3.2.8</mybatis.version > <mysql.version > 5.1.46</mysql.version > <servlet-api.version > 3.1.0</servlet-api.version > <slf4j.version > 1.7.25</slf4j.version > <spring.version > 4.3.17.RELEASE</spring.version > </properties > <dependencies > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-test</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > ${junit.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aspects</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > javax.servlet-api</artifactId > <version > ${servlet-api.version}</version > <scope > provided</scope > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > jstl</artifactId > <version > ${jstl.version}</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-api</artifactId > <version > ${slf4j.version}</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > slf4j-log4j12</artifactId > <version > ${slf4j.version}</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > jcl-over-slf4j</artifactId > <version > ${slf4j.version}</version > </dependency > <dependency > <groupId > org.slf4j</groupId > <artifactId > jul-to-slf4j</artifactId > <version > ${slf4j.version}</version > </dependency > <dependency > <groupId > log4j</groupId > <artifactId > log4j</artifactId > <version > ${log4j.version}</version > </dependency > <dependency > <groupId > org.apache.commons</groupId > <artifactId > commons-lang3</artifactId > <version > ${commons-lang3.version}</version > </dependency > <dependency > <groupId > commons-fileupload</groupId > <artifactId > commons-fileupload</artifactId > <version > ${commons-fileupload.version}</version > </dependency > <dependency > <groupId > org.apache.commons</groupId > <artifactId > commons-email</artifactId > <version > ${commons-email.version}</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > druid</artifactId > <version > ${alibaba-druid.version}</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > ${mysql.version}</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > ${mybatis.version}</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-spring</artifactId > <version > ${mybaits-spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.hibernate</groupId > <artifactId > hibernate-validator</artifactId > <version > ${hibernate-validator.version}</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-core</artifactId > <version > ${jackson.version}</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > ${jackson.version}</version > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-annotations</artifactId > <version > ${jackson.version}</version > </dependency > <dependency > <groupId > org.apache.httpcomponents</groupId > <artifactId > httpclient</artifactId > <version > ${apache-httpclient.version}</version > </dependency > <dependency > <groupId > org.apache.httpcomponents</groupId > <artifactId > fluent-hc</artifactId > <version > ${apache-httpclient.version}</version > </dependency > <dependency > <groupId > org.apache.httpcomponents</groupId > <artifactId > httpmime</artifactId > <version > ${apache-httpclient.version}</version > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > ${lombok.version}</version > </dependency > </dependencies > <build > <pluginManagement > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-install-plugin</artifactId > <version > 2.5.2</version > </plugin > </plugins > </pluginManagement > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <version > 3.7.0</version > <configuration > <source > ${java.version}</source > <target > ${java.version}</target > <encoding > ${project.build.sourceEncoding}</encoding > <showWarnings > true</showWarnings > </configuration > </plugin > </plugins > <resources > <resource > <directory > src/main/java</directory > <excludes > <exclude > **/*.java</exclude > </excludes > </resource > <resource > <directory > src/main/resources</directory > </resource > </resources > </build > </project >
全局配置文件
resources
目录下创建 ssm.properties
全局配置文件,供后续调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 jdbc.driverClass =com.mysql.jdbc.Driver jdbc.connectionURL =jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&useSSL=false jdbc.username =root jdbc.password =123456 jdbc.pool.init =1 jdbc.pool.minIdle =3 jdbc.pool.maxActive =20 web.view.prefix =/WEB-INF/views/ web.view.suffix =.jsp web.maxUploadSize =10485760
Spring 核心配置
resources
目录下创建 spring-context.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" > <context:annotation-config /> <context:component-scan base-package ="com.antoniopeng.hello.ssm" > <context:exclude-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> </context:component-scan > <bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name ="dataSource" ref ="dataSource" /> </bean > <tx:annotation-driven transaction-manager ="transactionManager" /> </beans >
整合 Spring MVC
创建 spring-mvc.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <?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:mvc ="http://www.springframework.org/schema/mvc" 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" > <context:property-placeholder ignore-unresolvable ="true" location ="classpath:ssm.properties" /> <context:component-scan base-package ="com.antoniopeng.hello.ssm.controller" use-default-filters ="false" > <context:include-filter type ="annotation" expression ="org.springframework.stereotype.Controller" /> </context:component-scan > <mvc:annotation-driven /> <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="prefix" value ="${web.view.prefix}" /> <property name ="suffix" value ="${web.view.suffix}" /> </bean > <mvc:resources mapping ="/static/**" location ="/static/" cache-period ="31536000" /> <mvc:interceptors > <mvc:interceptor > <mvc:mapping path ="/**" /> <mvc:exclude-mapping path ="/login" /> <mvc:exclude-mapping path ="/static/**" /> <bean class ="com.antoniopeng.hello.ssm.interceptor.LoginInterceptor" /> </mvc:interceptor > </mvc:interceptors > <bean id ="multipartResolver" class ="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name ="maxUploadSize" value ="${web.maxUploadSize}" /> </bean > </beans >
MyBatis 配置
创建 mybatis-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 <?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" > <configuration > <settings > <setting name ="logImpl" value ="STDOUT_LOGGING" /> <setting name ="cacheEnabled" value ="false" /> <setting name ="lazyLoadingEnabled" value ="true" /> <setting name ="aggressiveLazyLoading" value ="true" /> <setting name ="multipleResultSetsEnabled" value ="true" /> <setting name ="useColumnLabel" value ="true" /> <setting name ="useGeneratedKeys" value ="false" /> <setting name ="autoMappingBehavior" value ="PARTIAL" /> <setting name ="defaultExecutorType" value ="SIMPLE" /> <setting name ="mapUnderscoreToCamelCase" value ="true" /> <setting name ="localCacheScope" value ="SESSION" /> <setting name ="jdbcTypeForNull" value ="NULL" /> </settings > </configuration >
整合 Druid
resources
目录下创建 spring-context-druid.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <?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" 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" > <context:property-placeholder ignore-unresolvable ="true" location ="classpath:ssm.properties" /> <bean id ="dataSource" class ="com.alibaba.druid.pool.DruidDataSource" init-method ="init" destroy-method ="close" > <property name ="driverClassName" value ="${jdbc.driverClass}" /> <property name ="url" value ="${jdbc.connectionURL}" /> <property name ="username" value ="${jdbc.username}" /> <property name ="password" value ="${jdbc.password}" /> <property name ="initialSize" value ="${jdbc.pool.init}" /> <property name ="minIdle" value ="${jdbc.pool.minIdle}" /> <property name ="maxActive" value ="${jdbc.pool.maxActive}" /> <property name ="maxWait" value ="60000" /> <property name ="timeBetweenEvictionRunsMillis" value ="60000" /> <property name ="minEvictableIdleTimeMillis" value ="300000" /> <property name ="filters" value ="stat" /> </bean > </beans >
整合 MyBatis
resources
目录下创建 spring-context-mybatis.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" > <bean id ="sqlSessionFactory" class ="org.mybatis.spring.SqlSessionFactoryBean" > <property name ="dataSource" ref ="dataSource" /> <property name ="typeAliasesPackage" value ="com.antoniopeng.hello.ssm.entity" /> <property name ="mapperLocations" value ="classpath*:/mapper/**/*.xml" /> <property name ="configLocation" value ="classpath:/mybatis-config.xml" > </property > </bean > <bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name ="basePackage" value ="com.antoniopeng.hello.ssm.dao" /> </bean > </beans >
日志配置
创建 log4j.properties
日志配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 log4j.rootLogger =error, stdout log4j.appender.stdout =org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout =org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =[service] %d - %c -%-4r [%t] %-5p %c %x - %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.java.sql.Connection = debug log4j.logger.java.sql.Statement = debug log4j.logger.java.sql.PreparedStatement = debug log4j.logger.java.sql.ResultSet =debug log4j.logger.com.pro.mapper =debug
web.xml
完整的 web.xml
配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns ="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version ="3.1" > <welcome-file-list > <welcome-file > /login</welcome-file > </welcome-file-list > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:spring-context*.xml</param-value > </context-param > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <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 > <servlet > <servlet-name > springServlet</servlet-name > <servlet-class > org.springframework.web.servlet.DispatcherServlet</servlet-class > <init-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:spring-mvc.xml</param-value > </init-param > </servlet > <servlet-mapping > <servlet-name > springServlet</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > </web-app >
创建访问视图
在 webapp
> WEB-INF
> views
目录下新建 index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 <%@ page contentType ="text/html;charset=UTF-8" language ="java" %> <html > <head > <title > Index</title > </head > <body > <h1 > Hello SSM </h1 > </body > </html >
在 controller
目录下创建 IndexController
类
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.antoniopeng.hello.ssm.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller public class IndexController { @RequestMapping (value = "/" ) public String index () { return "index" ; } }
部署到 Tomcat 服务器
点击 Intellij IDEA
右上方 Add Configuration..
添加 Tomcat 本地服务器配置
配置下载好的 Tomcat 服务器
Tomcat8 官网下载地址
设置访问端口号,默认 8080
将项目添加到 Tomcat 服务器
最后运行项目,访问 http://localhost:8080/ 即可。
源码地址:https://github.com/antoniopeng/ssm-example.git
更多干货请移步:https://antoniopeng.com
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !