SpringBoot系列文章目录
SpringBoot知识范围-学习步骤【JSB系列之000】
文章目录
本项目效果图
本项目特色, springBoot+ mytis,文件上传, EXCEL导入,EXCEL导出,echarts 图表。layui界面
SpringBoot技术很多很多
韩顺平说:学习JAVA的人有两大难
第一困惑,JAVA能百度到的知识太多太多,完全不知道学啥
第二困惑,就是不知道以什么样的顺序去学,有的时候乱看一堆视频有遗漏,有的时候,两边的视频还有重复。
然后,再来看一般的springboot 的书籍中内容。
一般是这样。右侧为基础
一眼看上去直接就晕了。然后这么多东西,哪个是哪个的基础?
直接就懵了。上网查吧?
不上网还好,一上网一查,结果查到的是满天的学习完了JAVA 学到了springboot 工资不到3000。
我还能学下去?其实,这些人大多数都是对着视频上的代码说自己会。事实上会不会自己心里最有数。
如果基础不牢,要回避的知识
假定,你在学校做过了JAVA的小项目了。(我指的是swing,servlet 之类) 可能会点SSM,也可能不会,那我马上就要毕业了,我得搞项目,搞毕设呀。啊!!!
- 第一个要回避的就是JWT。
- 上面的不使用了,shiro 也就不需要了
- 然后是redis
说一下理由。第一,你一个人使用的系统,你用啥JWT?Session. cookie , JWT 都是你一个人在用。完全不存在用户信息泄漏。
单点登录就更用不上了。一共就一个系统。
redis 也不需要,别说你一个人在使用,就是全班的人都在使用50人在线,redis 的优势也自不出来。mysql 数据库连这一点点的数据都不能快速响应,那PHP的平台的不是要直接宕机?而且mybatis 本身还有缓存。
当然了,这是指你自己要把代码弄懂的这个前提下说的。如果说别人运行好的。那用啥环境也不是你能决定了,是吧?
环境及工具:
本系列环境
环境 | win11 |
---|---|
工具 | idea 2018 |
jdk | 1.8 |
数据库 | mysql5.5 |
maven | 3.6.0 |
项目导入方式 | maven 导入 |
数据库前端工具 | mysql-front (navicat 也可以) |
数据库前端工具:mysql-front (navicat 也可以)
主要是这些跟PHPStudy 2018 整合了,所以有的时候懒的打开navicat
关于环境的重要性,直接看上面的《SpringBoot 的项目编译即报错处理–JSB系列之001》
项目里可能要用到的技术
前后端分离
这是一个好技术。前后端分离的主要概念就是:后台只需提供API接口,前端调用AJAX实现数据呈现。 现状与分歧. 作为一名前端开发人员,我们应该尝试一些新颖的技术,完善每一个细节,后端的开发人员就无脑疯狂的输出json。
在传统的web应用开发中,大多数的程序员会将浏览器作为前后端的分界线。将浏览器中为用户进行页面展示的部分称之为前端,而将运行在服务器,为前端提供业务逻辑和数据准备的所有代码统称为后端。
难不难工作量是不一定,但是,两边的难度是真的降了。
后端的只用idea community IDEA社区版(Community)版本就足够。前端高级的玩玩webStorm,不行用hbuilder,vscode 好象都能写代码。甚至还有人用editplus ++ , nodepad 之流了,这就有点过份了哈。
json
JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写。
这个东东一出来就直接把xml 的这种格式按地上摩擦了。而且可以可读性好,也把一些不可读的数据包形式也给取代了。
其它的必要知识
- 网页三剑客,这个不用说了
- JAVA,你总得会idea吧
- maven 这个也是必须的
配置文件
这个项目的重点是配置文件,因为这个项目使用的springboot + mybatis 并非是mybatis plus。
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.2.2.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<groupId>com.silk</groupId>
<artifactId>dormitory</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dormitory</name>
<description>A Spring Boot Dormitory Project by Linda
Silk</description>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<dependencies>
<!-- FastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<!-- PageHelper分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- Excel相关 -->
<!-- 1. Poi:针对Excel2003(后缀为.xls) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<!-- 2. Poi-ooxml:针对Excel2007及以后版本(后缀为.xlsx) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<!-- 其它 -->
<!-- 1. 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 2. io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml配置
# Tomcat
server:
tomcat:
uri-encoding: UTF-8
port: 8080
servlet:
context-path: /dormitory
#配置数据源的属性
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/jbs_dormitory?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
username: root
password: 123456
jackson:
date-format: yyyy年MM月dd日
# 配置静态资源
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
mybatis:
mapper-locations: mapper/*.xml
pagehelper:
helper-dialect: mysql # 根据方言来选择合适的分页方式
上代码
Controller代码
package com.silk.controller;
import com.silk.entity.User;
import com.silk.service.UserService;
import com.silk.utils.Result;
import com.silk.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
/**
* @author dearmite
* @date 2023年3月09日, 周二
*/
@RestController
public class LoginController {
@Autowired
private UserService userService;
@PostMapping("/login")
// 从前端传入的JSON中获取对象信息
public Result login(@RequestBody User user){
User entity = userService.login(user.getUserName(), user.getUserPwd(), user.getUserType());
HttpSession session = WebUtils.getSession();
if (entity != null){
//String token = JwtUtil.sign(entity);
Map map = new HashMap();
//map.put(JwtUtil.token, token);
map.put("user", entity);
session.setAttribute("user", entity);
return Result.ok("登录成功", map);
}else {
return Result.fail("用户名或密码或角色错误");
}
}
}
package com.silk.utils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class WebUtils {
/**
* 得到requset
*/
public static HttpServletRequest getRequest() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
return request;
}
/**
* 得到session
*/
public static HttpSession getSession() {
return getRequest().getSession();
}
}
其它controller里使用从session里取user.
package com.silk.controller;
import com.silk.entity.Building;
import com.silk.entity.User;
import com.silk.service.BuildingService;
import com.silk.service.RoomService;
import com.silk.service.UserService;
import com.silk.utils.Result;
import com.silk.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @author dearmite
* @date 2023年3月15日, 周一
*/
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private UserService userService;
@Autowired
private BuildingService buildingService;
@Autowired
private RoomService roomService;
@GetMapping("/info")
public Result info(HttpServletRequest request){
User param = (User) WebUtils.getSession().getAttribute("user"); // 获取前端发起请求的用户信息
System.out.println(param);
User student = userService.detail(param.getId());
student.setRoom(roomService.detail(student.getRoomId()));
student.setBuilding(buildingService.detail(student.getRoomId()));
System.out.println(student);
return Result.ok(student);
}
}
Service
package com.silk.service;
import com.silk.mapper.RoomMapper;
import com.silk.entity.Room;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
/**
* @author dearmite
* @date 2023年3月13日, 周六
*/
@Service
public class RoomService {
@Autowired
private RoomMapper roomMapper;
public int create(Room room) {
return roomMapper.create(room);
}
public int delete(String ids) {
String[] arr = ids.split(",");
int row = 0;
for (String s : arr) {
if(!StringUtils.isEmpty(s)){
roomMapper.delete(Integer.parseInt(s));
row++;
}
}
return row;
}
public int delete(Integer id) {
return roomMapper.delete(id);
}
public int update(Room room) {
return roomMapper.update(room);
}
public int updateSelective(Room room) {
return roomMapper.updateSelective(room);
}
public PageInfo<Room> query(Room room) {
if(room != null && room.getPage() != null){
PageHelper.startPage(room.getPage(),room.getLimit());
}
return new PageInfo<Room>(roomMapper.query(room));
}
public Room detail(Integer id) {
return roomMapper.detail(id);
}
public int count(Room room) {
return roomMapper.count(room);
}
public int capacityPlusOne(Integer id){
return roomMapper.capacityPlusOne(id);
}
public int capacityMinusOne(Integer id){
return roomMapper.capacityMinusOne(id);
}
public int buildingTotalStudentBedAmount(Integer buildingId){
return roomMapper.buildingTotalStudentBedAmount(buildingId);
}
public double queryRoomBalance(Integer roomId){
return roomMapper.queryRoomBalance(roomId);
}
public int changeBalance(Double newBalance, Integer roomId){
return roomMapper.changeBalance(newBalance, roomId);
}
// 查询某栋楼的学生床位总数
public int queryTotalStuBed(Integer buildingId){
return roomMapper.queryTotalStuBed(buildingId);
}
// public int queryTotalStuRoom(Integer buildingId){
// return roomMapper.queryTotalStuRoom(buildingId);
// }
public List<Integer> queryEachStuRoomId(Integer buildingId){
return roomMapper.queryEachStuRoomId(buildingId);
}
public int queryEachStuRoomFreeBedAmount(Integer roomId){
return roomMapper.queryEachStuRoomFreeBedAmount(roomId);
}
}
mapper
package com.silk.mapper;
import java.util.List;
import java.util.Map;
import com.silk.entity.Room;
public interface RoomMapper {
public int create(Room room);
public int delete(Integer id);
public int update(Room room);
public int updateSelective(Room room);
public int capacityPlusOne(Integer id);
public int capacityMinusOne(Integer id);
public List<Room> query(Room room);
public Room detail(Integer id);
public int count(Room room);
public int buildingTotalStudentBedAmount(Integer buildingId);
public double queryRoomBalance(Integer roomId);
public int changeBalance(Double newBalance, Integer roomId);
public int queryTotalStuBed(Integer buildingId);
// public int queryTotalStuRoom(Integer buildingId);
public List<Integer> queryEachStuRoomId(Integer buildingId);
public int queryEachStuRoomFreeBedAmount(Integer roomId);
}
实体类
这里有必要说一下idea2018并不直接支持lombok,需要自己导入一下。
跑起来
后台
数据库的部分你自己搞定吧,如果到了做毕设还不会数据库,这个还是要下点功夫了。
这么点代码+数据库就能跑起来?没错,他确实是能运行
package com.silk;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author dearmite
* @date
*/
@SpringBootApplication
@MapperScan({
"com.silk.mapper"})
public class DormitoryApplication {
public static void main(String[] args) {
SpringApplication.run(DormitoryApplication.class, args);
//http://localhost:8080/dormitory/page/login.html
}
}
用浏览器最原始的方式验证
在浏览器上念出一段神奇的古老埃及法老的字符。
http://localhost:8080/dormitory/page/login.html
页面展示
宿舍管理 员可以看到下面的三个菜单
好,一步成功点亮,关电,拉闸,领盒饭!走人。
总结
提示:IT是一个要多动手的职业,一定要多练不要贪快:
这一部涉及到的知识可以说非常之多。能花一周的时间把这篇文章里的东西弄一个大概并运行出来,也是很不容易的,而且难度也不低,准确一点说,培训机构也能让你似懂非懂的把这个代码跑起来。
做到这一步,你会了springBoot了么?会了,但是现在你会的都是初级基本上就是对付一下简单项目。传的对象复杂了,数据信息多了,都可能会出现这样那样的问题。
还有就是springboot 的缓存,JWT,消息队列,安全机制 ,任务管理其实他们从SSM甚至SSH的时候就有了。只是springboot 一下子全集成过来了。你去看别人的SSM项目 ,这些东西也都有。
所以,程序猿与学员纯在着本质的差别。那么如何提高呢?当然要多读别人的成功项目了。
GITEE上面存在着大量的springboot的项目,英文没有问题的话,可以去github。
配套资源
SpringBoot宿舍管理系统–【JSB项目实战】
https://download.csdn.net/download/dearmite/88175536