IDEA搭建一个SpringBoot项目——十分详细(web+mysql)
前排提示:
IDEA版本:IntelliJ IDEA 2021.1.1 专业版(是否为专业版影响不大)
搭建目的:前端web页面能够获取到MySQL数据库中的数据
详细步骤:
1. 创建一个新项目
File -> New -> Project...

2. 项目环境配置
左侧选择Spring Initializr,右侧对项目一些属性进行配置。其中,包名Name为"newDemo",Project SDK默认1.8版本,java选择8(我的jdk是1.8),点击next;

3. 添加依赖项
添加依赖项页面如下:

在添加依赖项环节中,我们添加Web下的Spring Web,SQL下的MyBatis Framework和MySQL Driver;

右侧可以看见已经选择的依赖;

点击Finish完成项目创建;
4. 创建好的项目结构

5. application.yml配置
将resources文件夹下的application.properties文件后缀名改为yml;

双击进入配置页面,进行端口、数据库和Mybatis的配置,我的配置结果如下;

其中,需要注意的几个地方如下:
端口号设置为默认值:8080;
mysql数据库:edusystem,username:root,password:111111;
mybatis下的type-aliases-package:com.example.newDemo.bean(bean目录马上创建)
mysql数据库需要替换成自己的数据库、用户名和密码;

该部分代码如下(注意缩进):
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/edusystem?serverTimezone=Asia/Shanghai&characterEncoding=utf-8
username: root
password: 111111
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.example.newDemo.bean

6. web页面测试
在resources的static目录下创建index.html,内容如下;

点击右上角运行按钮;

可见运行成功;

打开浏览器,在地址栏输入localhost:8080,回车;

可见index.html的内容成功显示在页面上。至此,web环境搭建成功,接下来搭建mysql环境。
7. 创建bean、controller、mapper、service四层目录
在newdemo目录下创建bean、controller、mapper、service四层目录,目录结构如下所示;

8. 完善bean层
我的edusystem数据库下的departments表数据如下:

在bean层下建立Depart类,内容如下(属性要和数据表的列一一对应):
package com.example.newdemo.bean;
public class Depart {
private String id;
private String depName;
private Integer grades;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDepName() {
return depName;
}
public void setDepName(String depName) {
this.depName = depName;
}
public Integer getGrades() {
return grades;
}
public void setGrades(Integer grades) {
this.grades = grades;
}
}

9. 完善mapper层
在mapper层下建立DepartMapper接口,内容如下:
package com.example.newdemo.mapper;
import com.example.newdemo.bean.Depart;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface DepartMapper {
@Select({
"select",
"id, depName, grades",
"from departments"
})
List<Depart> selectAll();
}

10. 完善service层
在service层下建立DepartService接口和DepartServiceImpl实现类,内容分别如下;
DepartService接口:
package com.example.newdemo.service;
import com.example.newdemo.bean.Depart;
import java.util.List;
public interface DepartService {
public List<Depart> selectAll();
}

DepartServiceImpl实现类:
package com.example.newdemo.service;
import com.example.newdemo.bean.Depart;
import com.example.newdemo.mapper.DepartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("departService")
public class DepartServiceImpl implements DepartService{
@Autowired
private DepartMapper departMapper;
@Override
public List<Depart> selectAll() {
return departMapper.selectAll();
}
}

11. 完善controller层
在controller层下建立DepartController类,内容如下:
package com.example.newdemo.controller;
import com.example.newdemo.bean.Depart;
import com.example.newdemo.service.DepartService;
import com.example.newdemo.service.DepartServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/depart")
public class DepartController {
@Resource
private DepartService departService = new DepartServiceImpl();
@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
public List<Depart> selectAll() {
List<Depart> list = departService.selectAll();
return list;
}
}

12. 完善后的项目结构

13. 完善index.html以进行测试
对index.html的内容进行完善,用来获取数据表departments中的数据,内容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.result{
position: fixed;
width: 100%;
bottom: 0;
left: 0;
height: 300px;
background-color: rgba(0,0,0,.8);
color: white;
text-align: center;
letter-spacing: 2px;
padding-top: 20px;
font-size: 18px;
line-height: 28px;
overflow: scroll;
}
</style>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<button style="display: block;margin: 20px auto;width: 160px;height: 60px;" onclick="getAll()">查看院系信息</button>
<div class="result" id="result"></div>
<script>
function getAll(){
$.ajax({
type: "get",
url: "depart/selectAll",
data: {
},
success:function (data) {
console.log(data)
$("#result").empty()
for(var i = 0;i < data.length;i++){
$("#result").append(JSON.stringify(data[i]) + "<br>")
}
},
});
}
</script>
</body>
</html>

14. 点击右上角的运行

