【web】第七次作业:基于springboot+mybatis+vue的项目实战之增删改查CRUD

网页增删改查

整体页面

在这里插入图片描述

增加

在这里插入图片描述
点击提交
在这里插入图片描述
诗人信息界面
在这里插入图片描述

删除

在这里插入图片描述
删除后
在这里插入图片描述

修改

在这里插入图片描述
在这里插入图片描述
修改后
在这里插入图片描述

代码

在这里插入图片描述

controller

package com.wust.controller;

import com.wust.pojo.Poet;
import com.wust.pojo.Result;
import com.wust.service.serviceImpl.PoetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class PoetController {
    @Autowired
    private PoetService poetService;

    //查询
    @GetMapping("/poets")
    public Result select(){
        return Result.success(poetService.selectData());
    }

    //新增
    @RequestMapping("/poet")
    public Result insert(@RequestBody Poet poet){
        poetService.insert(poet);
        return Result.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        poetService.delete(id);
        return Result.success();
    }

    //修改  先查询,再修改
    @RequestMapping("/poets/{id}")
    public Result getById(@PathVariable Integer id){
        Poet poet = poetService.getById(id);
        return Result.success(poet);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Poet poet){
        poetService.update(poet);
        return Result.success();
    }
}

mapper

package com.wust.mapper;

import com.wust.pojo.Poet;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PoetMapper {
    @Select("select p_id as id, name, gender, dynasty, title, style from poet")
    List<Poet> selectPoet();

    @Insert("insert into poet (name, gender, dynasty, title, style) " +
            "values (#{name},#{gender},#{dynasty},#{title},#{style})")
    void insert(Poet poet);

    @Delete("delete from poet where p_id=#{id}")
    void delete(Integer id);

    @Select("select p_id as id, name, gender, dynasty, title, style from poet where p_id=#{id}")
    Poet getById(Integer id);

    @Update("update poet set " +
            "name=#{name},gender=#{gender},dynasty=#{dynasty},title=#{title},style=#{style} where p_id=#{id}")
    void update(Poet poet);
}

pojo

package com.wust.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Poet {
    private Integer id;   //id(主键)
    private String name;  //姓名
    private Integer gender; //性别
    private String dynasty; //朝代
    private String title; //头衔
    private String style; //风格
}
package com.wust.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {

    Integer code;
    String msg;
    Object data;

    public static Result success(){
        return new Result(1,"success",null);
    }
    public static Result success(Object data){
        return new Result(1,"success",data);
    }
    public static Result error(String msg){
        return new Result(0,msg,null);
    }
}

service

package com.wust.service.serviceImpl;


import com.wust.pojo.Poet;

import java.util.List;

public interface PoetService {
    List<Poet> selectData();

    void insert(Poet poet);

    void delete(Integer id);

    Poet getById(Integer id);

    void update(Poet poet);
}
package com.wust.service;

import com.wust.mapper.PoetMapper;
import com.wust.pojo.Poet;
import com.wust.service.serviceImpl.PoetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PoetService1 implements PoetService {
    @Autowired
    private PoetMapper poetMapper;
    public List<Poet> selectData(){
        return poetMapper.selectPoet();
    }

    public void insert(Poet poet){
        poetMapper.insert(poet);
    }


    public void delete(Integer id){
        poetMapper.delete(id);
    }


    public Poet getById(Integer id){
        return poetMapper.getById(id);
    }

    public void update(Poet poet){
        poetMapper.update(poet);
    }

}

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>诗人信息</title>
</head>

<script src="./js/vue.js"></script>
<script src="./js/axios-0.18.0.js"></script>

<body>

<h1 align="center">诗人信息列表展示</h1>
<div id="app" align="center">


  <table border="1" cellspacing="0" width="60%">
    <tr>
      <th>序号</th>
      <th>姓名</th>
      <th>性别</th>
      <th>朝代</th>
      <th>头衔</th>
      <th>风格</th>
      <th>操作  &nbsp;&nbsp;
        <button type="button" @click="insertPoet()">添加</button>
      </th>
    </tr>
    <tr align="center" v-for="poet in tableData">
      <td>{{poet.id}}</td>
      <td>{{poet.name}}</td>
      <td>{{poet.gender}}</td>
      <td>{{poet.dynasty}}</td>
      <td>{{poet.title}}</td>
      <td>{{poet.style}}</td>
      <td class="text-center">
