满汉楼代码与分析

需求说明
要完成的满汉楼项目说明
满汉楼项目功能多,界面复杂,涉及到复杂的awt和swing 技术和事件编程,做如下调整:
1.去掉界面和事件处理(工作中使用很少),使用控制台界面
2.完成满汉楼项目的登录、订座、点餐和结账、查看账单等功能
3.在实际工作中,独立完成项目新功能常重要,这是锻炼编程能力和思想的重要途径

在这里插入图片描述

功能实现
准备工具类Utility,提高开发效率,并搭建项目的整体结构
在实际开发中,公司都会提供相应的工具类和开发库,可以提高开发效率1.了解Utility类的使用
2.测试Utility类
在这里插入图片描述

use dxl_db01;
create table employee(
	id int primary key auto_increment,
	empId varchar(50) not null default '',
	pwd char(32) not null default '',
	name varchar(50) not null default '',
	job varchar(50) not null default ''
)charset = utf8;

在这里插入图片描述

create table diningTable(
	id int primary key auto_increment,
	state varchar(20) not null default '',
	orderName varchar(50) not null default '',
	orderTel varchar(20) not null default ''
)charset = utf8; 

insert into diningtable values(null,'空','','');
insert into diningtable values(null,'空','','');
insert into diningtable values(null,'空','','');
insert into diningtable values(null,'空','','');

在这里插入图片描述
在这里插入图片描述

create table menu(
	id int primary key auto_increment,
	name varchar(50) not null default '',
	type varchar(50) not null default '',
	price double not null default 0
)charset = utf8;

insert into menu values(null,'八宝饭','主食',10);
insert into menu values(null,'叉烧包','主食',20);
insert into menu values(null,'宫保鸡丁','热菜',30);
insert into menu values(null,'凉拌黄瓜','凉菜',12);

在这里插入图片描述

create table bill(
	id int primary key auto_increment,
	billId varchar(50) not null default '',
	menuId int not null default 0,
	nums int not null default 0,
	money double not null default 0,
	diningTableId int not null default 0,
	billDate datetime not null,
	state varchar(50) not null default ''
)charset = utf8;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package com.mhl.view;

import com.mhl.domain.*;
import com.mhl.service.BillService;
import com.mhl.service.DiningTableService;
import com.mhl.service.EmployeeService;
import com.mhl.service.MeunService;
import com.mhl.utils.Utility;

import java.util.List;

/**
 * @author
 * @version 1.0
 */
public class MHLView {

//    控制是否退出菜单
    private boolean loop = true;
    private String key = "";//接收用户的选择
    private EmployeeService employeeService = new EmployeeService();
//    定义一个DiningTableService 的属性
    private DiningTableService diningTableService = new DiningTableService();
    private MeunService meunService = new MeunService();
    private BillService billService = new BillService();

    public static void main(String[] args) {
        MHLView mhlView = new MHLView();
        mhlView.mainMenu();
    }

//    显示主菜单
    public void mainMenu(){
        while(loop){
            System.out.println("==============满汉楼=============");
            System.out.println("\t\t 1 登录满汉楼");
            System.out.println("\t\t 2 退出满汉楼");
            System.out.println("请输入你的选择:");
            key = Utility.readString(1);
            switch (key){
                case "1":
                    System.out.println("请输入员工号:");
                    String empId = Utility.readString(50);
                    System.out.println("请输入密  码:");
                    String pwd = Utility.readString(50);
                    Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);

//                    到数据库判断
                    if (employee != null){
                        System.out.println("============登录成功===============");
//                        显示二级菜单 循环操作用while
                        while(loop){
                            System.out.println("====================满汉楼(二级菜单)====================");
                            System.out.println("\t\t 1 显示餐桌状态");
                            System.out.println("\t\t 2 预定餐桌");
                            System.out.println("\t\t 3 显示所有菜品");
                            System.out.println("\t\t 4 点餐服务");
                            System.out.println("\t\t 5 查看账单");
                            System.out.println("\t\t 6 结账");
                            System.out.println("\t\t 9 退出满汉楼");
                            System.out.println("请输入你的选择:");
                            key = Utility.readString(1);
                            switch (key){
                                case "1":
                                    listDiningTable();
                                    break;
                                case "2":
                                    orderDiningTable();
                                    break;
                                case "3":
                                    listMenu();
                                    break;
                                case "4":
                                    orderMenu();
                                    break;
                                case "5":
                                    listBill();
                                    break;
                                case "6":
                                    payBill();
                                    break;
                                case "9":
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("你的输入有误,请重新输入!");
                                    break;
                            }
                        }
                    }else{
                        System.out.println("============登录失败===============");
                    }
                    break;
                case "2" :
                    System.out.println("退出满汉楼!");
                    loop = false;
                    break;
                default:
                    System.out.println("输入不正确!");
            }
        }
    }

