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项目是一件极其简单的事情.界面 ...
随机推荐
- CRM系统实施的原则
在我们使用CRM系统服务企业和客户之前,需要先系统的实施它.使用CRM系统却没有发挥它应有价值的案例很多,那么我们要怎样才能让CRM的作用发挥到最大,并确保它是成功的?那么今天小编跟您聊一聊,您的企业 ...
- 密码学系列之:memory-hard函数
密码学系列之:memory-hard函数 目录 简介 为什么需要MHF Memory hard的评估方法 MHF的种类 MHF的密码学意义 memory-hard在MHF中的应用 简介 Memory ...
- [刷题] 102 Binary Tree Level Order Traversal
要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...
- [Java] Hibernate
背景 持久化技术框架 封装了JDBC做数据库相关功能开发的重复性工作 比如创建连接,关闭连接,把字段逐一映射到属性中等 使用 在数据库中创建表 创建java project 导入hibernate依赖 ...
- PyCharm和JDK安装与配置(windows)
原创 PyCharm和JDK安装与配置(windows) mb5cd21e691f31a关注0人评论2024人阅读2020-03-20 21:08:41 一.PyCharm安装与配置 PyChar ...
- 基于多端口的Web服务
[Centos7.4版本] !!!测试环境我们首关闭防火墙和selinux [root@localhost ~]# systemctl stop firewalld [root@localhost ~ ...
- Linux进阶之链路聚合
CentOS7用命令配置链路聚合 链路聚合是一个计算机网络术语,将多个物理端口汇聚在一起,形成一个逻辑端口,以实现出入流量在各成员端口的负荷分担,交换机根据用户配置的端口负荷分担策略决定网络封包从哪个 ...
- 5.8-12 watch、which、whereis、locate、updatedb
5.8 watch:监视命令执行情况 watch命令可以以全屏的方式动态显示命令或程序的执行情况. -n 命令执行的间隔时间,默认为2s -d 高亮显示命令结果的变动之处 -t ...
- java为什么是一次编译,多次解释
在编程语言中分为编译型语言.解释型语言,而我们的java语言既是编译型的也是解释型的,因为比如c语言是编译型的,因为我们将c语言的源代码编译过后就形成了.exe文件,这样windows系统就可以直接运 ...
- docker启动失败如何查看容器日志
docker启动失败如何查看容器日志 在使用docker的时候,在某些未知的情况下可能启动了容器,但是过了没几秒容器自动退出了.这个时候如何排查问题呢? 通常碰到这种情况无非就是环境有问题或者应用有问 ...