01 使用idea创建项目demo
先放三张截图,按照图示创建demo。
创建完成后,项目结构如图:
注意:项目中引入的mysql驱动依赖,默认版本是8.0.29,由于我们使用的mysql是5.6.26,后面要修改调整一下。
看一下pom文件:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
02 跑个Hello world
为了能正常启动,首先要调整mysql-connector的版本,并配置好数据库连接参数。
调整依赖版本: <version>5.1.49</version>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
<scope>runtime</scope>
</dependency>
更新依赖。
然后,在resource目录下新建配置文件application.yml:
spring:
datasource:
url: jdbc:mysql://localhost:3306/ds1
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
用到的mysql库ds1,表sys_dept
DROP TABLE IF EXISTS `sys_dept`;
CREATE TABLE `sys_dept` (
`dept_id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '部门id',
`parent_id` BIGINT(20) DEFAULT '0' COMMENT '父部门id',
`ancestors` VARCHAR(50) DEFAULT '' COMMENT '祖级列表',
`dept_name` VARCHAR(30) DEFAULT '' COMMENT '部门名称',
`order_num` INT(4) DEFAULT '0' COMMENT '显示顺序',
`leader` VARCHAR(20) DEFAULT NULL COMMENT '负责人',
`phone` VARCHAR(11) DEFAULT NULL COMMENT '联系电话',
`email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
`status` CHAR(1) DEFAULT '0' COMMENT '部门状态(0正常 1停用)',
`del_flag` CHAR(1) DEFAULT '0' COMMENT '删除标志(0代表存在 2代表删除)',
`create_by` VARCHAR(64) DEFAULT '' COMMENT '创建者',
`create_time` DATETIME DEFAULT NULL COMMENT '创建时间',
`update_by` VARCHAR(64) DEFAULT '' COMMENT '更新者',
`update_time` DATETIME DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`dept_id`)
) ENGINE=INNODB AUTO_INCREMENT=110 DEFAULT CHARSET=utf8 COMMENT='部门表';
/*Data for the table `sys_dept` */
INSERT INTO `sys_dept`(`dept_id`,`parent_id`,`ancestors`,`dept_name`,`order_num`,`leader`,`phone`,`email`,`status`,`del_flag`,`create_by`,`create_time`,`update_by`,`update_time`) VALUES (100,0,'0','若依科技',0,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:56','',NULL),(101,100,'0,100','深圳总公司',1,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:56','',NULL),(102,100,'0,100','长沙分公司',2,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:56','',NULL),(103,101,'0,100,101','研发部门',1,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:56','',NULL),(104,101,'0,100,101','市场部门',2,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:56','',NULL),(105,101,'0,100,101','测试部门',3,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:56','',NULL),(106,101,'0,100,101','财务部门',4,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:56','',NULL),(107,101,'0,100,101','运维部门',5,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:56','',NULL),(108,102,'0,100,102','市场部门',1,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:57','',NULL),(109,102,'0,100,102','财务部门',2,'若依','15888888888','ry@qq.com','0','0','admin','2022-04-06 10:35:57','',NULL);
配置完这一步,已经能够正常启动。我们在创建个controller文件,运行helloworld。
TestController.java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value="/Hello",method = RequestMethod.GET)
public String Hello(String pra){
return "Hello world !"+pra;
}
}
启动项目:
出现上图的信息,说明已经启动成功。该项目启动使用的端口是8080。
下面进行测试:
打开谷歌浏览器,地址栏中输入:http://localhost:8080/Hello?pra=springboot,接口请求响应:
## 03 最后
Hello world 已经搞出来了,下一篇我们整合mybaits操作数据库。
环境说明: