1 IDEA社区版环境搭建
1.1 Tomcat安装
- 下载安装Tomcat:
Tomcat下载地址:https://tomcat.apache.org/,选择Tomcat9,选择64-bit Windows.zip下载。
- 下载文件名为apache-tomcat-9.0.62.zip。Tomcat9.0需要安装JDK8+的版本。解压压缩包到D:\apache目录下,配置环境变量:
CATALINA_HOME = D:\apache\apache-tomcat-9.0.62
在“系统变量”中找到Path变量,双击打开Path变量,在“变量值”的最后面添加:
%CATALINA_HOME%\bin %CATALINA_HOME%\lib
在“系统变量”中找到CLASSPATH,在末尾处添加如下配置:
%CATALINA_HOME%\lib\servlet-api.jar;
进入Tomcat目录下的bin目录,输入cmd。如D:\apache\apache-tomcat-9.0.62\bin>目录下打开cmd命令行,在控制台输入
catalina run
过程如下:
D:\apache\apache-tomcat-9.0.62\bin>catalina run Using CATALINA_BASE: "D:\apache\apache-tomcat-9.0.62" Using CATALINA_HOME: "D:\apache\apache-tomcat-9.0.62" Using CATALINA_TMPDIR: "D:\apache\apache-tomcat-9.0.62\temp" Using JRE_HOME: "D:\hadoop\jdk1.8.0_181" Using CLASSPATH: "D:\apache\apache-tomcat-9.0.62\bin\bootstrap.jar;D:\apache\apache-tomcat-9.0.62\bin\tomcat-juli.jar" Using CATALINA_OPTS: ""
在浏览器打开
http://localhost:8080/ • 1
即可打开Tomcat首页,如下图所示,表示Tomcat安装成功。
关闭终端Tomcat9服务,以免IEDA运行时出现端口冲突。
1.2 IDEA社区版配置Tomcat
IDEA2020社区版本默认不包含tomcat,需要手动安装tomcat插件。打开IDEA。需要选择File–Settings–plugins–搜索tomcat–选择Marketplace–选择Smart Tomcat–Installed–点击ok–安装后重启IDEA,如图所示。
2 SSM整合
2.1 IDEA创建工程:
打开IDEA,选择File–new–project–选择maven工程–输入工程名称chapter15和GAV坐标(groupId为edu.hncj.ssm,artifactId为chapter15,version为默认)见图所示,输入后选择Finish。
在pom.xml文件中添加SSM的相关依赖如下:
<?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>edu.hncj.ssm</groupId> <artifactId>chapter15</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring.version>5.3.1</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <!--springmvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</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-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- Mybatis核心 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <!--mybatis和spring的整合包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <!-- junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- MySQL驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <!-- log4j日志 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency> <!-- 日志 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- ServletAPI --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- Spring5和Thymeleaf整合包 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency> </dependencies> </project>
在java下新建如下包:
edu.hncj.ssm.controller
edu.hncj.ssm.service
edu.hncj.ssm.service.impl
edu.hncj.ssm.mapper
edu.hncj.ssm.pojo
工程目录结构设置:
由于社区版不支持web工程,需要手动创建webapp目录,工程创建完毕后,在工程的src.main目录下创建webapp目录,在webapp目录下创建WEB-INF目录,在WEB-INF目录下创建web.xml文件,内容如下:
<?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_4_0.xsd" version="4.0"> <!-- 配置Spring的编码过滤器 --> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置处理请求方式PUT和DELETE的过滤器 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置SpringMVC的前端控制器 --> <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 设置SpringMVC的配置文件的位置和名称 --> <init-param> <param-name>contextConfigLocation</param-name> <!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的src/main/resources --> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 作为框架的核心组件,在启动过程中有大量的初始化操作要做 而这些操作放在第一次请求时才执行会严重影响访问速度 因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <!-- 设置springMVC的核心控制器所能处理的请求的请求路径 /所匹配的请求可以是/login或.html或.js或.css方式的请求路径 但是/不能匹配.jsp请求路径的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 设置Spring的配置文件的位置和名称 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- 配置Spring的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
在手动添加tomcat下的servlet-api.jar到模块下,过程为File–Project Structure–Moudles–选择工程名chapter15–选择Dependencies–选择+号–添加tomcat所在目录的lib目录下的servlet-api.jar。
2.2 IDEA配置 smart tomcat
IDEA配置tomcat,选择Add Configuration–点击+号–选择smart tomcat–设置tomcat的相关参数–点击ok,默认的名称为Unnamed,如下图所示:
2.3 创建SpringMVC的配置文件并配置
在resources下创建springmvc.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: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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描组件--> <context:component-scan base-package="edu.hncj.ssm.controller"> </context:component-scan> <!--配置视图解析器--> <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="order" value="1"/> <property name="characterEncoding" value="UTF-8"/> <property name="templateEngine"> <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver"> <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> <!-- 视图前缀 --> <property name="prefix" value="/WEB-INF/templates/"/> <!-- 视图后缀 --> <property name="suffix" value=".html"/> <property name="templateMode" value="HTML5"/> <property name="characterEncoding" value="UTF-8"/> </bean> </property> </bean> </property> </bean> <!-- 配置默认的servlet处理静态资源 --> <mvc:default-servlet-handler/> <!-- 开启MVC的注解驱动 --> <mvc:annotation-driven/> <!-- 配置访问首页的视图控制 --> <mvc:view-controller path="/" view-name="index"></mvc:view-controller> <!--必须通过文件解析器的解析才能将文件转换为MultipartFile对象--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> </bean> </beans>
2.4 创建Mybaits的配置文件并配置
2.4.1 在resources下创建属性文件jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC jdbc.username=root jdbc.password=111111
2.4.2 创建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"> <configuration> <settings> <!--将下划线映射为驼峰--> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--开启延迟加载--> <setting name="lazyLoadingEnabled" value="true"/> <!--按需加载--> <setting name="aggressiveLazyLoading" value="false"/> </settings> <plugins> <!--设置分页插件--> <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> </plugins> </configuration>
依据mybatis部分的内容,会发现这里的配置明显减少,数据库的连接配置也没有,这些配置都会通过spring的配置完成。