• Springboot之SpringMVC相关(二)


    实现商品的增删改查

    步骤描述

    商品管理系统步骤:

    1. 创建boot02-1工程
    2. 在static里面创建index.html首页页面, 在页面中添加超链接, 请求地址为/insert.html
    3. 添加功能: 创建insert.html页面, 页面中有form表单获取用户输入的商品信息,提交地址为/insert
    4. 创建controller.ProductController,添加@RestController注解, 添加insert方法处理/insert请求,创建entity.Product实体类, 在insert方法参数列表处声明Product对象 接收传递过来的参数, 控制台输出接收到的product对象, 如果没有问题接下来通过JDBC代码把product对象中的数据保存到product表中 给客户端响应"添加完成! 返回首页"
    5. 列表功能: 在首页中添加商品列表超链接, 请求地址为/select ,在Controller中添加select方法处理/select请求,在方法中通过JDBC代码查询到所有商品的信息并把商品信息封装到Product对象中,然后把Product对象装进一个List集合, 最后遍历集合把数据装进一个html字符串中 响应给客户端(可复制粘贴)
    6. 删除功能: 在商品列表的表格中添加操作列, 每一行数据添加一个删除超链接, 请求地址为/delete?id=xxx
    7. 在ProductController中添加delete方法处理/delete请求, 接收传递过来的id参数并在控制台输出测试, 如果没有问题 则通过JDBC代码 执行删除的SQL语句 把数据库中的数据删除掉,然后给客户端响应"删除完成 返回首页"
    8. 修改功能:在首页添加修改超链接, 请求地址为/update.html页面
    9. 创建update.html页面,页面中提供form表单获取输入的修改信息, 提交地址为/update
    10. 在ProductController中添加udpate方法 处理/update请求, 方法的参数列表处声明Product对象接收传递过来的参数,控制台输出 测试, 如果测试没问题 通过JDBC代码执行SQL语句把数据库里面的数据修改掉.最后给客户端响应"修改完成 返回首页"

    项目结构

    在这里插入图片描述

    建表语句

    CREATE TABLE product(id INT PRIMARY KEY AUTO_INCREMENT,title VARCHAR(50),
    price DOUBLE(10,2),num INT);
    
    • 1
    • 2

    pom.xml

    新建SpringBoot工程,包含SpringMVC,pom中添加数据和连接池

     
       <dependency>
           <groupId>mysqlgroupId>
           <artifactId>mysql-connector-javaartifactId>
           <version>8.0.15version>
       dependency>
       
       <dependency>
           <groupId>com.alibabagroupId>
           <artifactId>druidartifactId>
           <version>1.1.21version>
       dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    DBUtils

    public class DBUtils {
        private static DruidDataSource dds;
        static {
            dds=new DruidDataSource();
            dds.setUrl("jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false");
            dds.setUsername("root");
            dds.setPassword("123456");
            dds.setInitialSize(3);
            dds.setMaxActive(5);
        }
        public static Connection getConn() throws SQLException {
            Connection conn = dds.getConnection();
            return conn;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    前端代码

    主页
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <a href="/insert.html">新增商品a>
    <a href="/select">商品列表a>
    <a href="/update.html">修改商品a>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    插入页面
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <a href="/insert.html">新增商品a>
    <a href="/select">商品列表a>
    <a href="/update.html">修改商品a>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    修改页面
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <form action="/update">
        <h1>商品修改h1>
        <input type="text" name="id" placeholder="请输入商品编号">
        <input type="text" name="title" placeholder="请输入商品名称">
        <input type="text" name="price" placeholder="请输入商品价格">
        <input type="text" name="num" placeholder="请输入库存数量">
        <input type="submit" value="修改">
    form>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    后端代码

    实体类
    public class Product {
        private Integer id;
        private String title;
        private Double price;
        private Integer num;
    
        public Product() {
        }
    
        public Product(Integer id, String title, Double price, Integer num) {
            this.id = id;
            this.title = title;
            this.price = price;
            this.num = num;
        }
    
        @Override
        public String toString() {
            return "Product{" +
                    "id=" + id +
                    ", title='" + title + '\'' +
                    ", price=" + price +
                    ", num=" + num +
                    '}';
        }
    
      //get和set省略
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    controller
    @RestController
    public class ProductController {
        @RequestMapping("/insert")
        public String insert(Product product){
            try(Connection conn=DBUtils.getConn()){
                String sql="insert into product values(null,?,?,?)";
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1,product.getTitle());
                ps.setDouble(2,product.getPrice());
                ps.setInt(3,product.getNum());
                ps.executeUpdate();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return "添加完成返回首页";
        }
    
        @RequestMapping("/select")
        public String select(){
            List<Product> list=new ArrayList<>();
            try(Connection conn=DBUtils.getConn()){
                String sql="select id,title,price,num from product";
                PreparedStatement ps = conn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                while (rs.next()){
                    int id=rs.getInt(1);
                    String title=rs.getString(2);
                    double price=rs.getDouble(3);
                    int num=rs.getInt(4);
                    list.add(new Product(id,title,price,num));
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            String html = "";
            html+="";
            html+="";for(Product p:list){
                html+="";
                html+="";
                html+="";
                html+="";
                html+="";
                html+="";
                html+="";}
            html+="
    商品列表
    id商品标题价格库存操作
    "+p.getId()+""+p.getTitle()+""+p.getPrice()+""+p.getNum()+"删除
    "
    ; return html; } @RequestMapping("/delete") public String delete(int id){ try(Connection conn=DBUtils.getConn()){ String sql="delete from product where id=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,id); ps.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); } return "删除成功返回首页"; } @RequestMapping("/update") public String update(Product product){ try(Connection conn=DBUtils.getConn()){ String sql="update product set title=?,price=?,num=? where id=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,product.getTitle()); ps.setDouble(2,product.getPrice()); ps.setInt(3,product.getNum()); ps.setInt(4,product.getId()); ps.executeUpdate(); } catch (SQLException throwables) { throwables.printStackTrace(); } return "修改完成返回首页"; } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    项目总结

    • 使用RestController 相当于每个方法上面都自动添加了一个@ResponseBody注解
  • 相关阅读:
    学生个人网页设计作品 学生个人网页模板 简单个人主页成品 个人网页制作 HTML学生个人网站作业设计
    【力扣】整数反转,判断是否溢出的数学解法
    java毕业生设计医院病房管理系统计算机源码+系统+mysql+调试部署+lw
    Linux--系统烧写
    锐捷Wlan——AC热备实验(DHCP在核心交换机)
    【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 数字排列游戏(200分) - 三语言AC题解(Python/Java/Cpp)
    常用数据库之sqlite的使用
    【C++】C++基础知识(七)---指针
    word 替换全部字母和数字为新罗马
    C# ref用法,实现引用传递(地址传递)
  • 原文地址:https://blog.csdn.net/longgetaotao_06/article/details/126747641