上篇文章中提到,由于当前项目主要以python为技术栈,后端技术框架为django,项目系统上线初期系统用户少、访问量小,系统运行流畅,但随着业务需求的增加以及访问量的不断加大,系统问题也随之而增多,在综合生态、性能等方面考虑,选择当前比较热门的SSM后端技术,以下内容为编写的一个查询员工数据的后端以及简单的前端页面显示.
提示:下面案例仅供参考,如有雷同,纯属巧合
SSM是spring boot、spring mvc、mybatis三个模块的简称,spring boot是一个精简版的spring,内置有tomcat容器,还可以自定义需要的组件,spring mvc是指model、view、controller三个模块的简称,更确切的来说是一种设计模式,view是指需要展示的内容,这也是使用方最关注的地方.
//mybatis依赖,
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.3.0
//苞米豆依赖,可以管理多个数据源的情况
com.baomidou
dynamic-datasource-spring-boot-starter
2.5.8
//mysql jdbc驱动包
com.mysql
mysql-connector-j
runtime
代码如下(示例):
//创建数据库(可根据需要,自行定义)
CREATE DATABASE `Mybatis` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
//切换到数据库
use Mybatis;
//创建员工表
CREATE TABLE `employee` (
`id` int NOT NULL AUTO_INCREMENT,
`last_name` varchar(255) DEFAULT NULL,
`gender` varchar(10) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
提示:yml文件作用效果等同于properties文件,但yml文件观感更好,以缩进来展示层级关系,图中密码被我隐藏了,可根据自身环境不同,自行配置
提示:EmployeeServiceImpl代码太长无法截图,只能粘贴代码了
package com.opple.spring.springdemo.service;
import com.opple.spring.springdemo.bean.Employee;
import com.opple.spring.springdemo.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
private EmployeeMapper mapper;
@Override
public Employee getEmpById(Integer id) {
System.out.println("您输入的员工id是: " + id);
Employee e = mapper.getEmpById(id);
System.out.println("员工信息查询成功 !" );
return e;
}
@Override
public void deleteEmpById(Integer id) {
mapper.deleteEmpById(id);
System.out.println("员工id为: " + id + "信息删除成功!");
}
@Override
public void updateEmp(Employee e) {
mapper.updateEmp(e);
System.out.println("员工信息更新成功!");
}
@Override
public void insertEmp(Employee e) {
mapper.insertEmp(e);
System.out.println("员工信息插入成功!");
}
@Override
public List<Employee> getAllEmp() {
System.out.println("所有员工信息查询成功!");
return mapper.getAllEmp();
}
}
package com.opple.spring.springdemo.controller;
import com.opple.spring.springdemo.bean.Employee;
import com.opple.spring.springdemo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "/emp")
public Object handleEmp(String op,Integer id,String lastname,String gender,String email){
Employee employee = new Employee(id,lastname,gender,email);
if("insert".equals(op)){
employeeService.insertEmp(employee);
return "ok";
}else {
if(id == null){
return "id非法";
}
switch (op){
case "select" :
Employee emp = employeeService.getEmpById(id);
return emp == null ? "查无此人" : emp;
case "delete" :
employeeService.deleteEmpById(id);
return "员工信息删除成功!";
case "insert" :
employeeService.insertEmp(employee);
return "员工信息增加成功!";
case "update" :
employeeService.updateEmp(employee);
return "员工信息更新成功!";
default:
return "ok";
}
}
}
@RequestMapping(value="/getAllEmp")
public Object handleGetAllEmp(){
List<Employee> emps = employeeService.getAllEmp();
return emps;
}
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>员工系统title>
<style type="text/css">
label{
cursor: pointer;
display: inline-block;
padding: 3px 6px;
text-align: right;
width: 55px;
vertical-align: top;
}
style>
head>
<body>
<form action="/emp">
<fieldset>
<legend>单个员工操作legend>
<input type="radio" name="op" value="select">查询
<input type="radio" name="op" value="insert">新增
<input type="radio" name="op" value="delete">删除
<input type="radio" name="op" value="update">更新br>
<label>id:label><input name="id" value=""> br>
<label>name:label><input name="lastname" value=""> br>
<label>gender:label><input name="gender" value=""> br>
<label>email:label><input name="email" value=""> br>
<input value="操作" type="submit">
fieldset>
form>
<a href="/getAllEmp">查询所有员工a>
body>
html>
至此一个简单的SSM框架的spring项目已经完成,由于时间仓促,还有许多需要完善的地方,希望各位大牛能够多多指点,后续本人再继续加以改进,另外,如果大家有好的技术文章或观点,也欢迎一起分享讨论!