基于springboot+mybatis+vue的项目实战之增删改查CRUD

目录结构

页面的效果大致如下:

PeotController.java

package com.example.controller;

import com.example.pojo.Peot;
import com.example.pojo.Result;
import com.example.service.PeotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PeotController {

    @Autowired
    private PeotService peotService;

    @RequestMapping("/findAll")
    public List<Peot> findAll(){
     return   peotService.findAll();
    }

    @RequestMapping("/findAllJsoon")
    public Result findAllJson(){
        return  Result.seccess(peotService.findAll()) ;
    }
 @RequestMapping("/deletePeot")
    public void deletePeot(Integer id){
        peotService.deletePeot(id);
    }
@RequestMapping("/peotfindById/{id}")
public Result peotfindById(@PathVariable("id") Integer id) {
    return  Result.seccess(peotService.peotfindById(id));
}

@RequestMapping("/peotupdate")
public  Result updatePeot(@RequestBody Peot peot){
    boolean r = peotService.updatePeot(peot);

    if(r) {
        // 成功  code==1
        return Result.success();
    } else {
        // 失败  code==0
        return Result.erro("更新失败");
    }
}

    @RequestMapping("/peotinsert")
    public Result insertUser(@RequestBody Peot peot){
        boolean result =peotService.insertUser(peot);
        if(result) {
            // 成功  code==1
            return Result.success();
        } else {
            // 失败  code==0
            return Result.erro("添加失败");

        }

    }


}

PeotMapper.java

package com.example.mapper;

import com.example.pojo.Peot;
import com.example.pojo.Result;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PeotMapper {

    @Select("select * from peom")
    public List<Peot> findAll();

    @Delete("delete from peom where id=#{id}")
    public int deletePeot(Integer id);

    @Select("select * from peom where id=#{id}")
    public Peot peotfindById(Integer ID);

    @Update("update peom set author=#{author},gender=#{gender},dynasty=#{dynasty},title=#{title} ,style=#{style} where id=#{id} ")
    public  boolean updatePeot(Peot peot);

    @Insert("insert into peom(author, gender, dynasty, title, style) values (#{author}, #{gender}, #{dynasty}, #{title}, #{style})")
    public int insert(Peot peot);

}

Peot.java

package com.example.pojo;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Peot {
    private Integer id;
    private String author;
    private String gender;
    private String dynasty;
    private String title;
    private String style;
}

Result.java

package com.example.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    private Integer code;//响应码,1 代表成功; 0 代表失败
    private String msg;  //响应信息 描述字符串
    private Object data; //返回的数据


    public static Result success(){
        return new Result(1,"success",null);
    }

    public static Result seccess(Object data){
        return new Result(1,"success",data);
    }

    public static Result erro(String str){
        return new Result(1,str,null);
    }


}

PeotService.java

package com.example.service;

import com.example.mapper.PeotMapper;
import com.example.pojo.Peot;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public interface PeotService {

public List<Peot> findAll();

public int deletePeot(Integer id);

public Peot peotfindById(Integer id);

public boolean updatePeot(Peot peot);

    public   boolean  insertUser(Peot peot);

}

PeotServiceImpl.java

package com.example.service.impl;

import com.example.mapper.PeotMapper;
import com.example.pojo.Peot;
import com.example.service.PeotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PeotServiceImpl implements PeotService {

    @Autowired
    private PeotMapper peotMapper;

    @Override
    public List<Peot> findAll() {
        return peotMapper.findAll();
    }

    @Override
    public int deletePeot(Integer id) {
        return peotMapper.deletePeot(id);
    }
    @Override
    public Peot peotfindById(Integer id) {
        return peotMapper.peotfindById(id);
    }

    @Override
    public boolean updatePeot(Peot peot) {
        return peotMapper.updatePeot(peot);
    }

    @Override
    public boolean  insertUser(Peot peot) {
        int result =  peotMapper.insert(peot);
        return result == 1;
    }

}

peot_findall.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios-0.18.0.js"></script>

</head>
<body>
<h1>诗人信息列表</h1>
<div id="app" align="center">
    <a href="peot_insert.html">新增</a>
<table  border="1">
    <tr>
    <th>id</th>
    <th>author</th>
    <th>gender</th>
    <th>dynasty</th>
    <th>title</th>
    <th>style</th>
    <th>操作</th>
    </tr>
    <tr v-for="peot in peotList">
        <td>{{peot.id}}</td>
        <td>{{peot.author}}</td>
        <td>{{peot.gender}}</td>
        <td>{{peot.dynasty}}</td>
        <td>{{peot.title}}</td>
        <td>{{peot.style}}</td>
        <td>
            <button type="button" @click="deleteId(peot.id)">删除</button>
            <a :href="'peot_edit.html?id='+peot.id">修改</a>
        </td>
    </tr>

</table>
</div>

</body>

