目录
创建springweb项目,选择springweb MyBatis Framework MySQL Driver
创建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
);