目录

IDEA 配置 Maven 创建 Spring Boot 项目

# IDEA 配置 Maven 创建 Spring Boot 项目

# 一.配置Maven

维基百科上的介绍:Apache Maven,是一个软件(特别是Java软件)项目管理及自动构建工具,由Apache软件基金会所提供。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤。

# 1.Maven下载及配置系统变量

image-20210818091844957.png

Maven (opens new window)官网下载对应的版本,下载后解压,将 bin 目录配置到环境变量中:

新建一个变量M2_Home,变量值为你的安装目录,如D:\Program Files\apache-maven-3.6.3

path中添加一条变量%M2_Home%\bin;

在cmd中输入mvn -v后出现Apache Maven 3.6.3版本相关信息即可

# 2.Maven 配置

配置仓库位置:打开 Maven 根目录下的 conf/setting.xml

搜索localRepository,按格式修改为你的本地仓库目录即可

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>D:\maven_repository</localRepository>
1
2
3
4
5
6
7

**配置阿里云下载:**搜索 <mirrors>,在 <mirrors> 下新加一个阿里云的镜像地址:

<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>
  </mirrors>
1
2
3
4
5
6
7

# 3.IDEA 配置 Maven

image-20210818091640580.png

# 二.IDEA 创建 springboot 项目

Spring Boot是基于Spring Framework 4.0派生的,用于快速搭建独立的基于生产级别的Spring应用的框架,可以以最小的依赖引入来构建一个Spring应用。并且它还具有以下特点:

  • 拥有嵌入式的Tomcat, Jetty, Undertow或者Reactor Netty(无需部署war文件)
  • 尽可能地自动配置(@AutoConfiguration)Spring和第三方库
  • 提供用于生产的功能,例如指标、运行状态检查和外部化配置
  • 无需麻烦而冗余的XML配置,一切都可以使用Java配置

# 1.新建一个项目

在idea中使用Spring Initializr (opens new window)来构建项目

Spring initializr 是Spring 官方提供的一个很好的工具,可以用来用来创建一个Spring boot 的项目。 可以选择使用Maven管理或者使用Gradle管理,还可以选择使用的编程语言,提供了Java,Kotlin,Groovy三种编程语言,还可以根据需要选择Spring Boot的版本。

image-20210818092528966.png

image-20210818092743387.png

image-20210818092815368.png

image-20210818092837775.png

image-20210818094250380.png

# 2.目录文件及配置

  • **src/main/java:**主程序入口 TinyContractApplication,可以通过直接运行该类来 启动 Spring Boot应用
  • **src/main/resources:**配置目录,该目录用来存放应用的一些配置信息,比如应用名、服务端口、数据库配置等。由于我们应用了Web模块,因此产生了 static目录与templates目录,前者用于存放静态资源,如图片、CSS、JavaScript等;后者用于存放Web页面的模板文件。
  • **src/test:**单元测试目录,生成的 TinyContractApplicationTests 通过 JUnit4实现,可以直接用运行 Spring Boot应用的测试。

application.properties 用来保存数据库链接信息等应用程序数据

pom.xml pom文件为maven工程的主要项目构建文件,以及相关配置文件,此文件中的重点为dependencies节点配置的各种starter。

至此项目初始化完成

# 三.踩坑及问题记录

1.application.properties配置文件

#配置mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test_db
spring.datasource.username=root
#密码
spring.datasource.password=123456
#端口号
server.port=8090
#指明mapper的映射文件位置
mybatis.mapper-locations=classpath:/mapper/**/*.xml
1
2
3
4
5
6
7
8
9
10

2.启动后报错:Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.`

我的mysql版本为:Server version: 5.7.30-log MySQL Community Server (GPL),高版本需配置为spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

3.启动后报错:org.apache.ibatis.binding.BindingException: Parameter 'pageSize' not found. Available parameters are [offset, pagesize, param1, param2]

mapper文件中多参数未指定@Param,修改为List<Member> pageList(@Param("offset") int offset, @Param("pagesize") int pagesize);

4.SpringBoot + Mybatis 在控制台输出sql语句

在application.properties配置中添加:logging.level.Mapper类的包=debug

logging.level.com.example.demo.dao.MemberMapper=debug

5.SpringBoot跨域请求处理方式

直接采用SpringBoot的注解@CrossOrigin(也支持SpringMVC)

简单粗暴的方式,Controller层在需要跨域的类或者方法上加上该注解即可

@CrossOrigin(value = "*",maxAge =500)
1
上次更新: 2024/03/20, 23:57:47
最近更新
01
使用 acme.sh 自动化SSL证书管理
03-25
02
COSCLI 的使用记录
03-25
03
腾讯云命令行工具 TCCLI
03-25
更多文章>