15. 打开浏览器,输入localhost:8080
可以看见有一个“查看院系信息”按钮:

点击该按钮,在页面下方得到如下结果:

可见,departments表中的数据成功显示在index页面中,至此,web+mysql环境搭建完毕。
IDEA搭建一个SpringBoot项目——十分详细(web+mysql)的更多相关文章
- SpringBoot(一):使用IDEA快速搭建一个SpringBoot项目(详细)
环境: JDK1.8 Maven:3.5.4 1.打开IDEA,右上角选择File→New→Project 选择Spring Initializr(使用IDEA自带的插件创建需要电脑联网) 2.点 ...
- 搭建一个SpringBoot项目
1.创建项目 New->Spring Starter Project 2.添加支持 增加对mybatis plus的支持,修改pom.xml,增加如下内容: <dependency> ...
- 使用IDEA 搭建一个 SpringBoot + Hibernate + Gradle 项目
现在创建个项目: 勾上 自已 需要东西.(这里作为演示) maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} 关闭项目 ...
- 使用idea创建第一个springboot项目
版权声明:版权归作者所有,转载请注明出处. https://blog.csdn.net/qq_34205356/article/details/81098354 前言:如今springboot越来越火 ...
- 从零开始的SpringBoot项目 ( 二 ) 使用IDEA创建一个SpringBoot项目
工欲善其事 , 必先利其器 . IntelliJ IDEA 2019.3.3 x64的安装与破解 下面详细说明下如何使用idea创建我们的第一个springboot项目: 首先打开idea主界面选择 ...
- springboot:快速构建一个springboot项目
前言: springboot作为springcloud的基础,springboot的热度一直很高,所以就有了这个springboot系列,花些时间来了解和学习为自己做技术储备,以备不时之需[手动滑稽] ...
- spting Boot 创建一个springBoot项目
spting Boot 创建一个springBoot项目 1)学习springBoot使用软件:IDEA软件(前面的文章有安装idea的过程). 也可以使用另一种方法在https://start.sp ...
- 【spring Boot】1.创建第一个springBoot项目
入手springBoot,搭建第一个springBoot项目. 看官方文档还是有点别扭. https://docs.spring.io/spring-boot/docs/current-SNAPSHO ...
- 使用IDEA创建一个springboot项目
工欲善其事,必先利其器. 不难发现,还是有很多小朋友在使用eclipse开发java项目.当你接触IDEA后,一切都变得美好了. 使用IDEA创建一个springboot项目是一件极其简单的事情.界面 ...
随机推荐
- 最全的cURL命令使用
cURL是什么 curl是Linux命令行工具,可以使用任何可支持的协议(如HTTP.FTP.IMAP.POP3.SCP.SFTP.SMTP.TFTP.TELNET.LDAP或FILE)在服务器之间传 ...
- awk sed grep 常用命令
文本间隔 在每一行后面增加一空行: sed G sed G v1 >v2 在每一行后面增加两行空行: sed 'G;G' 将第一个脚本所产生的所有空行删除(即删除所有偶数行): sed 'n;d ...
- CentOS 7.3 安装指南 作者: Matei Cezar 译者: LCTT geekpi
CentOS 7.3 安装指南 作者: Matei Cezar 译者: LCTT geekpi | 2016-12-20 09:12 评论: 11 收藏: 4 分享: 1 基于 Red Hat 企 ...
- 8.2-3 partprobe、tune2fs
8.2 partprobe:更新内核的硬盘分区表信息 partprobe命令用于在硬盘分区发生改变时,更新Linux内核中的硬盘分区表数据.有时在使用fdisk.part命令对硬盘进行分区 ...
- Centos6.5 修改主机名(hostname)
centos6需要修改两处:一处是/etc/sysconfig/network,另一处是/etc/hosts,只修改任一处会导致系统启动异常.首先切换到root用户. /etc/sysconfig/n ...
- python 中的nonlocal
python 中nonloal 关键字用来在函数或其他作用域中使用外层变量(非全局),也可使用global需要在函数外部
- 基于 BDD 理论的 Nebula 集成测试框架重构(上篇)
本文首发于 Nebula Graph 公众号 NebulaGraphCommunity,Follow 看大厂图数据库技术实践. 测试框架的演进 截止目前为止,在 Nebula Graph 的开发过程中 ...
- [LeetCode] 231. 2 的幂
位运算 231. 2 的幂 ``` class Solution { public boolean isPowerOfTwo(int n) { int cnt = 0; while (n>0) ...
- 机器学习PAI
机器学习PAI 机器学习PAI(Platform of Artificial Intelligence)是阿里云人工智能平台,提供一站式的机器学习解决方案.本文介绍什么是机器学习PAI. 什么是机器学 ...
- 孟老板 BaseAdapter封装(四) PageHelper
BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...