商品管理系统步骤:
CREATE TABLE product(id INT PRIMARY KEY AUTO_INCREMENT,title VARCHAR(50),
price DOUBLE(10,2),num INT);
新建SpringBoot工程,包含SpringMVC,pom中添加数据和连接池
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.15version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.21version>
dependency>
public class DBUtils {
private static DruidDataSource dds;
static {
dds=new DruidDataSource();
dds.setUrl("jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false");
dds.setUsername("root");
dds.setPassword("123456");
dds.setInitialSize(3);
dds.setMaxActive(5);
}
public static Connection getConn() throws SQLException {
Connection conn = dds.getConnection();
return conn;
}
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<a href="/insert.html">新增商品a>
<a href="/select">商品列表a>
<a href="/update.html">修改商品a>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<a href="/insert.html">新增商品a>
<a href="/select">商品列表a>
<a href="/update.html">修改商品a>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/update">
<h1>商品修改h1>
<input type="text" name="id" placeholder="请输入商品编号">
<input type="text" name="title" placeholder="请输入商品名称">
<input type="text" name="price" placeholder="请输入商品价格">
<input type="text" name="num" placeholder="请输入库存数量">
<input type="submit" value="修改">
form>
body>
html>
public class Product {
private Integer id;
private String title;
private Double price;
private Integer num;
public Product() {
}
public Product(Integer id, String title, Double price, Integer num) {
this.id = id;
this.title = title;
this.price = price;
this.num = num;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", title='" + title + '\'' +
", price=" + price +
", num=" + num +
'}';
}
//get和set省略
}
@RestController
public class ProductController {
@RequestMapping("/insert")
public String insert(Product product){
try(Connection conn=DBUtils.getConn()){
String sql="insert into product values(null,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,product.getTitle());
ps.setDouble(2,product.getPrice());
ps.setInt(3,product.getNum());
ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return "添加完成返回首页";
}
@RequestMapping("/select")
public String select(){
List<Product> list=new ArrayList<>();
try(Connection conn=DBUtils.getConn()){
String sql="select id,title,price,num from product";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()){
int id=rs.getInt(1);
String title=rs.getString(2);
double price=rs.getDouble(3);
int num=rs.getInt(4);
list.add(new Product(id,title,price,num));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
String html = "";
html+="商品列表 ";
html+="id 商品标题 价格 库存 操作 ";
for (Product p:list) {
html+="";
html+=""+p.getId()+" ";
html+=""+p.getTitle()+" ";
html+=""+p.getPrice()+" ";
html+=""+p.getNum()+" ";
html+="删除 ";
html+=" ";
}
html+="
";
return html;
}
@RequestMapping("/delete")
public String delete(int id){
try(Connection conn=DBUtils.getConn()){
String sql="delete from product where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id);
ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return "删除成功返回首页";
}
@RequestMapping("/update")
public String update(Product product){
try(Connection conn=DBUtils.getConn()){
String sql="update product set title=?,price=?,num=? where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,product.getTitle());
ps.setDouble(2,product.getPrice());
ps.setInt(3,product.getNum());
ps.setInt(4,product.getId());
ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return "修改完成返回首页";
}
}