• 【用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

     

    1. package cn.tedu.boot04.entity;
    2. public class Product {
    3. private Integer id;
    4. private String title;
    5. private Integer price;
    6. private Integer num;
    7. @Override
    8. public String toString() {
    9. return "Product{" +
    10. "id=" + id +
    11. ", title='" + title + '\'' +
    12. ", price=" + price +
    13. ", num=" + num +
    14. '}';
    15. }
    16. public Integer getId() {
    17. return id;
    18. }
    19. public void setId(Integer id) {
    20. this.id = id;
    21. }
    22. public String getTitle() {
    23. return title;
    24. }
    25. public void setTitle(String title) {
    26. this.title = title;
    27. }
    28. public Integer getPrice() {
    29. return price;
    30. }
    31. public void setPrice(Integer price) {
    32. this.price = price;
    33. }
    34. public Integer getNum() {
    35. return num;
    36. }
    37. public void setNum(Integer num) {
    38. this.num = num;
    39. }
    40. }

    建立mapper文件

    1. package cn.tedu.boot04.mapper;
    2. import cn.tedu.boot04.entity.Product;
    3. import org.apache.ibatis.annotations.*;
    4. import java.util.List;
    5. //Mapper注解作用: 设置当前接口为 映射接口,映射接口是供Mybatis框架生成JDBC
    6. //代码的依据,在接口中定义方法和书写SQL语句
    7. @Mapper
    8. public interface ProductMapper {
    9. //#{xxx}此指令会从注解下面方法的参数列表中找同名变量,如果找不到
    10. //则会调用参数列表中变量的同名get方法
    11. @Insert("insert into product values(null,#{title},#{price},#{num})")
    12. void insert(Product product);
    13. //声明返回值类型为List集合 Mybatis框架生成JDBC代码时会自动将查询到的数据
    14. //封装到Product对象里面, 并且把对象添加到一个list集合中,把集合return出来
    15. @Select("select id,title,price,num from product")
    16. List select();
    17. //删除注解 定义删除相关的SQL语句
    18. @Delete("delete from product where id=#{id}")
    19. void deleteById(int id);
    20. //修改注解
    21. @Update("update product set title=#{title},price=#{price}" +
    22. ",num=#{num} where id=#{id}")
    23. void update(Product product);

     建立controller文件

     

    1. package cn.tedu.boot04.controller;
    2. import cn.tedu.boot04.entity.Product;
    3. import cn.tedu.boot04.mapper.ProductMapper;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.ResponseBody;
    8. import org.springframework.web.bind.annotation.RestController;
    9. import java.sql.Connection;
    10. import java.sql.PreparedStatement;
    11. import java.sql.SQLException;
    12. import java.util.List;
    13. //相当于在每一个方法的上面都添加了一个@ResponseBody注解
    14. @RestController
    15. public class ProductController {
    16. //Autowired自动装配注解, 此注解是Spring框架中提供的注解
    17. //此注解添加后是Spring框架和Mybatis框架结合到一起,创建了一个
    18. //接口的实现类,并且实例化了该实现类 并赋值给了mapper变量
    19. //实现类中实现了接口里面的抽象方法(insert)
    20. //(required = false)告诉idea 这个mapper不是必须的
    21. @Autowired(required = false)
    22. ProductMapper mapper;
    23. @RequestMapping("/insert")
    24. public String insert(Product product){
    25. mapper.insert(product);
    26. return "添加完成!返回首页";
    27. }
    28. @RequestMapping("/select")
    29. public String select(){
    30. List list = mapper.select();
    31. //把集合中的数据装进table表格中
    32. String html = "";
    33. html+="
    34. ";
    35. html+="
    36. ";
    37. //遍历集合 添加tr和td
    38. for (Product p:list) {
    39. html+="
    40. ";
    41. html+="
    42. ";
    43. html+="
    44. ";
    45. html+="
    46. ";
    47. html+="
    48. ";
    49. //添加删除超链接的一列, 请求地址为 /delete?id=xxx ?是请求地址和参数的分隔符
    50. html+="
    51. ";
    52. html+="
    53. ";
    54. }
    55. html+="
    56. 商品列表
      id标题价格库存操作
      "+p.getId()+""+p.getTitle()+""+p.getPrice()+""+p.getNum()+"删除
      "
      ;
    57. return html;//把页面和数据一起返回给客户端
    58. }
    59. @RequestMapping("/delete")
    60. public String delete(int id){
    61. mapper.deleteById(id);
    62. return "删除完成!返回列表页面";
    63. }
    64. @RequestMapping("/update")
    65. public String update(Product product){
    66. mapper.update(product);
    67. return "修改完成!返回列表页面";
    68. }
    69. }

    static下建立add,index,update网页

    1. <body>
    2. <h1>商品管理系统h1>
    3. <a href="/add.html">添加商品a>
    4. <a href="/select">商品列表a>
    5. <a href="/update.html">修改商品a>
    6. body>
    1. <h1>添加商品h1>
    2. <form action="/insert">
    3. <input type="text" name="title" placeholder="标题">
    4. <input type="text" name="price" placeholder="价格">
    5. <input type="text" name="num" placeholder="库存">
    6. <input type="submit" value="添加">
    7. form>
    1. <body>
    2. <h1>修改页面h1>
    3. <form action="/update">
    4. <input type="text" name="id" placeholder="请输入修改商品的id">
    5. <input type="text" name="title" placeholder="标题">
    6. <input type="text" name="price" placeholder="价格">
    7. <input type="text" name="num" placeholder="库存">
    8. <input type="submit" value="修改">
    9. form>
    10. 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
    );
     

  • 相关阅读:
    文件系统管理--文件系统常用命令—挂载命令
    基于ChatGPT的大型语言模型试用心得
    AP5186 三功能 LED 降压型恒流芯片 手电筒 LED芯片
    已解决fatal error: Python.h: No such file or directory
    Android TV 桌面图标闪
    Python编程 基础数据类型
    ChatGPT之父被OpenAI解雇
    【SpringMvc】SpringMvc +MyBatis整理
    OpenGL 对比度调节
    【好玩】如何在github主页放一条贪吃蛇
  • 原文地址:https://blog.csdn.net/weixin_72612071/article/details/127820424