- 打开Idea编辑器,新建项目;
若项目打开,也可点击编辑器左上角,new 新建项目
2.选择maven,勾选create from archetype,选择org.apache.maven.archetypes:maven-archetype-webapp
3.填写groupId和artifactId
groupId是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。
定义了项目属于哪个组,比如,如果你的公司是dxh,有一个项目为reactive,那么groupId就应该是 com.dxh.reactive.
artifactId是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。
定义了当前maven项目在组中唯一的ID,比如reactive
4.选择配置好的maven,若properties中没有archetypeCatalog配置,则新增archetypeCatalog,值为internal,
原因:IDEA根据maven archetype的本质,其实是执行mvn archetype:generate命令,该命令执行时,需要指
定一个archetype-catalog.xml文件。该命令的参数-DarchetypeCatalog,可选值为:remote,internal ,local
等,用来指定archetype-catalog.xml文件从哪里获取。默认为remote,即从
http://repo1.maven.org/maven2/archetype-catalog.xml路径下载archetype-catalog.xml文件。
http://repo1.maven.org/maven2/archetype-catalog.xml 文件约为3-4M,下载速度很慢,导致创建过程卡住。
所以进行此配置
所以,个人觉得,若pom.xml文件中有标签报错的现象,一定检查好文件的标签闭合,是否有代码的欠缺,或
无用代码的增加导致
5.填写项目名称和项目在本地的存放路径,点击Finish
6.至此生成的项目,需要添加文件夹java 和 resources
java文件夹需要转为sources root,作用:通过这个类指定一个文件夹,你告诉IntelliJ IDEA,这个文件夹及其子文件夹中包含的源代码,可以编译为构建过程的一部分。
7.pom.xml文件的配置
此处配置,我遇到两个问题:
一,project标签报错,原因是代码中多写了个\,导致xml文件错误
二,因为pom.xml文件报错,所以依赖包没有下载完全,导致在web.xml文件中配置listener时,listener-class标签下的类路径报错
<?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>reactive</groupId>
<artifactId>reactiveD</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>reactiveD Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<java.version>1.8</java.version>
<springframework.version>4.2.5.RELEASE</springframework.version>
<jackson.version>2.9.1</jackson.version>
<aspectj.version>1.7.4</aspectj.version>
<slf4j.version>1.7.7</slf4j.version>
<logback.version>1.1.2</logback.version>
<commons-lang3.version>3.2.1</commons-lang3.version>
<jdbc.driver.groupId>mysql</jdbc.driver.groupId>
<jdbc.driver.artifactId>mysql-connector-java</jdbc.driver.artifactId>
<jdbc.driver.version>5.1.22</jdbc.driver.version>
</properties>
<!--依赖项定义-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring start-->
<!--spring 依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<!--spring 上下文-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<!--spring MVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<!--spring MVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<!--spring jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframework.version}</version>
</dependency>
<!--spring end-->
<!--数据库配置-->
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--spring + mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<!--jackson-->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
<!--slf4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<!--代码直接调用log4j会被桥接到slf4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--代码直接调用commons-logging会被桥接到slf4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
<build>
<finalName>reactiveD</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
8.web.xml文件的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!--spring监听器,用于监听spring-bean上下文加载-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--spring beans 配置文件所在目录-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--spring mvc 配置-->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-mvc.xml</param-value>
</init-param>
</servlet>
<!--/ 只拦截请求, 如/index /add, 不拦截页面 静态文件 如/index.jsp /style.css /*全拦截-->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--指定编码-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encodingFilter</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--druid 监控-->
</web-app>
9.web.xml中用到的applicationContext.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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<description>spring 公共配置文件</description>
<!--外部properties文件-->
<bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--忽略不能处理的占位-->
<property name="ignoreResourceNotFound" value="true"></property>
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
<!--使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入-->
<context:component-scan base-package="com.dxh.active">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
<!--数据源的配置-->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${db.driverClassName}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="initialSize" value="3"></property>
<property name="minIdle" value="3"></property>
<property name="maxActive" value="30"></property>
<property name="maxWait" value="6000"></property>
<property name="filters" value="stat,wall"></property>
</bean>
<!--配置sessionfactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--指定数据源-->
<property name="dataSource" ref="datasource"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--指定dao位置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dxh.active.dao.mysql.mappers"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!--事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
<!--事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--druid监控 慢SQL记录-->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<!-- 慢sql时间设置,即执行时间大于3000毫秒的都是慢sql -->
<property name="slowSqlMillis" value="3000"></property>
<property name="logSlowSql" value="true"></property>
<property name="mergeSql" value="true"></property>
</bean>
<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
<property name="dbType" value="mysql"></property>
</bean>
</beans>
10.web.xml中用到的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: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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--开启上下文注解-->
<context:annotation-config></context:annotation-config>
<!--启动注解驱动的springmvc 功能-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--启动包扫描功能-->
<context:component-scan base-package="com.dxh.active.web.rest"></context:component-scan>
<!--json格式数据的请求处理方式-->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"></bean>
<bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
11.applicationContext.xml中需要的application.properties配置
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc\:mysql\://192.168.12.190\:3306/数据库名(spring_test)?useUnicode\=true&characterEncoding\=utf-8&useSSL=false
db.username=root
db.password=root
12.applicationContext.xml中需要的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="useGeneratedKeys" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="true"></setting>
</settings>
<plugins>
<!--com.github.pagehelper未PageHelper类所在包名-->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>
13.按照路径编写controller层代码
@RestController
public class SpringController01 {
private Logger logger = LoggerFactory.getLogger(SpringController01.class);
@RequestMapping(value = "springtest/1", method = RequestMethod.GET)
public String getIndex(){
return "springtest 1";
}
}
14.代码整理完,配置tomcat,启动项目即可
15.浏览器展示结果: