bootstrap+springboot增删改查分页

本文介绍了一个基于Spring Boot的商品管理系统实现过程,包括前后端交互、数据分页查询、增删改查等功能,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:pom.xml中加入依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2:application.properties里面加入

spring.thymeleaf.prefix=classpath:/templates/
spring.resources.static-locations=classpath:/,classpath:/resources/,classpath:/static/

3:productList.html

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>列表展示页面</title>
</head>
<!--引入index.html,index.html里面全是引入的js-->
<div th:include="index :: header" ></div>
<style type="text/css">
    body{
        margin-top: 60px;
    }
</style>
<body>
<div  class="container-fluid">
<div id="toolbar">
    <form class="form-inline">

        <table>
            <tr>
                <td>
                    <button type="button" onclick="toAddProduct()" class="btn btn-success glyphicon glyphicon-plus">添加</button>
                </td>
                <td>
                    <button type="button" onclick="deleteProduct()"  class="btn btn-danger glyphicon glyphicon-trash">删除</button>
                </td>
                <td>名称</td>
                <td>
                    <input type="text" id="searchTitle" class="form-control"/></td>
                <td>
                <td>描述</td>
                <td>
                    <input type="text" id="searchDescription" class="form-control"/></td>
                <td>
                    <div class="text-center">
                        <button type="button" onclick="searchProduct()" class="btn btn-info glyphicon glyphicon-search">搜索</button>
                        <button type="button" onclick="cleanSearch()" class="btn btn-info glyphicon glyphicon-search">清空</button>   
                    </div>
                </td>

            </tr>
        </table>
    </form>
</div>

<table class="table" id="productTable"></table>
</div>
</body>
<script th:src="@{../product/showProduct.js}"></script>
</html>

4:product.js

//初始化
$(function(){
    findAllProductList();
})
//条件查询
function searchProduct(){
    var searchTitle = $("#searchTitle").val();
    var searchDescription = $("#searchDescription").val();
    $('#productTable').bootstrapTable('refresh',{
        query:{
            title:searchTitle,
            description:searchDescription
        }
    })
}


//清除条件查询
function  cleanSearch() {
    $("#searchTitle").val("");
    $("#searchDescription").val("");
    $("#productTable").bootstrapTable("refresh");
    console.log('暂未获取到用户数据')
}



//添加
function toAddProduct(){
    var dialog = bootbox.dialog({
        title: '添加商品',
        message: createAddContent("../page/toAddProduct"),
        size: 'large',
        buttons: {
            cancel: {
                label: "取消",
                className: 'btn-danger',
                callback: function(){}
            },
            ok: {
                label: "保存",
                className: 'btn-info',
                callback: function(){
                    saveProduct();
                }
            }
        }
    });

}
var res;
function createAddContent(url){
    $.ajax({
        url:url,
        async:false,
        success:function(data){
            res = data;
        }
    });
    return res;
}
function saveProduct(){
    $.ajax({
        url:"../commodity/saveProduct",
        type:"post",
        data:$("#addForm").serialize(),
        dataType:"json",
        success:function(data){
            if(data){
                $('#productTable').bootstrapTable('refresh');
            }else{
                bootbox.alert({
                    size:'small',
                    title:'提示',
                    message:'保存失败'
                })
            }
        }
    })
}


//删除
deleteProduct=function(){
    var row= $("#productTable").bootstrapTable('getSelections');
    if(row.length<1){
        bootbox.alert({
            size: "small",
            title: "提示",
            message: "请至少选择一条!",
            buttons: {
                ok: {
                    label: '确定',
                    className: 'btn-success'
                }
            },
            callback: function(){}
        })
    }else{
        var ids="";
        for (var i = 0; i < row.length; i++) {
            ids==""?ids+=row[i].productid:ids+=","+row[i].productid;
        }
        bootbox.alert({
            size: "small",
            title: "提示",
            message: "是否确认删除id为"+ids+"的数据吗!",
            buttons: {
                ok: {
                    label: '确定',
                    className: 'btn-success'
                }
            },
            callback: function(){
                if(true){
                    $.ajax({
                        url:"../commodity/deleteProductByIdAndIds",
                        data:{ids:ids},
                        success:function(){
                            alert("删除成功");
                            $("#productTable").bootstrapTable("refresh");
                        }
                    })
                }
            }
        })
    }
}


//列表
findAllProductList=function(){
    $("#productTable").bootstrapTable({
        toolbar:'#toolbar',
        url:'../commodity/findAllProductList',	//获取数据地址
        //toolbar:'#toolbar', //条查的工具栏
        method:'post',
        contentType:'application/x-www-form-urlencoded',//post请求按照表单提交,提交参数到后台
        pagination:true,//是否展示分页
        pageList:[2,4,6,8,10],
        pageNumber:1,
        pageSize:2,
        sidePagination:'server',
        clickToSelect: true, //是否启用点击选中行
        striped:true,//是否显示斑马线
        queryParams:function(){
            return {
                page: this.pageNumber,
                rows: this.pageSize,
                title:$("#searchTitle").val(),
                description : $("#searchDescription").val()
            };
        },
        columns:[
            {checkbox:true},
            {field:'productid',title:'商品ID',align:'center'},
            {field:'title',title:'商品名称',align:'center'},
            {field:'description',title:'商品描述',align:'center'},
            {field:'num',title:'库存',align:'center'},
            {field:'price',title:'价格',align:'center'},
            {field:'imgid',title:'图片',align:'center'},
            {field:'issale',title:'是否促销',align:'center',formatter:function(value,row,index){return value==1?"促销":"高价";}},
            {field:'saleprice',title:'促销价格',align:'center'},
            {field:'ishot',title:'是否热卖',align:'center',formatter:function(value,row,index){return value==1?"热卖":"卖不动";}},
            {field:'ison',title:'是否上架',align:'center',formatter:function(value,row,index){return value==1?"上架":"下架";}},
            {field:'istui',title:'是否推荐',align:'center',formatter:function(value,row,index){return value==1?"推荐":"不推荐";}},
            {field:'createtime',title:'创建时间',align:'center'},
            {field:'storename',title:'店铺名称',align:'center'},
            {field:'cz',title:'操作',align:'center',formatter:function(value,row,index){
                    return '<a href="javascript:findInfo('+row.productid+');">查看</a>|<a href="javascript:toEditProduct('+row.productid+');">编辑</a>'
            }}

        ]

    })

}


//查看详情
function findInfo(productid) {
    var dialog = bootbox.dialog({
        title: '查看详情',
        message: createAddContent('../page/toProductInfo'),
        size: 'large',
        buttons: {
            cancel: {
                label: "关闭",
                className: 'btn-danger',
                callback: function () {
                }
            }
        }
    });
    $.ajax({
        url:'../commodity/findProductInfoById',
        typr:'post',
        data:{
            productid:productid
        },
        dataType:'json',
        success:function(data){
            $("#productid").val(data.productid);
            $("#title").val(data.title);
            $("#description").val(data.description);
            $("#num").val(data.num);
            $("#price").val(data.price);
            $("#saleprice").val(data.saleprice);
            $("#issale").val(data.issale==1?"促销":"高价");
            $("#ishot").val(data.ishot==1?"热卖":"卖不动");
            $("#ison").val(data.ison==1?"上架":"下架");
            $("#istui").val(data.istui==1?"推荐":"不推荐");
            $("#storename").val(data.storename);
        }
    })
}



//修改编辑按钮
function toEditProduct(productid){
    var dialog = bootbox.dialog({
        title: '修改会员',
        message: createAddContent('../page/toEditProduct'),
        size: 'large',
        buttons: {
            cancel: {
                label: "取消!",
                className: 'btn-danger',
                callback: function(){
                }
            },
            ok: {
                label: "保存!",
                className: 'btn-info',
                callback: function(){
                    updateProduct();
                }
            }
        }
    });

    $.ajax({
        url:'../commodity/findProductInfoById',
        typr:'post',
        data:{
            productid:productid
        },
        dataType:'json',
        success:function(data){
            $("#productid").val(data.productid);
            $("#title").val(data.title);
            $("#description").val(data.description);
            $("#num").val(data.num);
            $("#price").val(data.price);
            $("#saleprice").val(data.saleprice);
            $("#issale").val(data.issale);
            $("#ishot").val(data.ishot);
            $("#ison").val(data.ison);
            $("#istui").val(data.istui);
            $("#storename").val(data.storename);
        }
    })
}

function updateProduct(){
    $.ajax({
        url:"../commodity/updateProduct",
        type:"post",
        data:$("#updateForm").serialize(),
        dataType:"json",
        success:function(data){
            if(data){
                $('#productTable').bootstrapTable('refresh');
            }else{
                bootbox.alert({
                    size:'small',
                    title:'提示',
                    message:'保存失败'
                })
            }
        }
    })
}

}

5:ProductController

package com.limy.controller;


import com.limy.model.Product;
import com.limy.service.CommodityServiceLmy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@Controller
@RequestMapping("commodity")
public class CommodityController {
    @Autowired
    private CommodityServiceLmy commodityServiceLmy;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //商品管理--分页列表---limy
    @RequestMapping("findAllProductList")
    @ResponseBody
    public HashMap<String,Object> findAllProductList(Integer page, Integer rows, Product product){
        HashMap<String, Object> productlist = commodityServiceLmy.findAllProductList(page,rows,product);
        return productlist;
    }


    //商品管理--批量删除---limy
    @RequestMapping("deleteProductByIdAndIds")
    @ResponseBody
    public boolean deleteProductByIdAndIds(Integer[] ids) {
        try {
            commodityServiceLmy.deleteProductByIdAndIds(ids);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
    //商品管理---添加---limy
    @RequestMapping("saveProduct")
    @ResponseBody
    public boolean saveProduct(Product product){
        if(null!=product.getTitle()&&!"".equals(product.getTitle())){
            try {
                String uuid = getUUID();
                product.setProductid(Integer.valueOf(uuid));
                product.setCreatetime(dateFormat.format(new Date()));
                commodityServiceLmy.saveProduct(product);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }else{
            return false;
        }
    }

    //商品管理---查看单条详情---limy
    @RequestMapping("findProductInfoById")
    @ResponseBody
    public Product findProductById(Integer productid){
        Product product = commodityServiceLmy.findProductById(productid);
        return product;
    }

    //商品管理---修改保存--limy
    @RequestMapping("updateProduct")
    @ResponseBody
    public boolean updateProduct(Product product){
        if(null!=product.getProductid()){
            try {
                product.setCreatetime(dateFormat.format(new Date()));
                commodityServiceLmy.updateProduct(product);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }else{
            return false;
        }
    }

    public static String getUUID() {
        String id =null;
        UUID uuid = UUID.randomUUID();
        id = uuid.toString();
        //去掉随机ID的短横线
        id = id.replace("-", "");
        //将随机ID换成数字
        int num = id.hashCode();
        //去绝对值
        num = num < 0 ? -num : num;
        id = String.valueOf(num);
        return id;
    }


}

6:service

package com.limy.service;


import com.limy.model.Product;

import java.util.HashMap;
import java.util.List;

public interface CommodityServiceLmy {
    //列表
    HashMap<String, Object> findAllProductList(Integer page, Integer rows, Product product);

    Product findProductById(Integer productid);
    //删除
    void deleteProductByIdAndIds(Integer[] ids);

    void saveProduct(Product product);

    void updateProduct(Product product);

    List<Product> fandAll();
}

7:serviceImpl

package com.limy.serviceImpl;

import com.limy.mapper.CommodityMapper;
import com.limy.model.Product;
import com.limy.service.CommodityServiceLmy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityServiceLmy {
    @Autowired
    private CommodityMapper commodityMapper;


    //列表
    @Override
    public HashMap<String, Object> findAllProductList(Integer page, Integer rows, Product product) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("product", product);
        int count = commodityMapper.findProductCount(params);
        Integer start = (page - 1) * rows;
        params.put("startIndex",start);
        params.put("endIndex",rows);
        List<Product> productList= commodityMapper.findProductList(params);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", count);
        hashMap.put("rows", productList);
        return hashMap;
    }

    @Override
    public Product findProductById(Integer productid) {
        return commodityMapper.findProductById(productid);
    }

    //删除
    @Override
    public void deleteProductByIdAndIds(Integer[] ids) {
        commodityMapper.deleteProductByIdAndIds(ids);
    }
    //添加
    @Override
    public void saveProduct(Product product) {
        commodityMapper.saveProduct(product);
    }

    @Override
    public void updateProduct(Product product) {
        commodityMapper.updateProduct(product);
    }

    @Override
    public List<Product> fandAll() {
        return commodityMapper.fandAll();
    }

}

8:mapper

package com.limy.mapper;

import com.limy.model.Product;

import java.util.HashMap;
import java.util.List;

public interface CommodityMapper {

    Product findProductById(Integer productid);

    int findProductCount(HashMap<String, Object> params);

    List<Product> findProductList(HashMap<String, Object> params);

    void deleteProductByIdAndIds(Integer[] ids);

    void saveProduct(Product product);

    void updateProduct(Product product);

    List<Product> fandAll();
}

9:product.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.limy.mapper.CommodityMapper">



    <select id="findProductCount" parameterType="map" resultType="int">
        select count(1) from shop_product t
        <include refid="tiaocha"></include>
    </select>
    <select id="findProductList" parameterType="map" resultType="com.limy.model.Product">
        select * from shop_product t
        <include refid="tiaocha"></include>
        LIMIT ${startIndex},${endIndex}
    </select>
    <sql id="tiaocha">
        <where>
            <if test="product.title!=null and  product.title!=''">
                and t.title like '%${product.title}%'
            </if>
            <if test="product.description!=null and  product.description!=''">
                and t.description like '%${product.description}%'
            </if>
        </where>
    </sql>



    <delete id="deleteProductByIdAndIds" parameterType="int">
        delete from shop_product where productid in
        (
        <foreach collection="array" index="index" item="ids" separator=",">
            #{ids}
        </foreach>
        )
    </delete>


    <!--添加-->
    <insert id="saveProduct" parameterType="com.limy.model.Product">
		insert into shop_product
		  (productid,title,description,num,price,imgid,issale,saleprice,ishot,ison,istui,createtime,storename)
		  values
		  (#{productid},#{title},#{description},#{num},#{price},#{imgid},#{issale},#{saleprice},#{ishot},#{ison},#{istui},#{createtime},#{storename})
	</insert>

    <!--根据id查询单条详情-->
    <select id="findProductById" resultType="com.limy.model.Product">
      select * from shop_product where productid=#{productid}
    </select>

    <!--修改-->
    <update id="updateProduct" parameterType="com.limy.model.Product">
		update shop_product set productid=#{productid},cateid=#{cateid},title=#{title},description=#{description},num=#{num},
		price=#{price},imgid=#{imgid},issale=#{issale},
		saleprice=#{saleprice},ishot=#{ishot},ison=#{ison},
		istui=#{istui},createtime=#{createtime},storeid=#{storeid},storename=#{storename}
		where productid = #{productid}
	</update>


    <select id="fandAll" resultType="com.limy.model.Product">
      select * from shop_product
    </select>

</mapper>

10:product实体类

package com.limy.model;

import java.io.Serializable;

public class Product implements Serializable {
    private static final long serialVersionUID = -7358719935960505448L;
    //商品表
        private     Integer         productid;          //商品id
        private     Integer         cateid;                //商品分类id
        private     String           title;                   //商品标题
        private     String           description;       //商品描述
        private     Integer         num;                  //库存
        private     Double         price;                 //价格
        private     String           imgid;                //封面图
        private     Integer         issale;                 //是否促销(0:否,1:是)
        private     Double         saleprice;            //促销价格
        private     Integer         ishot;                  //是否热卖(0:否,1:是)
        private     Integer         ison;                   //是否上架(0:否,1:是)
        private     Integer         istui;                   //是否推荐(0:否,1:是)
        private     String           createtime;         //创建时间
        private     Integer          storeid;            //店铺id
        private     String           storename;         //店铺名字


    public Integer getStoreid() { return storeid; }

    public void setStoreid(Integer storeid) { this.storeid = storeid; }

    public String getStorename() {return storename; }

    public void setStorename(String storename) { this.storename = storename; }

    public Integer getProductid() {
        return productid;
    }

    public void setProductid(Integer productid) {
        this.productid = productid;
    }

    public Integer getCateid() {
        return cateid;
    }

    public void setCateid(Integer cateid) {
        this.cateid = cateid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getImgid() {
        return imgid;
    }

    public void setImgid(String imgid) {
        this.imgid = imgid;
    }

    public Integer getIssale() {
        return issale;
    }

    public void setIssale(Integer issale) {
        this.issale = issale;
    }

    public Double getSaleprice() {
        return saleprice;
    }

    public void setSaleprice(Double saleprice) {
        this.saleprice = saleprice;
    }

    public Integer getIshot() {
        return ishot;
    }

    public void setIshot(Integer ishot) {
        this.ishot = ishot;
    }

    public Integer getIson() {
        return ison;
    }

    public void setIson(Integer ison) {
        this.ison = ison;
    }

    public Integer getIstui() {
        return istui;
    }

    public void setIstui(Integer istui) {
        this.istui = istui;
    }

    public String getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    @Override
    public String toString() {
        return "Product{" +
                "productid=" + productid +
                ", cateid=" + cateid +
                ", title='" + title + '\'' +
                ", description='" + description + '\'' +
                ", num=" + num +
                ", price=" + price +
                ", imgid='" + imgid + '\'' +
                ", issale=" + issale +
                ", saleprice=" + saleprice +
                ", ishot=" + ishot +
                ", ison=" + ison +
                ", istui=" + istui +
                ", createtime='" + createtime + '\'' +
                ", storeid=" + storeid +
                ", storename='" + storename + '\'' +
                '}';
    }
}

11:index.html-----引入所有的js文件

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>js引入页面</title>
    <div th:fragment="header">
    <!-- 引入bootstrap的css -->
    <link  href="../js/bootstrap/css/bootstrap.min.css" th:href="@{../js/bootstrap/css/bootstrap.min.css}" rel="stylesheet" >
    <!-- 引入bootstrap-treeview的css -->
    <link  href="../js/treeview/bootstrap-treeview.min.css" th:href="@{../js/treeview/bootstrap-treeview.min.css}" rel="stylesheet" >
    <!-- 引入bootstrap-addTabs的css -->
    <link  href="../js/addTabs/addTabs.css"  th:href="@{../js/addTabs/addTabs.css}" rel="stylesheet" >
    <!-- 引入bootstrap-table的css -->
    <link  href="../js/table/bootstrap-table.min.css"  th:href="@{../js/table/bootstrap-table.min.css}" rel="stylesheet" >
    <!-- 引入fileinput的css -->
    <link type="text/css" rel="stylesheet" href="../js/fileinput/css/fileinput.min.css"  th:href="@{../js/fileinput/css/fileinput.min.css}" />




    <!-- 引入jquery -->
    <script type="text/javascript" src="../js/jquery.min.js" th:src="@{../js/jquery.min.js}"></script>
    <!-- 引入my97 -->
    <script type="text/javascript" src="../js/my97/WdatePicker.js" th:src="@{../js/my97/WdatePicker.js}"></script>
    <!-- 引入bootstrap的js-->
    <script type="text/javascript" src="../js/bootstrap/js/bootstrap.min.js"  th:src="@{../js/bootstrap/js/bootstrap.min.js}"></script>
    <!-- 引入bootstrap的js-->
    <script type="text/javascript" src="../js/treeview/bootstrap-treeview.min.js" th:src="@{../js/treeview/bootstrap-treeview.min.js}"></script>
    <!-- 引入bootstrap的js-->
    <script type="text/javascript" src="../js/addTabs/addTabs.js" th:src="@{../js/addTabs/addTabs.js}"></script>
    <!-- 引入bootstrap-table的js-->
    <script type="text/javascript" src="../js/table/bootstrap-table.min.js" th:src="@{../js/table/bootstrap-table.min.js}"></script>
    <script type="text/javascript" src="../js/table/locale/bootstrap-table-zh-CN.min.js" th:src="@{../js/table/locale/bootstrap-table-zh-CN.min.js}"></script>
    <!-- 引入bootbox.js -->
    <script type="text/javascript" src="../js/bootbox/bootbox.js" th:src="@{../js/bootbox/bootbox.js}"></script>
        <!-- 引入fileinput的js -->
        <script type="text/javascript" src="../js/fileinput/js/fileinput.js" th:src="@{../js/fileinput/js/fileinput.min.js}"></script>
        <!--  引入fileinput的js -->
        <script type="text/javascript" src="../js/fileinput/js/locales/zh.js" th:src="@{../js/fileinput/js/locales/zh.js}"></script>


        <script type="text/javascript" src="../js/fileinput/themes/fa/theme.js" th:src="@{../js/fileinput/themes/fa/theme.js}"></script>


        <!-- 引入时间插件 -->
        <link type="text/css" rel="stylesheet" href="../js/datetimepicker/css/bootstrap-datetimepicker.css" th:href="@{../js/datetimepicker/css/bootstrap-datetimepicker.css}"/>


        <script type="text/javascript" src="../js/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js" th:src="@{../js/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js}"></script>
        <script type="text/javascript" src="../js/datetimepicker/js/bootstrap-datetimepicker.js" th:src="@{../js/datetimepicker/js/bootstrap-datetimepicker.js}"></script>

        <script type="text/javascript" src="../js/area.js" th:src="@{../js/area.js}"></script>
    </div>
</head>
<body>
测试跳转
</body>
</html>

12:效果图
在这里插入图片描述

<think>好的,我现在需要帮用户找到关于Spring Boot实现增删改查(CRUD)功能的示例或教程,特别是涉及页面实现的。根据用户提供的四个引用,我需要从中提取相关信息,并整理出一个结构清晰的回答。 首先,引用[1]提到了Spring Boot 3的RESTful API增删改查详细教程,其中介绍了RESTful架构风格。这可能涉及到后端API的实现,但用户需要页面,也就是前端部分,可能需要结合其他引用。 引用[2]提到使用了thymeleaf、restful、layui.css和bootstrap。这说明有一个教程可能结合了Spring Boot的后端与Thymeleaf模板引擎作为前端,同时使用Layui和Bootstrap进行页面美化。这应该符合用户的需求,因为包含了页面实现的部分。 引用[3]是关于整合MyBatis Plus实现CRUD,使用的是MyEclipse创建项目。这里的关键点是MyBatis Plus作为持久层框架,可能提供了后端CRUD操作的示例,但不确定是否有页面部分。需要进一步确认。 引用[4]提供了一个简易的CRUD案例,使用MySQL数据库,Navicat Premium,数据库表结构为user,包含id、username、password字段。这里可能是一个基础的增删改查实现,可能包含前后端,但需要看具体内容。 接下来,我需要将这些信息整合成一个分步骤的教程。首先,用户可能需要环境搭建,比如创建Spring Boot项目,配置数据库,然后是前后端的实现。根据引用[4],数据库使用MySQL,表结构已经给出,这可以作为数据模型。引用[3]中的MyBatis Plus整合可以简化数据访问层的开发。引用[2]中的Thymeleaf和前端框架(Layui、Bootstrap)可以用来构建页面。引用[1]的RESTful API可能需要调整,因为用户需要页面而不是纯API,但也可以结合使用,比如前端通过Thymeleaf渲染,后端提供数据处理。 可能需要分为以下几个步骤: 1. 创建Spring Boot项目,配置依赖(如Spring Web、MyBatis Plus、MySQL、Thymeleaf)。 2. 配置数据库连接,创建User实体类。 3. 使用MyBatis Plus生成Mapper和Service层,实现CRUD操作。 4. 创建Controller,处理HTTP请求,返回Thymeleaf模板。 5. 使用Thymeleaf编写前端页面,结合Layui和Bootstrap进行布局和样式设计。 6. 测试增删改查功能。 在回答中需要引用相关的参考文献,比如引用[3]和[4]关于MyBatis Plus和数据库配置,引用[2]关于前端页面实现。同时,用户可能需要示例代码,比如Controller的编写和Thymeleaf页面的结构。 需要注意用户是否希望前后端分离还是传统的MVC模式。根据引用[2],使用的是Thymeleaf,这意味着后端渲染前端页面,属于MVC模式,所以应按照这种方式指导。 可能还需要提到如何整合前端框架,比如引入Layui和Bootstrap的CSS和JS文件,如何在Thymeleaf模板中使用这些样式,以及如何通过表单提交数据到后端Controller。 最后,生成相关问题,比如如何优化CRUD性能,如何处理表单验证,如何分页等,引导用户进一步学习。</think>以下是为您整理的Spring Boot实现增删改查功能的开发指南,包含页面实现和关键代码示例: ### 一、开发环境准备 1. 使用IDEA或MyEclipse创建Spring Boot项目 2. 添加依赖(pom.xml): ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` ### 二、数据库配置(application.yml) ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/springboot_crud?useSSL=false username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver thymeleaf: prefix: classpath:/templates/ suffix: .html ``` ### 三、实体类与Mapper ```java // User.java @Data public class User { private Long id; private String username; private String password; } // UserMapper.java @Mapper public interface UserMapper extends BaseMapper<User> { } ``` ### 四、Controller实现 ```java @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; // 用户列表页 @GetMapping public String list(Model model) { model.addAttribute("users", userService.list()); return "user/list"; } // 新增用户 @PostMapping public String create(User user) { userService.save(user); return "redirect:/user"; } } ``` ### 五、页面实现(Thymeleaf模板) `src/main/resources/templates/user/list.html`示例: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <link rel="stylesheet" href="https://cdn.layui.com/layui-v2.7.6/css/layui.css"> </head> <body> <table class="layui-table"> <tr th:each="user : ${users}"> <td th:text="${user.username}"></td> <td> <a th:href="@{/user/delete/{id}(id=${user.id})}" class="layui-btn layui-btn-danger">删除</a> </td> </tr> </table> </body> </html> ``` ### 六、功能扩展建议 1. 分页查询:结合PageHelper插件实现[^3] 2. 表单验证:使用Hibernate Validator进行数据校验[^1] 3. 前端交互:通过Layui的layer组件实现弹窗表单[^2]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值