Mybatis框架实现单表增删改查

news/2024/12/26 22:27:10/文章来源:https://www.cnblogs.com/yololin/p/18634323

一 数据库准备
在 MySQL 数据库中创建travel_application表:
CREATE TABLE travel_application (
ID varchar(8) NOT NULL,
name varchar(255) DEFAULT NULL,
department varchar(255) DEFAULT NULL,
destination varchar(255) DEFAULT NULL,
departuredate date DEFAULT NULL,
returndate date DEFAULT NULL,
Type varchar(255) DEFAULT NULL,
Typecontent varchar(255) DEFAULT NULL,
Reason varchar(255) DEFAULT NULL,
State varchar(255) DEFAULT NULL,
Statereason varchar(255) DEFAULT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2. 创建 Spring Boot 项目
在 IDEA 中创建一个 Spring Boot 项目,选择Web和MyBatis Framework依赖。
3. 持久层(Dao)
实体类(Entity):在com.example.entity包下创建TravelApplication实体类。
package com.example.entity;

import java.util.Date;

public class TravelApplication {
private String id;
private String name;
private String department;
private String destination;
private Date departuredate;
private Date returndate;
private String type;
private String typecontent;
private String reason;
private String state;
private String statereason;

// 生成 Getter 和 Setter 方法
//...

}
Mapper 接口(Dao):在com.example.dao包下创建TravelApplicationMapper接口。
package com.example.dao;

import com.example.entity.TravelApplication;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TravelApplicationMapper {
// 插入出差申请
@Insert("INSERT INTO travel_application (ID, name, department, destination, departuredate, returndate, Type, Typecontent, Reason, State) VALUES (#{id}, #{name}, #{department}, #{destination}, #{departuredate}, #{returndate}, #{type}, #{typecontent}, #{reason}, #{state})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void insertTravelApplication(TravelApplication application);

// 根据ID删除出差申请
@Delete("DELETE FROM travel_application WHERE ID = #{id}")
void deleteTravelApplication(String id);// 更新出差申请
@Update("UPDATE travel_application SET destination = #{destination}, departuredate = #{departuredate}, returndate = #{returndate}, Type = #{type}, Typecontent = #{typecontent}, Reason = #{reason}, State = #{state} WHERE ID = #{id}")
void updateTravelApplication(TravelApplication application);// 查询所有出差申请
@Select("SELECT * FROM travel_application")
List<TravelApplication> selectAllTravelApplications();// 根据条件查询出差申请
@Select("<script>SELECT * FROM travel_application WHERE 1=1 " +"<if test='name!= null and name!= \"\"'> AND name = #{name} </if>" +"<if test='department!= null and department!= \"\"'> AND department = #{department} </if>" +"<if test='state!= null and state!= \"\"'> AND State = #{state} </if>" +"</script>")
List<TravelApplication> selectTravelApplicationsByCondition(TravelApplication application);

}
Mapper XML 文件:在src/main/resources/mapper目录下创建TravelApplicationMapper.xml文件。

4. 业务层(Service) 在com.example.service包下创建TravelApplicationService类。 package com.example.service;

import com.example.dao.TravelApplicationMapper;
import com.example.entity.TravelApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TravelApplicationService {
@Autowired
private TravelApplicationMapper travelApplicationMapper;

public void insertTravelApplication(TravelApplication application) {travelApplicationMapper.insertTravelApplication(application);
}public void deleteTravelApplication(String id) {travelApplicationMapper.deleteTravelApplication(id);
}public void updateTravelApplication(TravelApplication application) {travelApplicationMapper.updateTravelApplication(application);
}public List<TravelApplication> selectAllTravelApplications() {return travelApplicationMapper.selectAllTravelApplications();
}public List<TravelApplication> selectTravelApplicationsByCondition(TravelApplication application) {return travelApplicationMapper.selectTravelApplicationsByCondition(application);
}

}
5. 控制层(Controller)
在com.example.controller包下创建TravelApplicationController类。
package com.example.controller;

import com.example.entity.TravelApplication;
import com.example.service.TravelApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/travel")
public class TravelApplicationController {
@Autowired
private TravelApplicationService travelApplicationService;

@PostMapping("/apply")
public void insertTravelApplication(@RequestBody TravelApplication application) {travelApplicationService.insertTravelApplication(application);
}@DeleteMapping("/{id}")
public void deleteTravelApplication(@PathVariable("id") String id) {travelApplicationService.deleteTravelApplication(id);
}@PutMapping("/update")
public void updateTravelApplication(@RequestBody TravelApplication application) {travelApplicationService.updateTravelApplication(application);
}@GetMapping("/all")
public List<TravelApplication> selectAllTravelApplications() {return travelApplicationService.selectAllTravelApplications();
}@GetMapping("/condition")
public List<TravelApplication> selectTravelApplicationsByCondition(TravelApplication application) {return travelApplicationService.selectTravelApplicationsByCondition(application);
}

}
6. HTML 页面
在src/main/resources/static目录下创建index.html页面。

Travel Application

Travel Application Management

<div id="result"></div><script>function getAll() {$.ajax({type: "GET",url: "/travel/all",success: function (data) {let resultDiv = $("#result");resultDiv.empty();data.forEach(function (app) {resultDiv.append("<p>ID: " + app.id + ", Name: " + app.name + "</p>");});}});}function getByCondition() {let condition = {name: "John",department: "IT"};$.ajax({type: "GET",url: "/travel/condition",data: condition,success: function (data) {let resultDiv = $("#result");resultDiv.empty();data.forEach(function (app) {resultDiv.append("<p>ID: " + app.id + ", Name: " + app.name + "</p>");});}});}function add() {let application = {id: "20230001",name: "Alice",department: "HR",destination: "New York",departuredate: "2023-01-01",returndate: "2023-01-05",type: "Business Trip",state: "Pending"};$.ajax({type: "POST",url: "/travel/apply",contentType: "application/json",data: JSON.stringify(application),success: function () {alert("Application added successfully");}});}function update() {let application = {id: "20230001",destination: "Los Angeles"};$.ajax({type: "PUT",url: "/travel/update",contentType: "application/json",data: JSON.stringify(application),success: function () {alert("Application updated successfully");}});}function deleteApp() {let id = "20230001";$.ajax({type: "DELETE",url: "/travel/" + id,success: function () {alert("Application deleted successfully");}});}
</script>
7. 配置文件 application.properties(在src/main/resources目录下)配置如下: spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*.xml

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/859490.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用Detr训练自定义数据

前面记录了Detr及其改进Deformable Detr。这一篇记录一下用Detr训练自己的数据集。先看下Detr附录中给出的大体源码,整体非常清晰。接下来记录大体实现过程 一、数据准备 借助labelme对数据进行标注然后将标注数据转换成COCO格式,得到以下几个文件其中JPEGImages存放所有图片…

8086汇编(16位汇编)学习笔记05.asm基础语法和串操作

https://bpsend.net/thread-121-1-2.htmlasm基础语法 1. 环境配置xp环境配置 1.拷贝masm615到指定目录 2.将masm615目录添加进环境变量 3.在cmd中输入ml,可以识别即配置成功dosbox环境配置 1.拷贝masm611到指定目录 2.将masm611所在目录添挂载进dosbox 3.将masm611目录在dosbo…

WinNTSetup 系统安装利器 v5.4.0 单文件版

软件介绍 WinNTSetup,系统安装利器,目前最好用的系统安装器,Windows系统安装部署工具。支持所有Windows平台,支持多系统安装、完全格式化C盘、支持创建VHD虚拟硬盘、在Windows及PE系统下运行,允许在安装前对系统进行预优化设置、集成驱动程序、启用第三方主题支持、加入无…

解决 Cannot GET /favicon.ico

一、报错 二、定位(项目所在文件夹) 三、改名(添加图片,重命名)

Java编程规范-DO / BO / DTO / VO / AO的使用

Java 开发 DO / BO / DTO / VO / AO 的作用 Java 开发中,DO(Data Object)、BO(Business Object)、DTO(Data Transfer Object)、VO(View Object) 和 AO(Application Object) 是常用的对象类型,每种类型都在特定的层次和场景中发挥不同的作用。以下是它们的定义和使用…

硬件开发笔记(三十二):TPS54331电源设计(五):原理图BOM表导出、元器件封装核对

前言一个12V转5V、3.3V和4V的电源电路设计好了,下一步导出BOM表,二次核对元器件型号封装,这是可以生产前的最后一步了。 导出BOM表步骤一:打开原理图打开项目,双击点开原理图:   步骤二:报告-元器件列表列宽一点,板子元器件种类规格不多的时候,导出的东西也不多,因…

数字孪生-智能制造

1、数字企业内循环:打造端到端的数字化应用体验 2、GARTNER分层架构 3、企业数字化架构 4、数字企业的两大核心特征 6、产品数字主线赋能企业转型 7、数字主线关键技术:基于统一架构构建产品全量数字模型 8、闭环数字化解决方案 9、基于数字主线的设计-仿真-试验协同 10、产品数…

C# WPF PrintDialog 打印(3)

前面https://www.cnblogs.com/yinyu5/p/18634080使用PrintDocument方法打印了Canvas,这里打印下面的DataGrid列表内容:这里DataGrid的数据源是DataTable,后台代码:1 private void PrintDocument_DataTable_Method(string Title, DataTable dataTable)2 {3 …

【JAVA代码审计】记一次某java类的cms最最最详细的代码审计

前言 刚好遇到一个授权的渗透是通过该cms实现getshell,所以顺便审计一下java类的cms,这个管理系统是一个内容管理系统,下载地址 https://gitee.com/oufu/ofcms/tree/V1.1.3/tomcat下载地址 https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.78/bin/apache-tomcat-8.5.78-wind…

12.26日每日总结

昨天在调试51单片机的串口时,发现芯片手册上有一句话,在使用定时器1产生串口的波特率时,定时器1就不能使能了。不是不能用,是直接不让使能了,使能后会出错,导致发送的数据不稳定。 今天继续研究了触摸滑条,发现滑条输出的值为从小到大,如下图所示的样子,这就导致从最上…