环境

操作系统: Windows 10

IDE: IntelliJ IDEA 2017.2.4

构建工具: Maven

搭建环境

一、创建项目

  1. 新建项目:File -> New -> Project...

在弹窗左侧菜单选择Maven,勾选Maven模板(即勾上Create from archetype前的复选框),选择maven-archrtype-webapp

新建项目

  1. 输入项目信息:GroupIdArtifactId

编辑项目信息

  1. 在弹窗中选择自己本地的Maven仓库路径,也可使用默认仓库路径,在Properties中添加参数:archetypeCatalog=internal,不然创建项目结构的过程将很慢;

原因

当我们使用Maven原型生成项目骨架时会执行mvn archetype:generate命令,而该命令执行时需要指定一个archetype-catelog.xml文件,该命令的参数-DarchetypeCatelog,参数可选值为:remoteinternallocal等,用于指定archetype-catelog.xml文件读取位置;默认是remote,即从http://repo1.maven.org/maven2/archetype-catalog.xml下载,而文件约为3~4M,下载速度很慢,导致创建过程卡住。

编辑属性

  1. 输入项目名称,单击Finish
    编辑项目名称

生成Maven项目骨架如图:

项目骨架

  1. src/main目录下创建java目录,并标记目录为Sources Root

二、配置项目

  1. 使用maven引入项目所需的jar包,
    相关jar可以使用http://mvnrepository.com/搜索;

pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lwh</groupId>
    <artifactId>SSM-Demo</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SSM-Demo Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>

        <!-- spring 版本号 -->
        <spring.version>4.3.10.RELEASE</spring.version>

        <!-- Mybatis 版本号 -->
        <mybatis.version>3.4.4</mybatis.version>

        <!-- Mybatis/spring 整合包版本号 -->
        <mybatis-spring.version>1.3.0</mybatis-spring.version>

        <!-- mysql 版本号 -->
        <mysql-driver.version>5.1.38</mysql-driver.version>

        <!-- jstl 版本号 -->
        <jstl.version>1.2</jstl.version>

        <!-- fastjson 版本号 -->
        <fastjson.version>1.2.32</fastjson.version>

        <!-- log4j 版本号 -->
        <log4j.version>1.2.17</log4j.version>

        <!-- slf4j 版本号 -->
        <slf4j.version>1.7.25</slf4j.version>

    </properties>

    <dependencies>

        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</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-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</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-aop</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>${mybatis.version}</version>
        </dependency>

        <!-- mybatis/spring 整合包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis-spring.version}</version>
        </dependency>

        <!-- mysql driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-driver.version}</version>
        </dependency>

        <!-- jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!-- 日志 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>

        <!-- jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.0</version>
        </dependency>

        <!-- 上传组件包 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>SSM-Demo</finalName>
    </build>
</project>
  1. src/main/resources目录下新建jdbc.properties文件,用于配置数据库连接;

jdbc.properties

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/ssmdemo?useUnicode=true&characterEncoding=utf8
jdbc.username = root
jdbc.password = root
  1. src/main/resources目录下新建spring-mvc.xml文件,用于配置springMVC:

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: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">

    <!-- 自动扫描controller包下的所有类 -->
    <context:component-scan base-package="com.ssmdemo.controller" />

    <!-- 默认视图映射配置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 配置上传拦截 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760" /> <!-- 最大10M -->
    </bean>

</beans>
  1. src/main/resources目录下新建spring-mybatis.xml文件,用于整合spring和mybatis:

    <?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/tool"
        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/tool http://www.springframework.org/schema/tool/spring-tool.xsd">
    
     <!-- 加载配置参数文件 -->
     <context:property-placeholder location="classpath*:jdbc.properties" />
    
     <!-- 配置数据库连接池 -->
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="driverClassName" value="${jdbc.driver}" />
         <property name="url" value="${jdbc.url}" />
         <property name="username" value="${jdbc.username}" />
         <property name="password" value="${jdbc.password}" />
     </bean>
    
     <!-- 配置sqlSessionFactory -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
         <!-- 注入数据库连接池 -->
         <property name="dataSource" ref="dataSource" />
         <!-- 配置mybatis全局配置文件 -->
         <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
         <!-- 扫描sql配置文件, mapper需要的xml文件 -->
         <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml" />
     </bean>
    
     <!-- 配置扫描dao包, 注入到spring容器中 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <!-- 注入sqlSessionFactory -->
         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
         <!-- 映射接口文件的包路径 -->
         <property name="basePackage" value="com.ssmdemo.dao" />
     </bean>
    
