二、商品管理模块
分析关联的表
kind表
position表
goods表
先写类别,再写货架,再写商品
先写商品,再写类别,再写货架
先讲商品表:
业务:三表联查,将数据显示在表格中。
`select goods.goodsId,goods.`name`,goods.barcode,goods.price,goods.number,position.`name` as pname,kind.`name` as kname from kind inner join goods on kind.kindId = goods.kindId inner join position on goods.positionId = position.positionId`
下拉列表框:指类别和货架,为什么?因为下拉列表中的类别名称、货架名称来自数据库表中查询出来的数据。
查所有的类别名称
select name from kind
查所有的货架名称
select name from position
根据类别名称查类别的id
select kindid from kind where name = '保健品'
根据货柜名称查货柜的id
select positionId from position where name = 'A1'
如果类别正在使用,不能删除,货架同理。
如何判断类别是否在用?
用当前的货框id去商品表查,如果查得到说明在用,如果查不到说明没有被使用。
select goodsId from goods where positionId = 1
select goodsId from goods where kindId = 1
效果图:
实体代码
package com.store.entity;
/**
* @ClassName Goods
* @Description: TODO
* @Author 汤永红
* @Date 2020/9/1 0001-9:06
* @Version V1.0
**/
public class Goods {
/**
* 商品i
*/
private Integer goodsId;
/**
* 商品名称
*/
private String name;
/**
* 条形码
*/
private String barcode;
/**
* 价格
*/
private Float price;
/**
* 数量
*/
private Integer number;
/**
* 外键
*/
private Integer positionId;
/**
* 外键
*/
private Integer kindId;
/**
* 商品图片
*/
private String icon;
/**
* 类别名称
*/
private String kname;
/**
* 货架名称
*/
private String pname;
public Goods() {
}
public Goods(Integer goodsId, String name, String barcode, Float price, Integer number, Integer positionId, Integer kindId, String icon) {
this.goodsId = goodsId;
this.name = name;
this.barcode = barcode;
this.price = price;
this.number = number;
this.positionId = positionId;
this.kindId = kindId;
this.icon = icon;
}
public Goods(String name, String barcode, Float price, Integer number, Integer positionId, Integer kindId, String icon) {
this.name = name;
this.barcode = barcode;
this.price = price;
this.number = number;
this.positionId = positionId;
this.kindId = kindId;
this.icon = icon;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBarcode() {
return barcode;
}
public void setBarcode(String barcode) {
this.barcode = barcode;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public Integer getPositionId() {
return positionId;
}
public void setPositionId(Integer positionId) {
this.positionId = positionId;
}
public Integer getKindId() {
return kindId;
}
public void setKindId(Integer kindId) {
this.kindId = kindId;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getKname() {
return kname;
}
public void setKname(String kname) {
this.kname = kname;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
@Override
public String toString() {
return "Goods{" +
"goodsId=" + goodsId +
", name='" + name + '\'' +
", barcode='" + barcode + '\'' +
", price=" + price +
", number=" + number +
", positionId=" + positionId +
", kindId=" + kindId +
", icon='" + icon + '\'' +
'}';
}
}
导出Excel代码
package com.store.util;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.TableModel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExportExcel {
//要处理的表格
JTable table;
//文件输出流
FileOutputStream fos;
//文件选择对话框
JFileChooser jfc = new JFileChooser();
// 导出Excel 的数据来自哪张表
public ExportExcel(JTable table) {
this.table = table;
// 过滤 只接收后缀名是xls的文件
jfc.addChoosableFileFilter(new FileFilter() {
public boolean accept(File file) {
return (file.getName().indexOf("xls") != -1);
}
public String getDescription() {
return "Excel";
}
});
// 弹出保存对话框
jfc.showSaveDialog(null);
// 获得保存文件的路径
File file = jfc.getSelectedFile();
try {
// 输出到哪里 路径+文件名 d:/t57/store.xls
this.fos = new FileOutputStream(file + ".xls");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
public void export() {
// 创建工作薄
HSSFWorkbook wb = new HSSFWorkbook();
// 创建工作表
HSSFSheet hs = wb.createSheet();
// 获得表格模型,传过来的表格,我们要拿出里面的数据
TableModel tm = table.getModel();
// 有多少行
int row = tm.getRowCount();
// 有多少列
int cloumn = tm.getColumnCount();
// System.out.println("row " + row + " column " + cloumn);
// 创建excel的单元格
HSSFCellStyle style = wb.createCellStyle();
// 周边的线的样式
// 底
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 左
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// 右
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// 上
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 前景色,文字的颜色
style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
// Pattern模式,实线 虚线 点划线
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 字体
HSSFFont font = wb.createFont();
// 字号
font.setFontHeightInPoints((short) 11);
// 把字体告诉样式
style.setFont(font);
// 单元格样式
HSSFCellStyle style1 = wb.createCellStyle();
// 苗条型
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
//
style1.setFillForegroundColor(HSSFColor.ORANGE.index);
style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 字体
HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short) 15);
font1.setBoldweight((short) 700);
style1.setFont(font);
// 循环 行
for (int i = 0; i < row + 1; i++) {
// 造一行
HSSFRow hr = hs.createRow(i);
// 列
for (int j = 0; j < cloumn; j++) {
if (i == 0) {
// 表头
// 获得列名
String value = tm.getColumnName(j);
System.out.println("value " + value);
int len = value.length();
//hs.setColumnWidth(j, (len * 400));
hs.setColumnWidth(j, 200);
System.out.println(hs.getColumnWidth(j));
HSSFRichTextString srts = new HSSFRichTextString(value);
HSSFCell hc = hr.createCell(j);
hc.setCellStyle(style1);
hc.setCellValue(srts);
} else {
String vv = tm.getColumnName(j);
// System.out.println("value " + value);
int len = vv.length();
// 算列宽(导出去是合适的列宽)
hs.setColumnWidth(j, (len * 400));
// System.out.println("vlue " + tm.getValueAt(i - 1, j));
// 取值
if (tm.getValueAt(i - 1, j) != null) {
// 数据
String value = tm.getValueAt(i - 1, j).toString();
HSSFRichTextString srts = new HSSFRichTextString(value);
HSSFCell hc = hr.createCell((short) j);
hc.setCellStyle(style);
if (value.equals("") || value == null) {
hc.setCellValue(new HSSFRichTextString(""));
} else {
hc.setCellValue(srts);
}
}
}
}
}
// hs.setColumnWidth(0, 400);
try {
wb.write(fos);
fos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
实现类代码
package com.store.dao.impl;
import com.store.dao.GoodsDao;
import com.store.db.DBHelper;
import com.store.entity.Goods;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName GoodsDaoImpl
* @Description: TODO
* @Author 汤永红
* @Date 2020/9/1 0001-9:33
* @Version V1.0
**/
public class GoodsDaoImpl extends DBHelper implements GoodsDao {
@Override
public List<Goods> findAll() {
List<Goods> lists = null;
conn = getConnection();
String sql = "select goods.goodsId,goods.`name`,goods.barcode,goods.price,goods.number,position.`name` as pname,kind.`name` as kname from kind inner join goods on kind.kindId = goods.kindId inner join position on goods.positionId = position.positionId";
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs != null) {
lists = new ArrayList<>();
while (rs.next()) {
Goods goods = new Goods();
//装
goods.setGoodsId(rs.getInt("goodsId"));
goods.setName(rs.getString("name"));
goods.setBarcode(rs.getString("barcode"));
goods.setPrice(rs.getFloat("price"));
goods.setNumber(rs.getInt("number"));
goods.setPname(rs.getString("pname"));
goods.setKname(rs.getString("kname"));
lists.add(goods);
}
}
} catch (SQLException e) {
} finally {
closeAll(rs, pstmt, conn);
}
return lists;
}
}
服务层代码
package com.store.service;
import com.store.entity.Goods;
import java.util.List;
public interface GoodsService {
/**
* 查询所有商品信息
*
* @return 商品信息的集合
*/
List<Goods> findAll();
}
package com.store.service.impl;
import com.store.dao.GoodsDao;
import com.store.dao.impl.GoodsDaoImpl;
import com.store.entity.Goods;
import com.store.service.GoodsService;
import java.util.List;
/**
* @ClassName GoodsServiceImpl
* @Description: TODO
* @Author 汤永红
* @Date 2020/9/1 0001-9:41
* @Version V1.0
**/
public class GoodsServiceImpl implements GoodsService {
private GoodsDao goodsDao = new GoodsDaoImpl();
@Override
public List<Goods> findAll() {
return goodsDao.findAll();
}
}
界面代码
package com.store.ui;
import com.store.entity.Goods;
import com.store.service.GoodsService;
import com.store.service.impl.GoodsServiceImpl;
import com.store.util.ExportExcel;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;
/**
* @ClassName GoodsPanel
* @Description: TODO
* @Author 汤永红
* @Date 2020/8/31 0031-10:37
* @Version V1.0
**/
public class GoodsPanel extends JPanel {
GoodsService service = new GoodsServiceImpl();
//1.声明要用的
//上
JPanel panelTop;
//上上
JPanel panelTop1;
//上下
JPanel panelTop2;
//下
JPanel panelBottom;
//
JScrollPane scrollPane;
JTable table;
DefaultTableModel dtm;
JButton btnExcel;
public GoodsPanel() {
//上
panelTop = new JPanel();
//上上
panelTop1 = new JPanel();
//上下
panelTop2 = new JPanel();
//下
panelBottom = new JPanel();
btnExcel = new JButton("导出Excel");
//
scrollPane = new JScrollPane();
table = new JTable();
dtm = new DefaultTableModel();
table.setModel(dtm);//关系
refresh();//调
//3.放
//最大的改成边界布局
setLayout(new BorderLayout());
//设置连界布局
panelTop.setLayout(new BorderLayout());
panelTop.add(panelTop1, BorderLayout.NORTH);
panelTop.add(panelTop2, BorderLayout.CENTER);
panelTop2.add(btnExcel);
//底部
scrollPane.setViewportView(table);
panelBottom.setLayout(new BorderLayout());
panelBottom.add(scrollPane);
add(panelTop, BorderLayout.NORTH);//北
add(panelBottom, BorderLayout.CENTER);
btnExcel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//导出Excel
new ExportExcel(table).export();
}
});
}
//造表头,也可以从数据库查,利用别名
Vector getCols() {
Vector cols = new Vector();
cols.add("编号");
cols.add("名称");
cols.add("条形码");
cols.add("价格");
cols.add("数量");
cols.add("位置");
cols.add("类型");
return cols;
}
//造表身
Vector<Vector> getRows() {
List<Goods> lists = service.findAll();
Vector<Vector> rows = new Vector<>();
if (lists != null && lists.size() > 0) {
for (Goods g : lists) {
Vector v = new Vector();
//装
v.add(g.getGoodsId());
v.add(g.getName());
v.add(g.getBarcode());
v.add(g.getPrice());
v.add(g.getNumber());
v.add(g.getPname());
v.add(g.getKname());
rows.add(v);
}
}
return rows;
}
//刷新
public void refresh() {
Vector cols = getCols();
Vector<Vector> rows = getRows();
dtm.setDataVector(rows, cols);
}
}
参照以前所学知识:完成以下功能