目录
创建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


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

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

- 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 "修改完成!返回列表页面";
- }
-
-
- }
- <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
);