```
  1. src/main/resources目录下新建文件夹mybatis,在mybatis目录下新建mybatis-config.xml文件,用于配置mybatis:

    <?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="cacheEnabled" value="true"/>
     </settings>
    
     <!-- 配置别名 -->
     <typeAliases>
         <typeAlias type="com.ssmdemo.entity.User" alias="User" />
     </typeAliases>
    
```
  1. 配置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_2_5.xsd" id="WebApp_ID" version="2.5">
    
    <display-name>Archetype Created Web Application</display-name>
    
    <!-- 加载配置文件 -->
    <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath*:spring-mybatis.xml</param-value>
    </context-param>
    
    <!-- 配置spring上下文监听器 -->
    <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 编码格式为UTF-8 -->
    <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>
    
    <!-- 配置springMVC控制器DispatcherServlet -->
    <servlet>
     <servlet-name>DispatcherServlet</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>DispatcherServlet</servlet-name>
     <!-- 指定请求的后缀,可以随意写后缀 -->
     <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <session-config>
     <session-timeout>20</session-timeout>
    </session-config>
    
```
  1. 新建测试实体User.java
    package com.ssmdemo.entity;
    
    

public class User {

private Integer id;
private String loginName;
private String password;
private String userName;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getLoginName() {
    return loginName;
}

public void setLoginName(String loginName) {
    this.loginName = loginName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

}


8. 创建Dao接口`IUserDao.java`:
```java
package com.ssmdemo.dao;

import com.ssmdemo.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface IUserDao {

    /**
     * 查询用户列表
     * @return
     */
    List<User> listUsers();
}
  1. 实现Dao接口:
    src/main/resources目录下新建文件夹mapper,用于保存mapper需要的xml文件,新建UserMapper.xml文件,用于实现IUserDao

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.ssmdemo.dao.IUserDao">
    
     <!-- 查询用户列表 -->
     <select id="listUsers" resultType="User">
         SELECT ID,GUIDEX,LoginName,UserName,RegisterTime FROM User ORDER BY ID DESC
     </select>
    
```
  1. 创建Service接口IUserService.java:
    package com.ssmdemo.service;
    

import com.ssmdemo.entity.User;

import java.util.List;

public interface IUserService {

List<User> listUsers();

}


11. 实现Service接口`UserServiceImpl.java`:
```java
package com.ssmdemo.service.impl;

import com.ssmdemo.dao.IUserDao;
import com.ssmdemo.entity.User;
import com.ssmdemo.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private IUserDao userDao;

    public List<User> listUsers() {
        return this.userDao.listUsers();
    }


}
  1. 配置ControllerUserController.java:
    package com.ssmdemo.controller;
    

import com.alibaba.fastjson.JSONObject;
import com.ssmdemo.entity.User;
import com.ssmdemo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {

@Autowired
private IUserService userService;

@RequestMapping(value = "/users")
public String queryUsers(Model model) {
    List<User> users = this.userService.listUsers();
    model.addAttribute("users", users);
    return "user";
}

}


13. 创建jsp页面,在`WEB-INF`下创建文件夹`jsp`,并在`jsp`目录下创建文件`user.jsp`:
```jsp
<%--
  Created by IntelliJ IDEA.
  User: 986310747@qq.com
  Date: 2017/10/9
  Time: 17:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <style>
        h2 {
            text-align: center;
        }
        .user-wrapper {
            width: 960px;
            margin: 0 auto;
            padding: 10px;
        }
    </style>
</head>
<body>
<h2>用户列表</h2>
<div class="user-wrapper">
    <table>
        <thead>
            <tr>
                <th>序号</th>
                <th>登录名</th>
                <th>用户名</th>
            </tr>
        </thead>
        <tbody>
            <!-- 使用jstl标签循环输出 -->
            <c:forEach var="user" items="${users}">
                <tr>
                    <td>${user.id}</td>
                    <td>${user.loginName}</td>
                    <td>${user.userName}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
</div>
</body>
</html>

三、项目部署

  1. 新建Tomcat容器,在工具栏中单击运行按钮;

编辑配置

  1. 添加Tomcat;

添加Tomcat

  1. 配置Tomcat;

配置Tomcat

四、运行

成功运行如图:

成功运行

参考

解决IntelliJ IDEA 创建Maven项目速度慢问题 DarchetypeCatalog

文章目录