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

目录

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

 在启动目录下建立Product.java

建立mapper文件

 建立controller文件

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

数据库


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

 把这里的代码替换掉

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

 在启动目录下建立Product.java

 

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文件

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<Product> 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文件

 

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 "添加完成!<a href='/'>返回首页</a>";
    }

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

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

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


}

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

<body>
<h1>商品管理系统</h1>
<a href="/add.html">添加商品</a>
<a href="/select">商品列表</a>
<a href="/update.html">修改商品</a>
</body>
<h1>添加商品</h1>
<form action="/insert">
    <input type="text" name="title" placeholder="标题">
    <input type="text" name="price" placeholder="价格">
    <input type="text" name="num" placeholder="库存">
    <input type="submit" value="添加">
</form>
<body>
<h1>修改页面</h1>
<form action="/update">
  <input type="text" name="id" placeholder="请输入修改商品的id">
  <input type="text" name="title" placeholder="标题">
  <input type="text" name="price" placeholder="价格">
  <input type="text" name="num" placeholder="库存">
  <input type="submit" value="修改">
</form>
</body>

数据库

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
);
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

居然天上楼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值