挑战5分钟内基于Springboot+SpringMVC+Mybatis-plus快速构建web后端三层架构
目标
在清晨的代码编辑器上,一场新的挑战即将开始。程序员们肃立于安静的办公室,眼神专注地盯着屏幕,等待着编译器的一声提示。
随着编译器输出的激动人心的”start!”的提示,战斗的序幕拉开了。Bug如潮水般涌来,程序员们快速调整键盘,代码在屏幕上飞速滚动,IDE中的光标如箭一般穿梭,指引着他们找出每一个漏洞。代码的碰撞声、键盘的敲击声、鼠标的点击声交织成一曲激烈的交响乐,扣人心弦。
在这个混乱的调试中,每个程序员都展现出顶尖的技艺与冷静的勇气。他们忘我地追踪Bug,代码间的逻辑错综复杂,他们努力理清每一行代码的错误,誓言修复每一个漏洞。长时间的疲劳和思考在他们身上无处不在,但他们坚定不移,因为他们知道,这场战斗不仅仅是为了完成任务,更是为了程序的稳定与优化。
随着代码的逐步修复,胜利的曙光逐渐浮现。Bug一个个被排除,程序稳定运行,最终的”Deploy Successful!”的消息如太阳升起,标志着任务的完成和程序的成功运行。
这场战斗,不仅仅是一场技术上的挑战,更是一场耐心与智慧的考验。他们用思维与代码铸就了成功的光辉,为了项目的顺利进行,为了自己对技术的追求与信仰,他们勇敢地战斗着
开始挑战
打开idea
创建Spring项目
依赖就选springweb
创建完成首先在xml文件里配置依赖
4.0.0
com.bigdata1421
demo
0.0.1-SNAPSHOT
demo
demo
1.8
UTF-8
UTF-8
2.6.13
org.projectlombok
lombok
com.baomidou
mybatis-plus-boot-starter
3.4.3
com.alibaba
druid-spring-boot-starter
1.2.6
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.maven.plugins
maven-compiler-plugin
3.8.1
1.8
1.8
UTF-8
org.springframework.boot
spring-boot-maven-plugin
${spring-boot.version}
com.example.demo.DemoApplication
true
repackage
repackage
接着配置数据库连接信息
server:
port: 80
spring:
datasource:
druid:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
username: root
password: 123456
# 配置mybatis-plus
mybatis-plus:
global-config:
db—config:
table-prefix: tbl_
接下来
用lombok快速封装实体类
package com.example.demo.domain;
import lombok.Data;
@Data
public class User {
private String id;
private String name;
private String age;
private String gender;
}
用Mybatis-plus开发数据层接口
package com.example.demo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.domain.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao extends BaseMapper {
}
开发业务层
面向接口编程
先在接口定义了五个方法
package com.example.demo.service;
import com.example.demo.domain.User;
import java.util.List;
public interface UserService {
public boolean save(User user);
public User getById(Integer id);
public boolean update(User user);
public boolean delete(Integer id);
public List getAll();
}
书写实现类
在注入数据层接口的同时
重写业务层方法
package com.example.demo.service.impl;
import com.example.demo.dao.UserDao;
import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User getById(Integer id) {
return userDao.selectById(id);
}
@Override
public boolean save(User user) {
return userDao.insert(user)>0;
}
@Override
public boolean update(User user) {
return userDao.updateById(user)>0;
}
@Override
public boolean delete(Integer id) {
return userDao.deleteById(id)>0;
}
@Override
public List getAll() {
return userDao.selectList(null);
}
}
创建控制器
controller层MVC类
与前端页面进行交互
package com.example.demo.controller;
import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
//注入业务层
@Autowired
private UserService userService;
//规定请求方式
@GetMapping
public List getAll(){
return userService.getAll();
}
@PostMapping
public Boolean save(@RequestBody User user){
return userService.save(user);
}
@PutMapping
public Boolean update(@RequestBody User user){
return userService.update(user);
}
@DeleteMapping("{id}")
public Boolean delete(@PathVariable Integer id){
return userService.delete(id);
}
@GetMapping("{id}")
public User getById(@PathVariable Integer id){
return userService.getById(id);
}
}
这样一个基本的框架就已经完成
我们把springboot项目启动起来
我们这时候到postman里
发起请求 去进行一个查询
成功查询
挑战成功
耗时4分49秒!
爽
个人号推广
博客主页
多多!-CSDN博客
Web后端开发
https://blog.csdn.net/qq_30500575/category_12624592.html?spm=1001.2014.3001.5482
Web前端开发
https://blog.csdn.net/qq_30500575/category_12642989.html?spm=1001.2014.3001.5482
数据库开发
https://blog.csdn.net/qq_30500575/category_12651993.html?spm=1001.2014.3001.5482
项目实战
https://blog.csdn.net/qq_30500575/category_12699801.html?spm=1001.2014.3001.5482
算法与数据结构
https://blog.csdn.net/qq_30500575/category_12630954.html?spm=1001.2014.3001.5482
计算机基础
https://blog.csdn.net/qq_30500575/category_12701605.html?spm=1001.2014.3001.5482
回忆录
https://blog.csdn.net/qq_30500575/category_12620276.html?spm=1001.2014.3001.5482