//      显示所有餐桌状态
    public void listDiningTable(){
        List<DiningTable> list = diningTableService.list();
        System.out.println("\n餐桌编号\t\t餐桌状态");
        for (DiningTable diningTable:list){
            System.out.println(diningTable);
        }
        System.out.println("============显示完毕===========");
    }

//    预定餐桌
    public void orderDiningTable(){
        System.out.println("===============预定餐桌==============");
        System.out.println("请选择要预定的餐桌号(-1表示退出):");
        int orderId = Utility.readInt();
        if (orderId == -1){
            System.out.println("===========取消预定餐桌============");
            return;
        }
//        该方法得到的是 Y 或 N
        char key = Utility.readConfirmSelection();
        if (key == 'Y'){
//            根据对应的 orderId 返回对应的DiningTable对象,如果为null,说明该对象不存在
            DiningTable diningTable = diningTableService.getDiningTableById(orderId);
            if (diningTable == null){
                System.out.println("=================预定餐桌不存在================");
                return;
            }
//            判断该餐桌的状态是否为“空”
            if (!("空".equals(diningTable.getState()))){//说明当前餐桌不为空
                System.out.println("=============该餐桌已经预定或在就餐中============");
                return;
            }
//            这是说明可以预定  更新餐桌状态
            System.out.println("预订人的名字:");
            String orderName = Utility.readString(50);
            System.out.println("预定人的电话:");
            String orderTel = Utility.readString(50);
            if (diningTableService.orderDiningTable(orderId,orderName,orderTel)) {
                System.out.println("============预定餐桌成功===========");
            }else {
                System.out.println("===================预定餐桌失败==========");
            }
        }else{
            System.out.println("===============取消预定餐桌==============");
        }
    }

//    显示所有菜品
    public void listMenu(){
        List<Menu> list = meunService.list();
        System.out.println("\n菜品编号\t菜品名\t类别\t价格");
        for (Menu menu:list){
            System.out.println(menu);
        }
        System.out.println("===============显示完毕==========");
    }

//    点餐服务
    public void orderMenu(){
        System.out.println("============点餐服务==========");
        System.out.println("请输入点餐的桌号(-1退出):");
        int orderDiningTableId = Utility.readInt();
        if (orderDiningTableId == -1){
            System.out.println("============取消点餐============");
            return;
        }
        System.out.println("请输入点餐的菜品编号(-1退出):");
        int orderMenuId = Utility.readInt();
        if (orderMenuId == -1){
            System.out.println("============取消点餐============");
            return;
        }
        System.out.println("请输入点餐的菜品量(-1退出):");
        int orderNums = Utility.readInt();
        if (orderNums == -1){
            System.out.println("============取消点餐============");
            return;
        }

//        验证餐桌号是否存在
        DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
        if (diningTable == null){
            System.out.println("=============餐桌号不存在================");
            return;
        }
//        验证菜品编号
        Menu menu = meunService.getMenuById(orderMenuId);
        if (menu == null){
            System.out.println("==============菜品不存在============");
            return;
        }
//       点餐
        if (billService.orderMenu(orderMenuId,orderNums,orderDiningTableId)){
            System.out.println("===================点餐成功==============");
        }else{
            System.out.println("===================点餐失败==============");
        }
    }

//    显示账单的信息
//    public void listBill(){
//        List<Bill> Bills = billService.list();
//        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t状态");
//        for (Bill bill: Bills){
//            System.out.println(bill);
//        }
//        System.out.println("====================显示完毕=====================");
//    }

//    显示账单信息,带有菜品名
    public void listBill(){
        List<MultTableBean> multTableBeans = billService.list2();
        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t状态\t\t\t菜品名\t\t价格");
        for (MultTableBean multTableBean: multTableBeans){
            System.out.println(multTableBean);
        }
        System.out.println("====================显示完毕=====================");
    }


    //    完成结账
    public void payBill(){
        System.out.println("==========================结账服务========================");
        System.out.println("请选择要结账的餐桌编号(-1退出):");
        int diningTableId = Utility.readInt();
        if (diningTableId == -1){
            System.out.println("=====================取消结账=======================");
            return;
        }
//        验证餐桌是否存在
        DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);
        if (diningTable == null){
            System.out.println("========================结账的餐桌号不存在========================");
            return;
        }