<!--        删除操作:delete/id
            修改操作:poets/id   +  update -->
        <button type="button" @click="editPoet(poet.id)">修改</button>
        <button type="button" @click="deletePoet(poet.id)">删除</button>
      </td>
    </tr>
  </table>
</div>

</body>

<script>
  new Vue({
    el: "#app",
    data() {
      return {
        tableData: []
      }
    },

    methods: {
      // 删除操作
      deletePoet(id) {
        // 弹出确认对话框
        if (confirm('确定要删除这个诗人的信息吗?')) {
          // 如果用户点击了确定按钮,执行删除操作
          axios.delete('delete/' + id).then(res => {
            if (res.data.code) {
              alert('删除成功');
              this.loadData();
            } else {
              alert('删除失败');
            }
          });
        }
      },

      // 编辑操作
      editPoet(id) {
        // 跳转到编辑页面并传递诗人ID
        window.location.href = "poetview.html?id=" + id;
      },

      // 新增操作
      insertPoet(){
        // 跳转到新增页面上
        window.location.href = "poetinsert.html";
      },



      // 实时数据更新
      loadData() {
        axios.get('poets').then(res => {
          this.tableData = res.data.data;
          // 遍历tableData数组,处理性别字段
          for (let poet of this.tableData) {
            if (poet.gender === 1) {
              poet.gender = "男";
            } else if (poet.gender === 2) {
              poet.gender = "女";
            } else {
              poet.gender = "不明确";
            }
          }
        });
      }
    },


    mounted() {
        this.loadData();
    }
  });

</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增诗人信息</title>
</head>

<script src="./js/vue.js"></script>
<script src="./js/axios-0.18.0.js"></script>

<body>

<h1 align="center">新增诗人信息</h1>
<div id="insertApp" align="center">
    <form>
        <label for="name">姓名:</label>
        <input type="text" id="name" v-model="newForm.name"><br><br>

        <label for="gender">性别:</label>
        <input type="radio" id="male" value="1" v-model="newForm.gender"><input type="radio" id="female" value="2" v-model="newForm.gender"><br><br>

        <label for="dynasty">朝代:</label>
        <input type="text" id="dynasty" v-model="newForm.dynasty"><br><br>

        <label for="title">头衔:</label>
        <input type="text" id="title" v-model="newForm.title"><br><br>

        <label for="style">风格:</label>
        <input type="text" id="style" v-model="newForm.style"><br><br>

        <button type="button"  @click.prevent="insertPoet">提交</button>
    </form>
</div>

</body>

<script>
    new Vue({
        el: "#insertApp",
        data() {
            return {
                newForm: {
                    name: "",
                    gender: "",
                    dynasty: "",
                    title: "",
                    style: ""
                }
            }
        },

        methods: {
            insertPoet() {
                // 数据上传
                axios.post('/poet', this.newForm).then(res => {
                    if (res.data.code) {
                        alert('数据添加成功');
                        // 添加成功后跳转到诗人信息列表页面
                        window.location.href = "poet.html";
                    } else {
                        alert('添加失败');
                    }
                });
            }
        }
    });
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑诗人信息</title>
</head>

<script src="./js/vue.js"></script>
<script src="./js/axios-0.18.0.js"></script>

<body>

<h1 align="center">编辑诗人信息</h1>
<div id="editApp" align="center">
    <form>
        <label for="name">姓名:</label>
        <input type="text" id="name" v-model="editForm.name"><br><br>

        <label for="gender">性别:</label>
        <input type="radio" id="male" value="1" v-model="editForm.gender"><input type="radio" id="female" value="2" v-model="editForm.gender"><br><br>

        <label for="dynasty">朝代:</label>
        <input type="text" id="dynasty" v-model="editForm.dynasty"><br><br>

        <label for="title">头衔:</label>
        <input type="text" id="title" v-model="editForm.title"><br><br>

        <label for="style">风格:</label>
        <input type="text" id="style" v-model="editForm.style"><br><br>

        <button type="button" @click.prevent="updatePoet">提交</button>
    </form>
</div>

</body>

