通過學(xué)習(xí)這個實例項目,我們將積累點滴技術(shù)知識和實踐經(jīng)驗,進(jìn)一步提升我們的開發(fā)能力。學(xué)習(xí)如何構(gòu)建增刪改查功能的完整應(yīng)用,從數(shù)據(jù)庫訪問到前端界面的開發(fā),涵蓋了全棧開發(fā)的方方面面。此外,我們還將學(xué)會處理常見的業(yè)務(wù)邏輯和數(shù)據(jù)操作,提高編碼質(zhì)量和效率。通過實踐,我們能夠加深對軟件開發(fā)流程和最佳實踐的理解,培養(yǎng)解決問題和調(diào)試的能力。這些寶貴的經(jīng)驗將使我們更加自信地應(yīng)對實際項目中的挑戰(zhàn),并在職業(yè)生涯中取得更大的成功。
1. 技術(shù)棧介紹
1.1 Springboot
Spring Boot(Spring引導(dǎo)):
Spring Boot是一個簡化Java應(yīng)用程序開發(fā)的框架,特別是Web應(yīng)用程序開發(fā)。它通過約定優(yōu)于配置的方式,提供了構(gòu)建獨立的、生產(chǎn)級應(yīng)用程序的簡化方法。Spring Boot通過提供合理的默認(rèn)設(shè)置和自動配置各種組件,消除了手動配置的需求,使開發(fā)人員更加專注于應(yīng)用程序邏輯。
1.2 MyBatis
MyBatis(MyBatis):
MyBatis是一種持久化框架,它提供了一種通過SQL查詢與關(guān)系型數(shù)據(jù)庫進(jìn)行交互,并將結(jié)果映射到Java對象的方式。它簡化了數(shù)據(jù)庫訪問層,減少了樣板代碼,并允許開發(fā)人員直接編寫SQL查詢。MyBatis支持各種數(shù)據(jù)庫供應(yīng)商,并提供了動態(tài)SQL、對象映射和事務(wù)管理等功能。
1.3 Layui
Layui(layui):
Layui是一個流行的JavaScript庫,用于構(gòu)建Web界面。它提供了一套全面的UI組件和模塊,包括網(wǎng)格布局、表單元素、彈出層等。Layui具有簡潔易用的特點,可以快速構(gòu)建出美觀、交互友好的前端界面。
2. 開發(fā)環(huán)境
類型 | 內(nèi)容 |
---|---|
開發(fā)語言 | Java |
框架 | Spring Boot |
前端 | Layui |
JDK版本 | JDK1.8 |
數(shù)據(jù)庫 | MySQL 5.7 |
數(shù)據(jù)庫工具 | Navicat15 |
開發(fā)軟件 | IntelliJ IDEA |
Maven包 | Maven3.6.1 |
瀏覽器 | 谷歌瀏覽器 |
2.1 前端示例代碼
首頁index.html代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-COMPATIBLE" content="IE=edge,chrome=1">
<meta http-equiv="Cache-Control" content="no-siteapp">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>增刪改查項目實例</title>
<link rel="stylesheet" type="text/css" href="layui/css/layui.css">
<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript" src="scripts/Main.js"></script>
<style>
.layui-form-label.layui-required:after{
content:'*';
color:red;
font-weight: bold;
position: absolute;
top:5px;
left:15px;
}
.frm {
margin-top: 20px;
}
.div-hide {
display: none;
}
</style>
</head>
<body>
<blockquote class="layui-elem-quote layui-text">
<table>
<tr>
<td>
<input type="text" id="p_id" placeholder="編號" autocomplete="off" class="layui-input ipt1">
</td>
<td width="10"> </td>
<td>
<input type="text" id="p_name" placeholder="姓名" autocomplete="off" class="layui-input ipt1">
</td>
<td width="10"> </td>
<td>
<button class="layui-btn btn-search"><i class="layui-icon"></i>查詢</button>
<button class="layui-btn layui-btn-normal btn-add"><i class="layui-icon"></i>添加</button>
<button class="layui-btn layui-btn-warm btn-edit"><i class="layui-icon"></i>修改</button>
<button class="layui-btn layui-btn-danger btn-del"><i class="layui-icon"></i>刪除</button>
</td>
</tr>
</table>
</blockquote>
<!--數(shù)據(jù)表格-->
<table id="PersonTable" class="layui-hide" lay-filter="person-table"></table>
<!--人員表單-->
<div id="box" class="div-hide">
<form id="PersonForm" class="layui-form frm" lay-filter="person-form">
<div class="layui-form-item">
<label class="layui-form-label">編號</label>
<div class="layui-input-inline">
<input type="text" id="pid" name="id" required lay-verify="required" placeholder="編號"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">姓名</label>
<div class="layui-input-inline">
<input type="text" name="name" required lay-verify="required" placeholder="姓名"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">年齡</label>
<div class="layui-input-inline">
<input type="text" name="age" required lay-verify="required" placeholder="年齡"
autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">性別</label>
<input name="sex" value="男" title="男" checked type="radio">
<input name="sex" value="女" title="女" type="radio">
</div>
<div>
<button class="layui-btn form-save layui-hide" lay-submit lay-filter="save"></button>
<button type="reset" class="layui-btn form-reset layui-hide"></button>
</div>
</form>
</div>
</body>
</html>
2.2 后端示例代碼
package cn.lj.yy.controller;
import cn.lj.yy.model.Person;
import cn.lj.yy.service.PersonService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping(value = "Person")
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping(value = "getAllPersonList", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public String getAllPersonList(HttpServletRequest request) {
int pageSize = Integer.parseInt(request.getParameter("limit"));
int pageNumber = Integer.parseInt(request.getParameter("page"));
PageHelper.startPage(pageNumber, pageSize);
String _id = request.getParameter("id");
int id = 0;
if (_id != null && !"".equals(_id)) {
id = Integer.parseInt(_id);
}
String name = request.getParameter("name");
Map params = new HashMap();
params.put("id", id);
params.put("name", name);
Page<Person> data = personService.getAllPersonByPage(params);
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", 0);
jsonObject.put("count", data.getTotal());
jsonObject.put("data", data);
return jsonObject.toJSONString();
}
@RequestMapping(value = "addPerson", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public String addPerson(Person person) {
JSONObject jsonObject = new JSONObject();
if (personService.addPerson(person)) {
jsonObject.put("success", true);
jsonObject.put("message", "添加人員信息成功");
} else {
jsonObject.put("success", false);
jsonObject.put("message", "添加人員信息失敗");
}
return jsonObject.toJSONString();
}
@RequestMapping(value = "updatePerson", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public String updatePerson(Person person) {
JSONObject jsonObject = new JSONObject();
if (personService.updatePerson(person)) {
jsonObject.put("success", true);
jsonObject.put("message", "修改人員信息成功");
} else {
jsonObject.put("success", false);
jsonObject.put("message", "修改人員信息失敗");
}
return jsonObject.toJSONString();
}
@RequestMapping(value = "deletePerson", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public String deletePerson(int id) {
JSONObject jsonObject = new JSONObject();
if (personService.deletePerson(id)) {
jsonObject.put("success", true);
jsonObject.put("message", "刪除人員信息成功");
} else {
jsonObject.put("success", false);
jsonObject.put("message", "刪除人員信息失敗");
}
return jsonObject.toJSONString();
}
}
2.3 數(shù)據(jù)庫建表語句
建表語句:
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50734
Source Host : localhost:3306
Source Schema : yy2
Target Server Type : MySQL
Target Server Version : 50734
File Encoding : 65001
Date: 24/05/2023 21:52:51
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for person
-- ----------------------------
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
`sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = COMPACT;
-- ----------------------------
-- Records of person
-- ----------------------------
INSERT INTO `person` VALUES (1, '學(xué)生1', 18, '男');
INSERT INTO `person` VALUES (2, '學(xué)生3', 18, '女');
INSERT INTO `person` VALUES (3, '學(xué)生5', 20, '男');
INSERT INTO `person` VALUES (4, '學(xué)生6', 19, '女');
INSERT INTO `person` VALUES (6, '管理員', 22, '男');
INSERT INTO `person` VALUES (7, 'Rucoding', 18, '女');
SET FOREIGN_KEY_CHECKS = 1;
3. 項目截圖
4. 運行截圖
4.1 查詢界面
4.2 新增界面
4.3 修改界面
4.4 刪除界面
5. 小結(jié)
通過Spring Boot、MyBatis和Layui實現(xiàn)增刪改查項目實例,我們可以學(xué)到以下內(nèi)容:
-
使用Spring Boot簡化Java應(yīng)用程序開發(fā):Spring Boot提供了一種簡化的開發(fā)方式,通過自動配置和默認(rèn)設(shè)置,減少了繁瑣的手動配置,使開發(fā)人員能更專注于業(yè)務(wù)邏輯的實現(xiàn)。
-
數(shù)據(jù)庫訪問與映射:使用MyBatis,可以學(xué)習(xí)如何編寫SQL查詢,并將查詢結(jié)果映射到Java對象中。MyBatis提供了一些高級特性,如動態(tài)SQL和事務(wù)管理,可以提升數(shù)據(jù)庫訪問的靈活性和可靠性。
-
前端界面開發(fā):通過Layui,可以學(xué)習(xí)如何使用現(xiàn)代化的JavaScript庫構(gòu)建美觀、交互友好的前端界面。Layui提供了豐富的UI組件和模塊,可以快速搭建出符合用戶期望的界面。
整個項目實例結(jié)合了Spring Boot、MyBatis和Layui的優(yōu)勢,展示了一個完整的增刪改查功能的實現(xiàn)過程??梢酝ㄟ^這個實例了解如何將這些技術(shù)整合在一起,提高開發(fā)效率,并構(gòu)建出功能完善、易于維護(hù)的Web應(yīng)用程序。
6. 完整代碼下載
下載傳送門文章來源地址http://www.zghlxwxcb.cn/news/detail-458502.html
7. plus+簡易后臺管理系統(tǒng)
簡易后臺管理系統(tǒng),升級版plus+,學(xué)習(xí)Spring Boot、Layui和Mybatis-plus的實戰(zhàn)經(jīng)驗,構(gòu)建簡易后臺管理系統(tǒng)。掌握快速開發(fā)、前端界面設(shè)計和數(shù)據(jù)庫操作,學(xué)習(xí)最佳實踐和定制擴(kuò)展。提供實用項目基礎(chǔ),培養(yǎng)綜合能力。適合學(xué)習(xí)和實踐,助力我們快速構(gòu)建高效后臺管理系統(tǒng)。
系統(tǒng)圖示:文章來源:http://www.zghlxwxcb.cn/news/detail-458502.html
下載傳送門
到了這里,關(guān)于開發(fā)實例:Spring Boot、MyBatis和Layui打造增刪改查項目的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!