//        验证餐桌是否有需要结账的菜单
        if (!billService.hasPayBillByDiningTableId(diningTableId)){
            System.out.println("=====================该餐桌没有未结账菜单================");
            return;
        }
        System.out.println("结账方式(现金/支付宝/微信)回车表示退出:");
        String payMode = Utility.readString(20, "");//如果回车,就返回“”
        if ("".equals(payMode)) {
            System.out.println("=======================取消结账=====================");
            return;
        }
        char key = Utility.readConfirmSelection();
        if (key == 'Y'){//结账
//            调用方法
            if(billService.payBill(diningTableId,payMode)){
                System.out.println("==================结账成功===============");
            }else{
                System.out.println("================结账失败==============");
            }
        }else{
            System.out.println("=======================取消结账=====================");
        }
    }
}

package com.mhl.utils;

/**
 * @author
 * @version 1.0
 * 工具类的作用:
 *  处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
 */

import java.util.*;
/**

 */
public class Utility {
    //静态属性。。。
    private static Scanner scanner = new Scanner(System.in);


    /**
     * 功能:读取键盘输入的一个菜单选项,值:1——5的范围
     * @return 1——5
     */
    public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);//包含一个字符的字符串
            c = str.charAt(0);//将字符串转换成字符char类型
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }

    /**
     * 功能:读取键盘输入的一个字符
     * @return 一个字符
     */
    public static char readChar() {
        String str = readKeyBoard(1, false);//就是一个字符
        return str.charAt(0);
    }
    /**
     * 功能:读取键盘输入的一个字符,如果直接按回车,则返回指定的默认值;否则返回输入的那个字符
     * @param defaultValue 指定的默认值
     * @return 默认值或输入的字符
     */

    public static char readChar(char defaultValue) {
        String str = readKeyBoard(1, true);//要么是空字符串,要么是一个字符
        return (str.length() == 0) ? defaultValue : str.charAt(0);
    }

    /**
     * 功能:读取键盘输入的整型,长度小于2位
     * @return 整数
     */
    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, false);//一个整数,长度<=10位
            try {
                n = Integer.parseInt(str);//将字符串转换成整数
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
    /**
     * 功能:读取键盘输入的 整数或默认值,如果直接回车,则返回默认值,否则返回输入的整数
     * @param defaultValue 指定的默认值
     * @return 整数或默认值
     */
    public static int readInt(int defaultValue) {
        int n;
        for (; ; ) {
            String str = readKeyBoard(10, true);
            if (str.equals("")) {
                return defaultValue;
            }

            //异常处理...
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }

    /**
     * 功能:读取键盘输入的指定长度的字符串
     * @param limit 限制的长度
     * @return 指定长度的字符串
     */

    public static String readString(int limit) {
        return readKeyBoard(limit, false);
    }

    /**
     * 功能:读取键盘输入的指定长度的字符串或默认值,如果直接回车,返回默认值,否则返回字符串
     * @param limit 限制的长度
     * @param defaultValue 指定的默认值
     * @return 指定长度的字符串
     */

    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("")? defaultValue : str;
    }


    /**
     * 功能:读取键盘输入的确认选项,Y或N
     * 将小的功能,封装到一个方法中.
     * @return Y或N
     */
    public static char readConfirmSelection() {
        System.out.println("确认是否预定(Y/N): 请小心选择");
        char c;
        for (; ; ) {//无限循环
            //在这里,将接受到字符,转成了大写字母
            //y => Y n=>N
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");
            }
        }
        return c;
    }

    /**
     * 功能: 读取一个字符串
     * @param limit 读取的长度
     * @param blankReturn 如果为true ,表示 可以读空字符串。
     * 					  如果为false表示 不能读空字符串。
     *
     *	如果输入为空,或者输入大于limit的长度,就会提示重新输入。
     * @return
     */
    private static String readKeyBoard(int limit, boolean blankReturn) {

        //定义了字符串
        String line = "";

        //scanner.hasNextLine() 判断有没有下一行
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();//读取这一行

            //如果line.length=0, 即用户没有输入任何内容,直接回车
            if (line.length() == 0) {
                if (blankReturn) return line;//如果blankReturn=true,可以返回空串
                else continue; //如果blankReturn=false,不接受空串,必须输入内容
            }

            //如果用户输入的内容大于了 limit,就提示重写输入
            //如果用户如的内容 >0 <= limit ,我就接受
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不能大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}