<script>
    new Vue({
        el: "#editApp",
        data() {
            return {
                editForm: {
                    id: "",
                    name: "",
                    gender: "",
                    dynasty: "",
                    title: "",
                    style: ""
                }
            }
        },

        methods: {
            updatePoet() {
                // 数据上传
                axios.put('update', this.editForm).then(res => {
                    if (res.data.code) {
                        alert('数据修改成功');
                        // 修改成功后跳转到诗人信息列表页面
                        window.location.href = "poet.html";
                    } else {
                        alert('修改失败');
                    }
                });
            },

            // 根据诗人ID获取当前信息
            getPoetInfo(id) {
                axios.get('poets/' + id).then(res => {
                    if (res.data.code) {
                        this.editForm = res.data.data;
                    } else {
                        alert('获取诗人信息失败');
                    }
                });
            }

        },

        mounted() {
            var poetId = window.location.search.split('=')[1];
            this.getPoetInfo(poetId);
        },

    });

</script>
</html>
  • 28
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Spring Boot 是一个用于创建和部署独立的、基于生产级别的Spring应用程序的框架。它简化了以往使用Spring框架所需的大量配置,并提供了自动化的依赖管理和容器配置。 MyBatis 是一个优秀的持久化框架,它可以简化数据库操作并提供了强大的SQL语句管理和映射功能。通过MyBatis,我们可以使用简洁的XML或注解来描述数据库映射关系,实现与数据库的交互。 Vue 是一套用于构建用户界面的渐进式JavaScript框架。它拥有响应式数据绑定和组件化的架构,可以帮助我们更快速、高效地开发交互式的前端应用程序。 一个基于Spring BootMyBatisVue的系统实现可以如下进行: 1. 在Spring Boot中,我们可以使用Spring Initializr快速生成一个基础项目,添加Spring BootMyBatis相关的依赖项。 2. 创建Java实体类,用于映射数据库表结构。可以使用注解或XML配置文件定义实体类的属性和数据库字段的映射关系。 3. 编写MyBatis的映射文件或注解,实现数据库操作的CRUD功能。可以使用MyBatis的动态SQL语句,根据实际需要灵活构建查询条件。 4. 在Spring Boot中配置数据源和MyBatis相关的属性,使其能够正确地连接和操作数据库。 5. 创建Spring Boot的控制器,处理前端请求并调用MyBatis的相应方法进行数据库操作。可以使用@RestController注解定义RESTful API接口。 6. 在Vue中创建组件,用于展示和接收用户的界面操作。可以使用Vue数据绑定和组件化特性,实现页面的动态更新和交互。 7. 使用Vue的路由功能,实现前端页面的导航和页面切换。可以通过定义不同的路由规则,让用户能够在不同的页面间进行导航。 8. 发布项目时,使用Spring Boot提供的打包工具将系统打包为可执行的JAR文件,并部署到服务器上。 通过以上步骤,我们可以基于Spring BootMyBatisVue实现一个完整的系统。Spring Boot负责处理后端的业务逻辑和数据库操作,MyBatis负责与数据库进行交互,Vue负责构建交互式的前端界面。整体架构简洁清晰,开发效率提高,系统性能良好。 ### 回答2: SpringBoot是一个易于上手的Java开发框架,可以快速搭建稳定高效的Web应用程序。它提供了自动化配置和默认约定来简化开发过程,并集成了许多常用的第三方库和工具,如MyBatisMyBatis是一种流行的持久层框架,用于将Java对象和关系数据库之间进行映射。它通过提供一个简单的、方便的方式来执行SQL查询和更新,从而实现了数据的持久化。在SpringBoot中使用MyBatis,可以通过注解或XML文件来定义数据库操作,并结合MyBatis的动态SQL功能,实现灵活的数据库访问。 Vue是一个轻量级的JavaScript框架,用于构建用户界面。它采用组件化的开发模式,将页面拆分成多个可重用的组件,使得前端开发更加高效和模块化。Vue还提供了响应式的数据绑定和虚拟DOM技术,可以快速地构建交互式的单页应用。 在一个系统中,可以使用SpringBoot + MyBatis + Vue的组合来完成各个层面的功能。SpringBoot作为后端框架,负责处理业务逻辑,提供RESTful API接口,并通过MyBatis数据库进行交互。MyBatis则负责将Java对象和数据库之间进行映射,执行SQL查询和更新操作。 而Vue作为前端框架,负责渲染页面、处理用户交互,并通过调用后端提供的API接口获取和提交数据Vue通过组件化的方式来构建页面,每个组件负责渲染一个部分,最终组合成完整的页面。 在实现过程中,可以使用Vue的路由功能来实现前端页面的导航和跳转,通过axios等网络请求库与后端进行数据交互。同时,可以利用SpringBoot的自动化配置和注解功能来简化后端开发,提高开发效率。通过整合SpringBootMyBatisVue,可以快速搭建一个稳定高效的系统,实现业务需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值