vue3+koa2+mysql增删改查

vue3 

App.vue:

<template>
  <el-button type="primary" @click="openAddDialog">新增</el-button>

  <el-input
    v-model="state.searchByUsername"
    clearable
    placeholder="输入用户名称搜索"
    style="width: 400px; margin: 0 10px"
  />
  <el-button type="success" @click="searchUser">搜索</el-button>
  <el-table :data="state.tableData" style="width: 100%">
    <el-table-column
      label="用户名"
      prop="username"
      width="180"
    ></el-table-column>
    <el-table-column label="密码" width="180">
      <template #default="scope">
        <el-tag>{
  { scope.row.password }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button
        >
        <el-button
          size="small"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 弹窗 -->
  <el-dialog
    :destroy-on-close="true"
    v-model="state_dialog.isShow"
    :title="state_dialog.type == 'add' ? '新增' : '编辑'"
  >
    <el-form
      ref="ref_form"
      :model="state_dialog.form"
      :rules="state_dialog.rules"
    >
      <el-form-item label="用户名" label-width="140px" prop="username">
        <el-input
          v-model="state_dialog.form.username"
          clearable
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="密码" label-width="140px" prop="password">
        <el-input
          v-model.number="state_dialog.form.password"
          clearable
          autocomplete="off"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="state_dialog.isShow = false">取消</el-button>
        <el-button type="primary" @click="submitForm(ref_form)">确定</el-button>
        <el-button type="primary" @click="resetForm(ref_form)">重置</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script  setup>
import { onMounted, reactive, ref } from "vue";
import axios from "axios";
const ref_form = ref(null);
const state = reactive({
  searchByUsername: "",
  ta
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一个典型的前后端分离的例子,我可以给您提供一个简单的示例代码,来帮助您理解如何使用 Vue3 + Vite + Ajax + SpringBoot 实现增删功能。 前端使用 Vue3 + Vite + Ajax 实现: 1. 在 Vue3 中,使用 `axios` 或者 `fetch` 进行 AJAX 请求,请求后端 SpringBoot 服务器,获取数据或者提交数据。 2. 在 Vue3 中,使用 `vue-router` 进行 SPA 单页应用的路由控制,实现前端页面的跳转。 3. 在 Vue3 中,使用 `vuex` 进行状态管理,方便组件之间的数据共享和传递。 下面是一个简单的 Vue3 组件的示例代码: ```vue <template> <div> <h1>用户列表</h1> <table> <thead> <tr> <th>ID</th> <th>用户名</th> <th>邮箱</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="user in users" :key="user.id"> <td>{{ user.id }}</td> <td>{{ user.username }}</td> <td>{{ user.email }}</td> <td> <button @click="editUser(user)">编辑</button> <button @click="deleteUser(user.id)">删除</button> </td> </tr> </tbody> </table> </div> </template> <script> import axios from 'axios'; export default { name: 'UserList', data() { return { users: [], }; }, methods: { async getUsers() { const response = await axios.get('/api/users'); this.users = response.data; }, async deleteUser(id) { await axios.delete(`/api/users/${id}`); this.getUsers(); }, editUser(user) { // 跳转到编辑用户页面,使用 vue-router this.$router.push(`/users/${user.id}`); }, }, mounted() { this.getUsers(); }, }; </script> ``` 后端使用 SpringBoot 实现: 1. 在 SpringBoot 中,使用 `@RestController` 和 `@RequestMapping` 注解定义 RESTful API 接口,实现增删的逻辑。 2. 在 SpringBoot 中,使用 `@Service` 和 `@Repository` 注解定义服务层和数据访问层,实现业务逻辑和数据库操作。 3. 在 SpringBoot 中,使用 `Spring Data JPA` 或者 `MyBatis` 等框架进行数据库操作,简化代码编写。 下面是一个简单的 SpringBoot Controller 的示例代码: ```java @RestController @RequestMapping("/api/users") public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping public List<User> listUsers() { return userService.listUsers(); } @PostMapping public void createUser(@RequestBody User user) { userService.createUser(user); } @PutMapping("/{id}") public void updateUser(@PathVariable Long id, @RequestBody User user) { userService.updateUser(id, user); } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userService.deleteUser(id); } } ``` 这是一个简单的增删的示例代码,您可以根据自己的需要进行修和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值