package com.mhl.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author
 * @version 1.0
 * 基于Druid数据连接池的工具类
 */
public class JDBCUtilsByDruid {

    private static DataSource ds;

//    在静态代码块完成 ds初始化
    static{
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("src\\druid.properties"));
        ds = DruidDataSourceFactory.createDataSource(properties);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    }

//      编写getConnection 方法
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

//    关闭连接,在数据库连接池技术中的关闭连接,close 不是真的断掉连接
//    而是把使用的Connection 对象放回连接池
    public static void close(ResultSet resultSet, Statement statement, Connection connection){
            try {
                if (resultSet != null){
                    resultSet.close();
                }
                if (statement != null){
                    statement.close();
                }
                if (connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
    }
}

package com.mhl.service;

import com.mhl.dao.MenuDAO;
import com.mhl.domain.Menu;

import java.util.List;

/**
 * @author
 * @version 1.0
 * 完成menu 表的各种操作(通过调用MenuDAO完成)
 */
public class MeunService {
    private MenuDAO menuDAO = new MenuDAO();

//    方法返回所有菜品
    public List<Menu> list(){
        return menuDAO.queryMulti("select * from menu",Menu.class);
    }

//    根据id 返回Menu 对象
    public Menu getMenuById(int id){
        return (Menu) menuDAO.querySingle("select * from menu where id = ?", Menu.class, id);
    }
}

package com.mhl.service;

import com.mhl.dao.EmployeeDAO;
import com.mhl.domain.Employee;

/**
 * @author
 * @version 1.0
 * 业务层 通过调用EmployeeDAO完成
 */
public class EmployeeService {

//    定义一个 EmployeeDAO对象 属性
    private EmployeeDAO employeeDAO = new EmployeeDAO();

//    方法,根据empId 和 pwd 返回一个 Employee 对象
//    如果查询不到,就返回null
    public Employee getEmployeeByIdAndPwd(String empId,String pwd){
        Employee employee = (Employee) employeeDAO.querySingle("select * from employee where empId = ? and pwd = md5(?)", Employee.class, empId, pwd);
        return employee;
    }
}

package com.mhl.service;

import com.mhl.dao.DiningTableDAO;
import com.mhl.domain.DiningTable;

import java.util.List;

/**
 * @author
 * @version 1.0
 * 技巧:sql语句先通过查询分析器
 */
public class DiningTableService {
//定义一个 DiningTableDAO 对象
    private DiningTableDAO diningTableDAO = new DiningTableDAO();

//    返回所有餐桌的信息
    public List<DiningTable>  list(){
       return diningTableDAO.queryMulti("select id ,state from diningtable", DiningTable.class, null);
    }

//    根据id,查询对应的餐桌 DiningTable 对象
//    如果返回null, 表示id编号对应的餐桌不存在
    public DiningTable getDiningTableById(int id){
        return (DiningTable) diningTableDAO.querySingle("select * from diningtable where id = ?",DiningTable.class,id);
    }

//    如果餐桌可以预定,调用该方法,对其状态进行更新(包括预定人的电话和名字)
    public boolean orderDiningTable(int id,String orderName,String orderTel){
        int update = diningTableDAO.update("update diningtable set state='已经预定',orderName=?,orderTel=? where id=?", orderName, orderTel, id);
        return update > 0;
    }

//     需要提供一个更新 餐桌状态的方法
    public boolean updateDiningTableState(int id, String state){
        int update = diningTableDAO.update("update diningtable set state= ? where id =?", state, id);
        return update > 0;
    }

//    提供方法,将指定餐桌设置为空闲状态
    public boolean updateDiningTableToFree(int id, String state){
        int update = diningTableDAO.update("update diningTable set state=?,orderName='',orderTel='' where id=?", state, id);
        return update > 0;
    }
}
package com.mhl.service;

import com.mhl.dao.BillDAO;
import com.mhl.dao.MultiTabDAO;
import com.mhl.domain.Bill;
import com.mhl.domain.MultTableBean;

import java.util.List;
import java.util.UUID;

/**
 * @author
 * @version 1.0
 * 处理和账单相关的业务逻辑
 */
public class BillService {
//    定义BillDAO属性
    private BillDAO billDAO = new BillDAO();
//    定义一个MenuService属性
    private MeunService meunService = new MeunService();
    private DiningTableService diningTableService = new DiningTableService();
    private MultiTabDAO multiTabDAO = new MultiTabDAO();

//    生成账单
//    需要更新对应餐桌的状态
    public boolean orderMenu(int menuId, int nums, int diningTableId){
//        生成一个账单号,UUID
        String billId = UUID.randomUUID().toString();

//        将账单生成到bill 表,直接计算账单金额
        int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')",
                billId, menuId, nums, meunService.getMenuById(menuId).getPrice() * nums, diningTableId);
        if (update <= 0){
            return false;
        }

//        需要更新对应餐桌的状态
        return diningTableService.updateDiningTableState(diningTableId,"就餐中");
    }

//    返回所有的账单,提供给 view 调用
    public List<Bill> list(){
        return billDAO.queryMulti("select * from bill",Bill.class);
    }

//    返回所有账单并带有菜品名
    public List<MultTableBean> list2(){
        return multiTabDAO.queryMulti("select bill.*,name,price "
                + "from bill,menu " + "where bill.menuId = menu.id",MultTableBean.class);
    }

//    查看某个餐桌是够有未结账的菜单
    public boolean hasPayBillByDiningTableId(int diningTableId){
        Bill bill = (Bill) billDAO.querySingle("select * from bill where diningTableId=? and state='未结账' limit 0,1",Bill.class,diningTableId);
        return bill != null;
    }

//    完成结账(如果餐桌存在,并且该餐桌有未结账的账单)
    public boolean payBill(int diningTableId,String payMode){
//        如果这里使用事务,需要你使用ThreadLocal来解决,框架中比如mybatis 提供了事务支持
//        1.修改bill表
        int update = billDAO.update("update bill set state=? where diningTableId=? and state='未结账'", payMode, diningTableId);
        if (update <= 0){
            return false;
        }
//        2.修改diningTable表
//        不直接在这里操作,而是在DiningTable,而应调用DiningTableService 方法
        if (!diningTableService.updateDiningTableToFree(diningTableId,"空")){
            return false;
        }
        return true;
    }
}

