运行结果:
- GoodsDao
package com.itheima.dao;
import com.itheima.pojo.Goods;
import com.itheima.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class GoodsDao {
private QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
public List<Goods> queryAll() throws SQLException {
String sql = "select * from t_goods";
return qr.query(sql,new BeanListHandler<>(Goods.class));
}
public void addGoods(Goods goods) throws SQLException {
String sql = "insert into t_goods values(?,?,?,?,?,?)";
Object[] pamars = {
null,goods.getName(),goods.getPrice(),
goods.getStock(),goods.getDescription(),goods.getType_name()};
qr.update(sql,pamars);
}
public int deleGoods(String id) throws SQLException {
String sql = "delete from t_goods where id=?";
return qr.update(sql, id);
}
public Goods findByIdGood(String id) throws SQLException {
String sql = "select * from t_goods where id=?";
return qr.query(sql,new BeanHandler<>(Goods.class),id);
}
public int updateGoods(Goods goods) throws SQLException {
String sql = "update t_goods set NAME = ?" +
",price=?,stock=?,description=?,type_name=? where id = ?";
Object[] pamars = {
null,goods.getName(),goods.getPrice(),
goods.getStock(),goods.getDescription(),goods.getDescription()};
return qr.update(sql,pamars);
}
}
- Result
package com.itheima.entity;
/**
* HTTP接口统一的返回数据结构
**/
public class Result {
private Boolean flag;//执行结果,true为执行成功 false为执行失败
private String message;//返回结果信息
private Object data;//返回数据
public Boolean getFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Result(Boolean flag, String message) {
this.flag = flag;
this.message = message;
}
public Result(Boolean flag, String message, Object data) {
this.flag = flag;
this.message = message;
this.data = data;
}
}
- Goods
package com.itheima.pojo;
/**
* @author 黑马程序员
* @Company http://www.ithiema.com
* @Version 1.0
*/
public class Goods {
private Integer id;
private String name;
private Double price;
private Integer stock;
private String description;
private String type_name;
public Goods() {
}
public Goods(Integer id, String name, Double price, Integer stock, String description, String type_name) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
this.description = description;
this.type_name = type_name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType_name() {
return type_name;
}
public void setType_name(String type_name) {
this.type_name = type_name;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", stock=" + stock +
", description='" + description + '\'' +
", type_name='" + type_name + '\'' +
'}';
}
}
- GoodsService
package com.itheima.service;
import com.itheima.dao.GoodsDao;
import com.itheima.pojo.Goods;
import java.sql.SQLException;
import java.util.List;
public class GoodsService {
private GoodsDao goodsDao = new GoodsDao();
public List<Goods> queryAll(){
try {
return goodsDao.queryAll();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public boolean addGoods(Goods goods){
try {
goodsDao.addGoods(goods);
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public int deleGoods(String id){
try {
return goodsDao.deleGoods(id);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
public Goods findGoods(String id){
try {
return goodsDao.findByIdGood(id);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public int updateGoods(Goods goods){
try {
return goodsDao.updateGoods(goods);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}
- DruidUtils
package com.itheima.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* 阿里巴巴的连接池 Druid 工具类
*/
public class DruidUtils {
/*
1. 加载 druid.properties 配置文件
2. 创建 Druid 连接池对象
3. 提供 获得 连接池对象的方法
4. 提供 从连接池中 获取连接对象Connection的 方法
*/
public static DataSource ds = null;
static {
try {
//1. 加载 druid.properties 配置文件
InputStream is = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
Properties prop = new Properties();
prop.load(is)