【用Mapper替代DBUtils实现商品管理系统】

目录

创建springweb项目,选择springweb MyBatis Framework MySQL Driver

在启动目录下建立Product.java

建立mapper文件

建立controller文件

在static下建立add,index,update网页

数据库

创建springweb项目,选择springweb MyBatis Framework MySQL Driver

【用Mapper替代DBUtils实现商品管理系统】

把这里的代码替换掉

spring.datasource.url=jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

【用Mapper替代DBUtils实现商品管理系统】

在启动目录下建立Product.java

【用Mapper替代DBUtils实现商品管理系统】
package cn.tedu.boot04.entity;

public class Product {
    private Integer id;
    private String title;
    private Integer price;
    private Integer num;

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", price=" + price +
                ", num=" + num +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

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

    public Integer getPrice() {
        return price;
    }

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

    public Integer getNum() {
        return num;
    }

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

建立mapper文件

【用Mapper替代DBUtils实现商品管理系统】
package cn.tedu.boot04.mapper;

import cn.tedu.boot04.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

//Mapper注解作用: 设置当前接口为 映射接口,映射接口是供Mybatis框架生成JDBC
//代码的依据,在接口中定义方法和书写SQL语句
@Mapper
public interface ProductMapper {

    //#{xxx}此指令会从注解下面方法的参数列表中找同名变量,如果找不到
    //则会调用参数列表中变量的同名get方法
    @Insert("insert into product values(null,#{title},#{price},#{num})")
    void insert(Product product);

    //声明返回值类型为List集合 Mybatis框架生成JDBC代码时会自动将查询到的数据
    //封装到Product对象里面, 并且把对象添加到一个list集合中,把集合return出来
    @Select("select id,title,price,num from product")
    List select();

    //删除注解 定义删除相关的SQL语句
    @Delete("delete from product where id=#{id}")
    void deleteById(int id);

    //修改注解
    @Update("update product set title=#{title},price=#{price}" +
            ",num=#{num} where id=#{id}")
    void update(Product product);

建立controller文件

【用Mapper替代DBUtils实现商品管理系统】
package cn.tedu.boot04.controller;

import cn.tedu.boot04.entity.Product;
import cn.tedu.boot04.mapper.ProductMapper;
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 org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
//相当于在每一个方法的上面都添加了一个@ResponseBody注解
@RestController
public class ProductController {
    //Autowired自动装配注解, 此注解是Spring框架中提供的注解
    //此注解添加后是Spring框架和Mybatis框架结合到一起,创建了一个
    //接口的实现类,并且实例化了该实现类 并赋值给了mapper变量
    //实现类中实现了接口里面的抽象方法(insert)
    //(required = false)告诉idea 这个mapper不是必须的
    @Autowired(required = false)
    ProductMapper mapper;

    @RequestMapping("/insert")
    public String insert(Product product){
        mapper.insert(product);
        return "添加完成!返回首页";
    }

    @RequestMapping("/select")
    public String select(){
        List list = mapper.select();
        //把集合中的数据装进table表格中
        String html = "";
        html+="商品列表";
        html+="id标题价格库存操作";
        //遍历集合 添加tr和td
        for (Product p:list) {
            html+="";
            html+=""+p.getId()+"";
            html+=""+p.getTitle()+"";
            html+=""+p.getPrice()+"";
            html+=""+p.getNum()+"";
            //添加删除超链接的一列, 请求地址为 /delete?id=xxx  ?是请求地址和参数的分隔符
            html+="删除";
            html+="";
        }
        html+="";
        return html;//把页面和数据一起返回给客户端
    }

    @RequestMapping("/delete")
    public String delete(int id){
        mapper.deleteById(id);
        return "删除完成!返回列表页面";
    }

    @RequestMapping("/update")
    public String update(Product product){
        mapper.update(product);
        return "修改完成!返回列表页面";
    }

}

在static下建立add,index,update网页


商品管理系统
添加商品
商品列表
修改商品
添加商品


修改页面

数据库

show databases ;
CREATE DATABASE empdb charset=utf8;
use empdb;

create table product(
id int primary key auto_increment,
title varchar(50),
price int,
num int
);

Original: https://blog.csdn.net/weixin_72612071/article/details/127820424
Author: 居然天上楼
Title: 【用Mapper替代DBUtils实现商品管理系统】

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/659318/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球