package com.mhl.domain;

import java.util.Date;

/**
 * @author
 * @version 1.0
 * 这是一个Javabean  可以和多张表进行对应
 */
public class MultTableBean {
    private Integer id;
    private String billId;
    private Integer menuId;
    private Integer nums;
    private Double money;
    private Integer diningTableId;
    private Date billDate;
    private String state;

//    增加一个来自menu表的name
//    可以不一致,但是需要修改sql语句,规范保持一致
    private String name;

//    来自menu表的price
    private Double price;

    public MultTableBean() {
    }

    public MultTableBean(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state, String name, Double price) {
        this.id = id;
        this.billId = billId;
        this.menuId = menuId;
        this.nums = nums;
        this.money = money;
        this.diningTableId = diningTableId;
        this.billDate = billDate;
        this.state = state;
        this.name = name;
        this.price = price;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBillId() {
        return billId;
    }

    public void setBillId(String billId) {
        this.billId = billId;
    }

    public Integer getMenuId() {
        return menuId;
    }

    public void setMenuId(Integer menuId) {
        this.menuId = menuId;
    }

    public Integer getNums() {
        return nums;
    }

    public void setNums(Integer nums) {
        this.nums = nums;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Integer getDiningTableId() {
        return diningTableId;
    }

    public void setDiningTableId(Integer diningTableId) {
        this.diningTableId = diningTableId;
    }

    public Date getBillDate() {
        return billDate;
    }

    public void setBillDate(Date billDate) {
        this.billDate = billDate;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

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

    @Override
    public String toString() {
        return id + "\t\t" + menuId + "\t\t" + nums + "\t\t" + money + "\t\t"
                + diningTableId + "\t\t" + billDate + "\t\t" + state + "\t\t"
                + name + "\t\t" + price;
    }
}

package com.mhl.domain;

/**
 * @author
 * @version 1.0
 */
public class Menu {
    private Integer id;
    private String name;
    private String type;
    private Double price;

    public Menu() {
    }

    public Menu(Integer id, String name, String type, Double price) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.price = price;
    }

    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 String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return  id + "\t\t" + name + "\t\t" + type + "\t\t" + price;
    }
}

package com.mhl.domain;

/**
 * @author
 * @version 1.0
 * javabean 和employee表对应
 */
public class Employee {
    private Integer id;
    private String empId;
    private String pwd;
    private String name;
    private String job;

    public Employee() {
    }