<script>
    new Vue({
        el:"#app",
        data() {
            return {
                peotList:[]
            }
        },
        mounted(){
        axios.get('/findAllJsoon').then(res=>{
            if(res.data.code){
                this.peotList = res.data.data;
            }
            }
        )},
        methods:{
            findAll:function () {
                var _this = this;
                axios.post('/findAllJsoon', {
                })
                    .then(function (response) {
                        _this.peotList = response.data.data;//响应数据给tableData赋值
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            deleteId:function (id) {
                var _thisd = this;
                if (window.confirm("确定要删除该条数据吗???")){
                    axios.post('/deletePeot?id='+id)
                        .then(function (response) {
                            alert("删除成功")
                            _thisd.findAll();
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            }

        }
    })


</script>

</html>

peot_insert.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <script src="./js/vue.js"></script>
  <script src="./js/axios-0.18.0.js"></script>

</head>
<body>
<div id="app">
    <table border="1">

        <tr>
            <td>姓名</td>
            <td><input type="text" v-model="peot.author"> </td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="gender" v-model="peot.gender" value="1"> 男
                <input type="radio" name="gender" v-model="peot.gender" value="0"> 女
            </td>
        </tr>
        <tr>
            <td>朝代</td>
            <td><input type="text" v-model="peot.dynasty"> </td>
        </tr>
        <tr>
            <td>头衔</td>
            <td><input type="text" v-model="peot.title"> </td>
        </tr>
        <tr>
            <td>风格</td>
            <td><input type="text" v-model="peot.style"> </td>
        </tr>

        <tr>
            <td></td>
            <td><input type="button" @click="addPeot" value="增加"> </td>

        </tr>
    </table>

</div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {
            peot: {
                "author":"",
                "gender":"",
                "dynasty":"",
                "title":"",
                "style":""
            }        //详情

        },
        methods: {

            addPeot() {
                var url = 'peotinsert'
                axios.post(url,this.peot)
                    .then(res => {
                        var baseResult = res.data
                        if(baseResult.code == 1) {
                            // 成功
                            location.href = 'peot_findall.html'
                        } else {
                            // 失败
                            alert(baseResult.message)
                        }
                    })
                    .catch(err => {
                        console.error(err);
                    })
            }
        },

    })
</script>

</html>

peot_edit.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios-0.18.0.js"></script>

</head>
<body>
<div id="app">
    <table border="1">
        <tr>
            <td>编号</td>
            <td><input type="text" v-model="this.id"> </td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" v-model="peot.author"> </td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <input type="radio" name="gender" v-model="peot.gender" value="1"> 男
                <input type="radio" name="gender" v-model="peot.gender" value="0"> 女
            </td>
        </tr>
        <tr>
            <td>朝代</td>
            <td><input type="text" v-model="peot.dynasty"> </td>
        </tr>
        <tr>
            <td>头衔</td>
            <td><input type="text" v-model="peot.title"> </td>
        </tr>
        <tr>
            <td>风格</td>
            <td><input type="text" v-model="peot.style"> </td>
        </tr>

        <tr>
            <td></td>
            <td><input type="button" @click="updatePeot" value="更新"> </td>

        </tr>
    </table>
    {{peot}}
</div>


</body>
<script>
    new Vue({
        el: '#app',
        data: {
            id: '',
            peot: {},        //详情
        },
        methods: {
            selectById() {
                //${this.id}
                var url = `peotfindById/${this.id}`  //注意这里是反引号
                //反引号(backticks,也称为模板字符串或模板字面量)是ES6(ECMAScript 2015)中引入的一种新字符串字面量功能,
                // 它允许您在字符串中嵌入表达式。反引号用`(键盘上通常位于Tab键上方)来界定字符串的开始和结束。
                axios.get(url)
                    .then(response => {
                        var baseResult = response.data
                        if(baseResult.code == 1) {
                            this.peot = baseResult.data
                        }
                    })
                    .catch( error => {})
            },
            updatePeot() {
                var url = 'peotupdate'
                axios.put(url,this.peot)
                    .then(res => {
                        var baseResult = res.data
                        if(baseResult.code == 1) {
                            // 成功
                            location.href = 'peot_findall.html'
                        } else {
                            // 失败
                            alert(baseResult.message)
                        }
                    })
                    .catch(err => {
                        console.error(err);
                    })
            },


        },
        created() {
            // 获得参数id值
            this.id = location.href.split("?id=")[1]
            // 通过id查询详情
            this.selectById()
        },

    })



</script>

</html>

application.properties  

修改为自己的数据库,以及数据库连接的账号密码。

# 应用服务 WEB 访问端口
server.port=8080
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.example.mybatis.entity

#数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/heima
spring.datasource.username=root
spring.datasource.password=123
#开启mybatis的日志输出
mybatis.configuration.logimpl=org.apache.ibatis.logging.stdout.StdOutImpl
#开启数据库表字段
#实体类属性的驼峰映射
mybatis.configuration.map-underscore-to-camel-case=true

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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,可以快速搭建一个稳定高效的系统,实现业务需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值