美文网首页
Intellij后台开发环境配置和新建项目

Intellij后台开发环境配置和新建项目

作者: 红太羊_8225 | 来源:发表于2021-05-18 14:40 被阅读0次

一准备工作

[if !supportLists](1) [endif]编译器:Intellij IDEA

[if !supportLists](2) [endif]项目自动构建工具:Maven

[if !supportLists](3) [endif]数据库:MySQL

[if !supportLists](4) [endif]开发环境:Windows,安装JDK1.8

[if !supportLists](5) [endif]编辑xml文档工具 Atom

二配置Maven

[if !supportLists](1) [endif]修改Maven的settings.xml文件。

在Maven安装路径下的conf文件夹下,找到settings.xml文件,使用Atom打开编辑。修改的地方主要有两处:

[if !supportLists]1) [endif]修改本地仓库的位置。默认Maven会把下载的jar包等文件放到C盘,将settings.xml文件中的localRepository标签中的路径修改为其它盘符(我放到了安装路径下的rep文件夹中)。如图:

[if !supportLists]1) [endif]修改镜像位置。这里使用的是阿里云的镜像,会提高jar包的下载速度。

修改完毕后保存即可。

[if !supportLists](1) [endif]Intellij中配置Maven

在Intellij程序界面中,点击File->settings(或使用快捷键 Ctrl+Alt+S),搜索框中输入Maven。如图:

点击红色箭头指示的 Override,修改红色框图中的路径为Maven的settings.xml文件的位置,蓝色框图中的本地仓库位置会自动修改为settings.xml文件中localRepository标签中的路径位置。

[if !supportLists](1) [endif]如果希望此配置在Intellij新建其他项目时依然有效,点击File->other settings->default settings,配置如(2)。

三新建Spring Boot项目

[if !supportLists]1) [endif]File->new ->project,如图

SDK默认会选择,Service.URL使用默认的即可,点击next。

[if !supportLists]1) [endif]进入一下界面,编辑项目的一些配置信息:

Group:组名

Artifact:包名

Type:默认为Maven,可根据需要修改。我们这里不需要修改

Language:开发语言,默认为java

Packaging:打包后的格式,可选jar和war两种

Java version:java的版本,默认即可

Version:打包的版本

Name:名称

(以上配置在项目的pom.xml文件中也可以修改)

修改完毕后,点击next。

[if !supportLists]1) [endif]进入下一个页面,可以选择项目的一些依赖。

我们这里只选择Web、Mybatis和MySQL,点击next。

[if !supportLists]1) [endif]进入下一个页面,编辑项目名称和路径。编辑完成点击Finish即可。

[if !supportLists]1) [endif]进入如下页面。项目创建成功。

四运行项目

以我的项目为例,右键点击Demo1Application选择run即可运行。但是如教程所示创建的项目还有几个地方需要修改,才能正常运行。

[if !supportLists]1) [endif]当我们运行项目后,控制台会提示如下信息:

究其原因,无非是我们在创建项目时选择了支持数据库,现在没有数据库连接的配置。这里只需在application.properties文件中添加我们的数据库连接配置就好。以我的为例:

spring.datasource.url=jdbc:mysql://192.168.4.224:3306/***?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8spring.datasource.username=***spring.datasource.password=***spring.datasource.driver-class-name=com.mysql.jdbc.Driver

[if !supportLists]2) [endif]写好数据库连接配置后再次运行,我们会发现之前的提示木有了。谢天谢地!接下来就可以写我们的代码了。

还是以我的项目为例,新建controller文件夹,并新建TestController类。

具体代码如下:

@Controllerpublic class TestController {    @RequestMapping(value = "/test", method = RequestMethod.GET)    @ResponseBody    public String processTest(){        return "ok";    }}

在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求。我们使用@Controller注解来标记某个类为controller。

@RequestMapping注解标记请求的路径,和请求的方法。

@ResponseBody注解是用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

完成上述代码后,就可以启动运行了,访问http://localhost:8080/test,成功。

相关文章

网友评论

      本文标题:Intellij后台开发环境配置和新建项目

      本文链接:https://www.haomeiwen.com/subject/zfzejltx.html