    public Employee(Integer id, String empId, String pwd, String name, String job) {
        this.id = id;
        this.empId = empId;
        this.pwd = pwd;
        this.name = name;
        this.job = job;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", empId='" + empId + '\'' +
                ", pwd='" + pwd + '\'' +
                ", name='" + name + '\'' +
                ", job='" + job + '\'' +
                '}';
    }
}

package com.mhl.domain;

/**
 * @author
 * @version 1.0
 */
public class DiningTable {
    private Integer id;
    private String state;
    private String orderName;
    private String orderTel;

    public DiningTable() {
    }

    public DiningTable(Integer id, String state, String orderName, String orderTel) {
        this.id = id;
        this.state = state;
        this.orderName = orderName;
        this.orderTel = orderTel;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public String getOrderTel() {
        return orderTel;
    }

    public void setOrderTel(String orderTel) {
        this.orderTel = orderTel;
    }

    @Override
    public String toString() {
        return id + "\t\t\t" + state;
    }
}

package com.mhl.domain;

import java.util.Date;

/**
 * @author
 * @version 1.0
 */
public class Bill {
    private Integer id;
    private String billId;
    private Integer menuId;
    private Integer nums;
    private Double money;
    private Integer diningTableId;
    private Date billDate;
    private String state;

    public Bill() {
    }

    public Bill(Integer id, String billId, Integer menuId, Integer nums, Double money, Integer diningTableId, Date billDate, String state) {
        this.id = id;
        this.billId = billId;
        this.menuId = menuId;
        this.nums = nums;
        this.money = money;
        this.diningTableId = diningTableId;
        this.billDate = billDate;
        this.state = state;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBillId() {
        return billId;
    }

    public void setBillId(String billId) {
        this.billId = billId;
    }

    public Integer getMenuId() {
        return menuId;
    }

    public void setMenuId(Integer menuId) {
        this.menuId = menuId;
    }

    public Integer getNums() {
        return nums;
    }

    public void setNums(Integer nums) {
        this.nums = nums;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Integer getDiningTableId() {
        return diningTableId;
    }

    public void setDiningTableId(Integer diningTableId) {
        this.diningTableId = diningTableId;
    }

    public Date getBillDate() {
        return billDate;
    }

    public void setBillDate(Date billDate) {
        this.billDate = billDate;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return id + "\t\t" + menuId + "\t\t" + nums + "\t\t" + money + "\t\t"
                + diningTableId + "\t\t" + billDate + "\t\t" + state ;
    }
}

package com.mhl.dao;

import com.mhl.domain.MultTableBean;

/**
 * @author
 * @version 1.0
 */
public class MultiTabDAO extends BasicDAO<MultTableBean>{
}

package com.mhl.dao;

/**
 * @author
 * @version 1.0
 */
public class MenuDAO extends BasicDAO{
}

package com.mhl.dao;

/**
 * @author
 * @version 1.0
 */
public class EmployeeDAO extends BasicDAO{

}

package com.mhl.dao;

/**
 * @author
 * @version 1.0
 */
public class DiningTableDAO extends BasicDAO{
}

package com.mhl.dao;

/**
 * @author
 * @version 1.0
 */
public class BillDAO extends BasicDAO{

}

package com.mhl.dao;


import com.mhl.utils.JDBCUtilsByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author
 * @version 1.0
 * 开发BasicDAO,是其他DAO的父类
 */
public class BasicDAO<T> {//泛型 指定具体类型
    private QueryRunner qr = new QueryRunner();

//    开发通用的dml,针对任意的表
    public int update(String sql,Object... parameters){
        Connection connection = null;

        try {
            connection = JDBCUtilsByDruid.getConnection();
            int update = qr.update(connection, sql, parameters);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);//将编译异常转成运行异常
        }finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }
    }

//    返回多个对象(机查询的结果是多行的),针对任意表

    /**
     *
     * @param sql sql语句,可以有?
     * @param clazz 传入一个类的Class对象比如 Actor.class
     * @param parameters 传入?的具体的值,可以是多个
     * @return 根据Actor.class 返回对应的 ArrayList集合
     */
    public List<T> queryMulti(String sql,Class<T> clazz, Object... parameters){

        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }

    }

//   查询单行结果
    public T querySingle(String sql, Class<T> clazz, Object... parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return qr.query(connection,sql, new BeanHandler<T>(clazz), parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }
    }

//     查询单行单列的方法,即返回单值的方法
    public Object queryScalar(String sql,Object... parameters){

        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return qr.query(connection,sql,new ScalarHandler(),parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtilsByDruid.close(null,null,connection);
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值