前排提示

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)的更多相关文章

  1. SpringBoot(一):使用IDEA快速搭建一个SpringBoot项目(详细)

    环境: JDK1.8   Maven:3.5.4 1.打开IDEA,右上角选择File→New→Project 选择Spring Initializr(使用IDEA自带的插件创建需要电脑联网) 2.点 ...

  2. 搭建一个SpringBoot项目

    1.创建项目 New->Spring Starter Project 2.添加支持 增加对mybatis plus的支持,修改pom.xml,增加如下内容: <dependency> ...

  3. 使用IDEA 搭建一个 SpringBoot + Hibernate + Gradle 项目

    现在创建个项目: 勾上 自已 需要东西.(这里作为演示) maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} 关闭项目 ...

  4. 使用idea创建第一个springboot项目

    版权声明:版权归作者所有,转载请注明出处. https://blog.csdn.net/qq_34205356/article/details/81098354 前言:如今springboot越来越火 ...

  5. 从零开始的SpringBoot项目 ( 二 ) 使用IDEA创建一个SpringBoot项目

    工欲善其事 , 必先利其器 . IntelliJ IDEA 2019.3.3 x64的安装与破解 下面详细说明下如何使用idea创建我们的第一个springboot项目: 首先打开idea主界面选择 ...

  6. springboot:快速构建一个springboot项目

    前言: springboot作为springcloud的基础,springboot的热度一直很高,所以就有了这个springboot系列,花些时间来了解和学习为自己做技术储备,以备不时之需[手动滑稽] ...

  7. spting Boot 创建一个springBoot项目

    spting Boot 创建一个springBoot项目 1)学习springBoot使用软件:IDEA软件(前面的文章有安装idea的过程). 也可以使用另一种方法在https://start.sp ...

  8. 【spring Boot】1.创建第一个springBoot项目

    入手springBoot,搭建第一个springBoot项目. 看官方文档还是有点别扭. https://docs.spring.io/spring-boot/docs/current-SNAPSHO ...

  9. 使用IDEA创建一个springboot项目

    工欲善其事,必先利其器. 不难发现,还是有很多小朋友在使用eclipse开发java项目.当你接触IDEA后,一切都变得美好了. 使用IDEA创建一个springboot项目是一件极其简单的事情.界面 ...

随机推荐

  1. Spring Cloud Alibaba Nacos Discovery 实战

    Nacos 作为服务注册中心,可以快速简单的将服务自动注册到 Nacos 服务端,并且能够动态无感知的刷新某个服务实例的服务列表,为分布式系统提供服务注册与发现功能 一.创建服务 1.创建项目 pom ...

  2. Nginx导航

    简介 最近都在弄微服务的东西,现在来记录下收获.我从一知半解到现在能从0搭建使用最大的感触有两点 1.微服务各大组件的版本很多,网上很多博客内容不一定适合你的版本,很多时候苦苦琢磨都是无用功 2.网上 ...

  3. 【转载】Windows 10系统默认将画面显示比例调整至125%或150%,最高分辨率已经达到3840×2160(4K)这一级别。

    高分屏打开软件界面模糊?不会设置太浪费 2017-08-31 19:37 抹又重彩 现在有好多朋友都喜欢并买了高分屏笔记本电脑.高分屏笔记本就是配有高分辨率屏幕的笔记本.为了给用户带来更好的视觉体验, ...

  4. 一、MegaCli命令介绍

    一.MegaCli命令介绍 MegaCli是一款管理维护硬件RAID软件,可以用来查看raid信息等MegaCli 的Media Error Count: 0 Other Error Count: 0 ...

  5. Linux进阶之排错

    Permission denied 检查selinux长时间没有响应 防火墙 nginx和httpd可以自检nginx和httpd有日志 /var/log/nginx/ /var/log/httpd/ ...

  6. Mybatis Mapper 映射文件(xxxMapper.xml)

    什么是 Mapper 映射文件 Mapper 映射文件是 Mybatis 用于实现 ORM 映射规则的配置文件,Mybatis 通过映射文件可将数据库查询结构映射为 Java 对象. 创建 Mappe ...

  7. osi七层模型与tcp/ip四层模型的差别

    OSI 七层协议 应用层 表示层 会话层 运输层 网络层 数据链路层 物理层 TCP/IP 四层协议 应用层 运输层 网际层 网络接口层 五层协议: 应用层 为用户的应用进程提供服务 HTTP SMT ...

  8. 5分钟安装docker教程

    Centos安装docker需要操作系统是 CentOS 7 or 8,必须启用centos extras存储库.默认情况下,此存储库处于启用状态,但如果已禁用它,则需要重新启用它. 卸载旧版本 老版 ...

  9. kind:Kubernetes in Docker,单机测试 Kubernetes 群集的最佳方案

    请访问原文发布链接:https://sysin.org/article/kind/,查看最新版. 作者:gc(at)sysin.org,主页:www.sysin.org 2021.04.28 更新,k ...

  10. VMware虚拟机CentOS磁盘扩容

    版本信息: VMware Workstation 15 Pro  15.5.2 build-15785246, CentOS7 原虚拟机默认20G,安装东西多了,磁盘空间不够用, docker mys ...