MVC设计模式:Model - View - Controller
View:接受用户录入的数据,传递给控制层。接收控制层返回的数据
Controller:接受View层的数据,同时调用相应的Model层进行数据处理,并接收处理的结果返回到View层。
Model:业务数据和业务逻辑处理,Web方面分为Service层和Dao层。
Service层:对Controller层传递的数据进行业务的处理,传递给Dao层,同时会接受Dao层处理过后的数据,进行业务逻辑处理后,返回给Contrller层
Dao层:与数据库交互,进行相关的业务处理
财务管理系统的主要结构,除了相应的Model,Controller,View接口以及接口实现的包外,还有domain包(存放JavaBean类),tools包(存放自定义工具类),app包(用于启动软件)
本项目使用的jar包,有dbcp连接池,dbutils工具包,mysql的驱动jar包
数据库使用的是Mysql5.5版本,下面是数据表
工具包tools 自定义JDBCUtils类,可以方便进行数据库的连接,采用的配置文件是propertie类型
package com.tools;
//tools工具包:存放项目中使用到的工具类
//JDBCUtils 自定义工具类,从配置文件中读取,数据库连接相关参数,返回连接池DataSource对象
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class JDBCUtils {
private static BasicDataSource bs = new BasicDataSource();
private static String classDriver;
private static String url;
private static String user;
private static String password;
private static int initialSize;
private static int maxActive;
private static int maxIdle;
private static int minIdle;
//设置连接池参数
static {
try {
read();
bs.setDriverClassName(classDriver);
bs.setUrl(url);
bs.setUsername(user);
bs.setPassword(password);
bs.setInitialSize(initialSize);
bs.setMaxActive(maxActive);
bs.setMaxIdle(maxIdle);
bs.setMinIdle(minIdle);
;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("数据库连接失败");
}
}
public static DataSource getDataSource() {
return bs;
}
//读取properties文件
private static void read() throws IOException {
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream(
"source.properties");
Properties p = new Properties();
p.load(is);
classDriver = p.getProperty("classDriver");
url = p.getProperty("url");
user = p.getProperty("username");
password = p.getProperty("password");
initialSize = Integer.parseInt(p.getProperty("initialSize"));
maxActive = Integer.parseInt(p.getProperty("maxActive"));
maxIdle = Integer.parseInt(p.getProperty("maxIdle"));
minIdle = Integer.parseInt(p.getProperty("minIdle"));
}
}
domain包,定义一个ZhangWu类,对数据进行包装,方便数据传递
package com.domain;
//domain实体包 存放需要用到的存放JavaBean类
public class ZhangWu {
private String zwid;
private String flname;
private double money;
private String zhanghu;
private String createTime;
private String description;
public ZhangWu() {
super();
}
public ZhangWu(String zwid, String flname, double money, String zhanghu,
String createTime, String description) {
super();
this.zwid = zwid;
this.flname = flname;
this.money = money;
this.zhanghu = zhanghu;
this.createTime = createTime;
this.description = description;
}
@Override
public String toString() {
return zwid + "\t\t" + flname + "\t\t" + money + "\t\t" + zhanghu + "\t\t"
+ createTime + "\t" + description;
}
public String getZwid() {
return zwid;
}
public void setZwid(String zwid) {
this.zwid = zwid;
}
public String getFlname() {
return flname;
}
public void setFlname(String flname) {
this.flname = flname;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getZhanghu() {
return zhanghu;
}
public void setZhanghu(String zhanghu) {
this.zhanghu = zhanghu;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package com.viewImpl;
//视图层(用于用户交互显示)
import java.util.Scanner;
import com.controllerImpl.ZhangWuController;
import com.domain.ZhangWu;
import com.view.View;
public class ZhangWuView implements View {
private ZhangWuController controller = new ZhangWuController();
private Scanner sc1 = new Scanner(System.in);
private Scanner sc2 = new Scanner(System.in);
@Override
public void run() {
while (true) {
System.out.println("-----------------欢迎使用管家账务系统----------------");
System.out.println("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统");
System.out.println("请输入要操作的功能序号[1-5]");
String select = sc2.nextLine();
switch (select) {
// 添加账务功能
case "1":
add();
break;
// 编辑账务功能
case "2":
update();
break;
// 删除账务功能
case "3":
delete();
break;
// 查询账务功能
case "4":
select();
break;
// 退出功能
case "5":
System.out.println("谢谢使用");
System.exit(0);
default:
System.out.println("功能序号输入错误,请重新输入");
break;
}
}
}
// 查询账务功能
@Override
public void select() {
System.out.println("1.查询所有 2.条件查询");
String select = sc2.nextLine();
switch (select) {
// 查询所有
case "1":
selectAll();
break;
// 条件查询
case "2":
selectCon();
break;
default:
System.out.println("功能输入错误");
}
}
// 按日期范围进行查询
private void selectCon() {
System.out.println("请输入开始日期(yyyy-MM-dd)");
String startTime = sc2.nextLine();
System.out.println("请输入结束日期(yyyy-MM-dd)");
String endTime = sc2.nextLine();
controller.selectCon(startTime, endTime);
}
//查询所有数据
private void selectAll() {
controller.selectAll();
}
// 添加账务功能
@Override
public void add() {
System.out.println("请输入类别:");
String flname = sc2.nextLine();
System.out.println("请输入金额:");
int money = sc1.nextInt();
System.out.println("请输入账户:");
String zhanghu = sc2.nextLine();
System.out.println("请输入日期(yyyy-MM-dd)");
String createTime = sc2.nextLine();
System.out.println("请输入说明:");
String description = sc2.nextLine();
ZhangWu zhangwu = new ZhangWu(null, flname, money, zhanghu, createTime,
description);
controller.add(zhangwu);
}
// 编辑账务功能
@Override
public void update() {
selectAll();
System.out.println("请输入您要修改的ID");
int id = sc1.nextInt();
controller.update(id);
}
// 查询账务功能
@Override
public void delete() {
selectAll();
System.out.println("请输入您要删除的ID");
int id = sc1.nextInt();
controller.delete(id);
}
}
package com.controllerImpl;
//controller层作用: 控制层 (获取界面上的数据,为界面设置数据; 将要实现的功能交给业务层处理)
import java.util.List;
import com.controller.Controller;
import com.domain.ZhangWu;
import com.serviceImpl.ZhangWuService;
public class ZhangWuController implements Controller{
private ZhangWuService service = new ZhangWuService();
//全部查询 显示查询结果
@Override
public void selectAll() {
List list = service.selectAll();
System.out.println("ID\t\t类别\t\t金额\t\t账户\t\t时间\t\t说明");
for (ZhangWu zhangWu : list) {
System.out.println(zhangWu);
}
}
//按日期范围查询 显示查询结果
@Override
public void selectCon(String startTime, String endTime) {
List list = service.selectCon(startTime, endTime);
System.out.println("ID\t\t类别\t\t金额\t\t账户\t\t时间\t\t说明");
for (ZhangWu zhangWu : list) {
System.out.println(zhangWu);
}
}
//添加数据,并显示是否添加成功
@Override
public void add(ZhangWu zhangwu) {
boolean flag = service.add(zhangwu);
if(flag){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
//修改数据,并显示是否修改成功
@Override
public void update(int id) {
boolean flag = service.update(id);
if(flag){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
}
//删除数据,并显示是否删除成功
@Override
public void delete(int id) {
boolean flag = service.delete(id);
if(flag){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
}
package com.serviceImpl;
/*service层作用: 业务层, 功能的实现, 与controller控制层和数据访问层DAO交互,
将对数据库的操作交给DAO数据访问层来处理*/
import java.util.List;
import com.daoImpl.ZhangWuDao;
import com.domain.ZhangWu;
import com.service.Service;
public class ZhangWuService implements Service {
private ZhangWuDao dao = new ZhangWuDao();
// 查询所有数据
@Override
public List selectAll() {
return dao.selectAll();
}
// 按日期查询
@Override
public List selectCon(String startTime, String endTime) {
return dao.selectCon(startTime, endTime);
}
// 添加数据
@Override
public boolean add(ZhangWu zhangwu) {
return dao.add(zhangwu);
}
// 更新数据
@Override
public boolean update(int id) {
return dao.update(id);
}
// 删除数据
@Override
public boolean delete(int id) {
return dao.delete(id);
};
}
package com.daoImpl;
//dao层作用: 数据访问层, 用来操作数据库表的数据
//采用DBUtils工具类 简化数据库的操作
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.dao.Dao;
import com.domain.ZhangWu;
import com.tools.JDBCUtils;
public class ZhangWuDao implements Dao {
private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
private Scanner sc1 = new Scanner(System.in);
private Scanner sc2 = new Scanner(System.in);
// 与数据库交互,查询表中的所有数据
@Override
public List selectAll() {
try {
String sql = "SELECT * FROM gjp_zhangwu";
List list = qr.query(sql, new BeanListHandler(
ZhangWu.class));
return list;
} catch (SQLException e) {
throw new RuntimeException("查询失败");
}
}
// 与数据库交互,按照时间范围查询数据
@Override
public List selectCon(String startTime, String endTime) {
try {
String sql = "SELECT * FROM gjp_zhangwu WHERE createTime BETWEEN ? AND ?";
Object[] params = { startTime, endTime };
List list = qr.query(sql, new BeanListHandler(
ZhangWu.class), params);
return list;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("查询失败");
}
}
// 与数据库交互,添加数据
@Override
public boolean add(ZhangWu zhangwu) {
try {
String sql = "INSERT INTO gjp_zhangwu VALUES (?,?,?,?,?,?)";
Object[] param = { zhangwu.getZwid(), zhangwu.getFlname(),
zhangwu.getMoney(), zhangwu.getZhanghu(),
zhangwu.getCreateTime(), zhangwu.getDescription() };
int row = qr.update(sql, param);
if (row != 0) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("添加失败");
}
}
// 查询id是否在表中存在
private boolean select(int id) {
String sql = "SELECT zwid FROM gjp_zhangwu WHERE zwid = ?";
try {
int row = qr.query(sql, new ScalarHandler(), id);
} catch (Exception e) {
System.out.println("该id不存在");
return false;
}
return true;
}
// 与数据库交互,更新数据
@Override
public boolean update(int id) {
// 若id存在,修改数据
if (select(id)) {
String sql = "UPDATE gjp_zhangwu SET flname = ?,money=?,zhanghu=?,createTime=?,description=? WHERE zwid = ?";
System.out.println("请输入类别:");
String flname = sc2.nextLine();
System.out.println("请输入金额:");
int money = sc1.nextInt();
System.out.println("请输入账户:");
String zhanghu = sc2.nextLine();
System.out.println("请输入日期(yyyy-MM-dd):");
String createTime = sc2.nextLine();
System.out.println("请输入说明:");
String description = sc2.nextLine();
Object[] params = { flname, money, zhanghu, createTime,
description, id };
try {
qr.update(sql, params);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("修改失败");
}
return true;
} else {
return false;
}
}
// 与数据库交互,删除数据
@Override
public boolean delete(int id) {
// 若id存在,删除数据
if (select(id)) {
String sql = "DELETE FROM gjp_zhangwu WHERE zwid = ?";
try {
qr.update(sql, id);
return true;
} catch (SQLException e) {
return false;
}
} else {
return false;
}
}
}
package com.app;
//启动软件界面(测试界面)
import com.viewImpl.ZhangWuView;
public class Test {
private static ZhangWuView view = new ZhangWuView();
public static void main(String[] args) {
view.run();
}
}