java caeser_IDEA配置SSM(Spring-Spring MVC-MyBatis)环境(maven)教程-实现一个简单的API

@[toc]

前言

本篇从安装到配置到实现一个简单的 API 全过程,意在教大家如何使用 IDEA 如何配置 SSM 框架,如何使用 maven 仓库,也是我走了一些些弯路才得以成功的,所以希望记录我步骤来帮助大家学 Java 学 Spring,一起来看看吧!

1、安装 idea

准备的软件有:

AAffA0nNPuCLAAAAAElFTkSuQmCC

1.1 我们安装一下 idea

AAffA0nNPuCLAAAAAElFTkSuQmCC

1.2 安装位置默认,然后选择64位

AAffA0nNPuCLAAAAAElFTkSuQmCC

1.3 安装IDEA还是挺简单,因为无论你怎么安装都能安装成功,就一路下一步就行。

2、安装 tomcat

AAffA0nNPuCLAAAAAElFTkSuQmCC

一路下一步就可以

我们记录一下安装的位置:C:\Program Files\Apache Software Foundation\Tomcat 8.5

3、配置 IDEA

3.1 选插件

AAffA0nNPuCLAAAAAElFTkSuQmCC

3.2 如果实在不懂就全选就好,如果只开发 JAVA 开发 Spring 使用 Maven,就勾第一排横着的三个就行。

我们先用免费的30天就好

AAffA0nNPuCLAAAAAElFTkSuQmCC

4、开始创建工程

4.1 选择创建新的工程

AAffA0nNPuCLAAAAAElFTkSuQmCC

4.2 默认选了 SDK 就不用管,选择 maven 管理,选择

org.apache.maven.archetypes:maven-archetype-webapp

然后点击 next 即可

AAffA0nNPuCLAAAAAElFTkSuQmCC

4.3 新建一个文件夹并起名 apidemo

AAffA0nNPuCLAAAAAElFTkSuQmCC

4.4 添加 GroupId 和 ArtifactId

AAffA0nNPuCLAAAAAElFTkSuQmCC

4.5 解压 maven 仓库记住解压位置,然后点击 override 选择文件找到 maven 的 setting.xml 文件

AAffA0nNPuCLAAAAAElFTkSuQmCC

点击 next

AAffA0nNPuCLAAAAAElFTkSuQmCC

选择创建工程的文件路径,点击 Finish

AAffA0nNPuCLAAAAAElFTkSuQmCC

点击 Enable Auto-Import

AAffA0nNPuCLAAAAAElFTkSuQmCC

等待下载包,这个取决于你的网速,反正我是1、2分钟就能下载完了。下载完之后注意左下角有个 BUILD SUCCESS,而且我们还可以看到 src 文件下已经有基本骨架了

AAffA0nNPuCLAAAAAElFTkSuQmCC

5、配置工程

5.1 新建几个文件夹,先在 main 目录下新建 java 、resources文件夹,在 webapp 里的 WEB-INF 下新建 html 文件夹

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC

5.2 resources 文件夹下新建 mapper 文件夹和 spring 文件夹,还是 resources 文件夹新建 jdbc.properties 文件和 mybatis-config.xml

AAffA0nNPuCLAAAAAElFTkSuQmCC

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/upmovie_playsys?userUnicode=true&characterEncoding=utf8

jdbc.username=root

jdbc.password=123456

mybatis-config.xml

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

5.3 spring 文件夹下新建 spring-dao.xml、spring-service.xml、spring-web.xml 三个文件

[外链图片转存失败(img-RHYi5cgd-1568174056880)(./IDEA18.png )]

spring-web.xml

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-3.2.xsd">

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

5.4 新建包 com 然后在 com下新建 caeser 再新建 apidemo

在该包下,新建 dao、entity、web、service 四个文件夹

AAffA0nNPuCLAAAAAElFTkSuQmCC

修改一下项目属性

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC

web包下面新建 MovieController.java

AAffA0nNPuCLAAAAAElFTkSuQmCC

MovieController.java

package com.caeser.apidemo.web;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

@RequestMapping("/mv")

public class MovieController {

@RequestMapping(value = "/get", method = RequestMethod.GET)

@ResponseBody

private Map getMV( HttpServletRequest request){

Map modelMap = new HashMap();

modelMap.put("jobDepartmentList", "123");

return modelMap;

}

}

6、修改 web.xml

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1" metadata-complete="true">

Archetype Created Web Application

index.jsp

Kaptcha

com.google.code.kaptcha.servlet.KaptchaServlet

kaptcha.border

no

kaptcha.textproducer.font.color

red

kaptcha.image.width

135

kaptcha.textproducer.char.string

ACDEFHKPRSTWX345679

kaptcha.image.height

50

kaptcha.textproducer.font.size

43

kaptcha.noise.color

black

kaptcha.textproducer.char.length

4

kaptcha.textproducer.font.names

Arial

Kaptcha

/Kaptcha

spring-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/spring-*.xml

spring-dispatcher

/

7、修改 pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.caeser

apidemo

1.0-SNAPSHOT

war

apidemo Maven Webapp

http://www.example.com

UTF-8

1.7

1.7

4.3.7.RELEASE

net.sf.jasperreports

jasperreports

6.7.0

junit

junit

4.12

test

ch.qos.logback

logback-classic

1.2.3

ch.qos.logback

logback-classic

1.2.3

org.springframework

spring-core

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-test

${spring.version}

test

javax.servlet

javax.servlet-api

3.1.0

com.fasterxml.jackson.core

jackson-databind

2.8.7

commons-collections

commons-collections

3.2

org.mybatis

mybatis

3.4.2

org.mybatis

mybatis-spring

1.3.1

mysql

mysql-connector-java

5.1.37

c3p0

c3p0

0.9.1.2

net.coobird

thumbnailator

0.4.8

com.github.penggle

kaptcha

2.3.2

commons-fileupload

commons-fileupload

1.3.2

apidemo

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-war-plugin

3.2.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

org.apache.maven.plugins

maven-compiler-plugin

3.6.1

1.8

1.8

UTF8

8、等待下载 jar 包

看你网速如何了,下载 jar 的过程其实蛮长的

9、配置 server

9.1 给 IDEA 添加 tomcat

AAffA0nNPuCLAAAAAElFTkSuQmCC

9.2 搜索 tom 勾选插件

AAffA0nNPuCLAAAAAElFTkSuQmCC

9.3 然后重启 IDEA

然后再配置 tomcat ,File- Setting-Build,Execution,Deployment-Application Servers

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC

一路OK即可

9.4 执行一次 mvn clean 和 mvn install

AAffA0nNPuCLAAAAAElFTkSuQmCC

9.5 点击 Add Configuration,添加一个本地的 tomcat

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC

AAffA0nNPuCLAAAAAElFTkSuQmCC

10、启动tomcat

AAffA0nNPuCLAAAAAElFTkSuQmCC

你可能感兴趣的:(java,caeser)