From 312c414c6b8f5f175a3d42411d0f9da5ef3e851f Mon Sep 17 00:00:00 2001 From: twinkle255 Date: Fri, 5 May 2023 23:09:56 +0800 Subject: [PATCH] =?UTF-8?q?Bug=20fixed=EF=BC=8C=E8=B0=83=E6=95=B4=E4=BA=86?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=BB=93=E6=9E=84=E3=80=82=E8=BF=98=E6=9C=89?= =?UTF-8?q?=E5=B0=B1=E6=98=AF=E6=88=91=E6=81=A8DDL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 71 ++------ log.md | 67 ++++++++ src/main/java/dao/IndentAbstractDAO.java | 5 +- .../java/dao/specification/abstractDAO.java | 5 +- .../java/dao/specification/indentDAO.java | 34 ++++ .../java/dao/specification/indentItemDAO.java | 4 + .../java/dao/specification/merchantsDAO.java | 1 + src/main/java/dao/specification/userDAO.java | 4 +- src/main/java/entities/Indent.java | 6 +- src/main/java/entities/indentItem.java | 2 +- src/main/java/example/App.java | 6 +- src/main/java/example/shellUI.java | 157 ------------------ src/main/java/util/SQLDatabaseConnection.java | 14 +- src/main/java/view/MerchantsView.java | 61 +++++++ src/main/java/view/UserView.java | 61 +++++++ src/main/java/view/shellUI.java | 51 ++++++ 16 files changed, 317 insertions(+), 232 deletions(-) create mode 100644 log.md create mode 100644 src/main/java/dao/specification/indentDAO.java create mode 100644 src/main/java/dao/specification/indentItemDAO.java delete mode 100644 src/main/java/example/shellUI.java create mode 100644 src/main/java/view/MerchantsView.java create mode 100644 src/main/java/view/UserView.java create mode 100644 src/main/java/view/shellUI.java diff --git a/README.md b/README.md index eac19f6..171ac54 100644 --- a/README.md +++ b/README.md @@ -1,56 +1,17 @@ -# Homework-entities -*网安实训作业* +# Homework +- - - +**Last Update Time : 2023-05-05** -# 2023-03-16 -- 初始化项目 - -# 2023-03-22 -- 增加实体类 -- 删除.idea文件 - -# 2023-03-26 -- 完善实体类 -- 我靠,折腾了一晚上,提交了十几次,终于提交时把.idea这个文件给忽略了 - - 方法:在`.gitgnore`文件中添加 - - `.idea/encodings.xml` - - `.idea/misc.xml` - - `.idea/vcs.xml` - - `.idea/workspace.xml` - - 然后在控制台输入`git rm -r --cached .idea` - - 删除`gitee`或`github`上的`.idea`文件 - - `update`一下远端项目 - - `push`代码 -- 我好菜。。。 - -# 2023-03-27 -- 完善了一下实体类,发现商家那一部分漏了一点东西,给它补上了 -- 另外就是为什么`.idea`文件我已经取消提交了,为啥本地的`commit`里面还会有有啊……无语了 -- 是不是跟`git`八字不合。。。 - -# 2023-03-29 -- 添加`JDBC`驱动,连接数据库 - -# 2023-05-04 -- 创建数据库表,测试连接成功 -- 调整项目结构 - - 调整实体类与数据库对应的结构 - - 修改外键 -- 完成DAO层抽象接口 - - 完成MerchantsDAO和UserDAO具体功能 - -# 2023-05-05-NO.1 -- Bug fixed: MerchantsDAO 按ID查询功能 -- 搭建shellUI的大体框架 -- 完成UserDAO具体功能 - -# 2023-05-05-NO.2 -- 实现UserDAO和MerchantsDAO所有接口功能 -- shellUI完成 -- 测试 - - UserDAO和MerchantsDAO的增加、查询、删除功能测试通过 - - shellUI功能测试通过 -- Bug Fixed - - DAO层Merchants、User类查询问题 - - 按ID查询报错 - - 全部查询只显示最后添加的行 - - 输出格式混乱问题 \ No newline at end of file +日志文件:`log.md` +- - - +### 运行方式: +```text +src -> main -> java -> example -> App.run() +``` +### 待修复bug +- `userDAO`和`merchantsDAO`中`update`语句未传参 +- 未测试非法数据 +### 待完成功能 +- `indentDAO`和`indentItemDAO`方法实现 +- 联合查询 +- `updata`方法更新指定字段 \ No newline at end of file diff --git a/log.md b/log.md new file mode 100644 index 0000000..44280d8 --- /dev/null +++ b/log.md @@ -0,0 +1,67 @@ +# Homework + +*网安实训作业* + +## 2023-05-05-NO.2 + +- 实现UserDAO和MerchantsDAO所有接口功能 +- shellUI完成 +- 测试 + - UserDAO和MerchantsDAO的增加、查询、删除功能测试通过 + - shellUI功能测试通过 +- Bug Fixed + - DAO层Merchants、User类查询问题 + - 按ID查询报错 + - 全部查询只显示最后添加的行 + - 输出格式混乱问题 + - 我恨DDL + +## 2023-05-05-NO.1 + +- Bug fixed: MerchantsDAO 按ID查询功能 +- 搭建shellUI的大体框架 +- 完成UserDAO具体功能 + +## 2023-05-04 + +- 创建数据库表,测试连接成功 +- 调整项目结构 + - 调整实体类与数据库对应的结构 + - 修改外键 +- 完成DAO层抽象接口 + - 完成MerchantsDAO和UserDAO具体功能 + - + +## 2023-03-29 + +- 添加`JDBC`驱动,连接数据库 + +## 2023-03-27 + +- 完善了一下实体类,发现商家那一部分漏了一点东西,给它补上了 +- 另外就是为什么`.idea`文件我已经取消提交了,为啥本地的`commit`里面还会有有啊……无语了 +- 是不是跟`git`八字不合。。。 + +## 2023-03-26 + +- 完善实体类 +- 我靠,折腾了一晚上,提交了十几次,终于提交时把.idea这个文件给忽略了 + - 方法:在`.gitgnore`文件中添加 + - `.idea/encodings.xml` + - `.idea/misc.xml` + - `.idea/vcs.xml` + - `.idea/workspace.xml` + - 然后在控制台输入`git rm -r --cached .idea` + - 删除`gitee`或`github`上的`.idea`文件 + - `update`一下远端项目 + - `push`代码 +- 我好菜。。。 + +## 2023-03-22 + +- 增加实体类 +- 删除.idea文件 + +## 2023-03-16 + +- 初始化项目 \ No newline at end of file diff --git a/src/main/java/dao/IndentAbstractDAO.java b/src/main/java/dao/IndentAbstractDAO.java index ef85f83..0c2a2a2 100644 --- a/src/main/java/dao/IndentAbstractDAO.java +++ b/src/main/java/dao/IndentAbstractDAO.java @@ -1,4 +1,7 @@ package dao; -public interface IndentAbstractDAO { +import entities.Indent; + +public interface IndentAbstractDAO extends abstractDAO { + } diff --git a/src/main/java/dao/specification/abstractDAO.java b/src/main/java/dao/specification/abstractDAO.java index 1ddd834..1a97ff2 100644 --- a/src/main/java/dao/specification/abstractDAO.java +++ b/src/main/java/dao/specification/abstractDAO.java @@ -1,8 +1,5 @@ package dao.specification; -import java.util.List; - //@TODO 重构代码 合并userDAO和merchantsDAO中重复方法 -public class abstractDAO { - +public class abstractDAO { } diff --git a/src/main/java/dao/specification/indentDAO.java b/src/main/java/dao/specification/indentDAO.java new file mode 100644 index 0000000..229f649 --- /dev/null +++ b/src/main/java/dao/specification/indentDAO.java @@ -0,0 +1,34 @@ +package dao.specification; + +import dao.IndentAbstractDAO; +import entities.Indent; + +import java.util.List; + +public class indentDAO implements IndentAbstractDAO { + + @Override + public int insert(Indent indent) { + return 0; + } + + @Override + public int delete(Indent indent) { + return 0; + } + + @Override + public int update(Indent indent) { + return 0; + } + + @Override + public List search(Indent indent) { + return null; + } + + @Override + public Indent searchID(Long id) { + return null; + } +} diff --git a/src/main/java/dao/specification/indentItemDAO.java b/src/main/java/dao/specification/indentItemDAO.java new file mode 100644 index 0000000..2877a97 --- /dev/null +++ b/src/main/java/dao/specification/indentItemDAO.java @@ -0,0 +1,4 @@ +package dao.specification; + +public class indentItemDAO { +} diff --git a/src/main/java/dao/specification/merchantsDAO.java b/src/main/java/dao/specification/merchantsDAO.java index 8a9c713..ff8165e 100644 --- a/src/main/java/dao/specification/merchantsDAO.java +++ b/src/main/java/dao/specification/merchantsDAO.java @@ -60,6 +60,7 @@ public class merchantsDAO implements merchantsAbstractDAO { } //更新信息 + //@TODO 只能更新所有信息,不能选择性更新 @Override public int update(@NotNull Merchants merchants) { int flag; diff --git a/src/main/java/dao/specification/userDAO.java b/src/main/java/dao/specification/userDAO.java index 1fd7d6b..e24de49 100644 --- a/src/main/java/dao/specification/userDAO.java +++ b/src/main/java/dao/specification/userDAO.java @@ -64,6 +64,7 @@ public class userDAO implements userAbstractDAO { } //更新用户个人信息 + //@TODO 只能更新所有信息,不能选择性更新 @Override public int update(@NotNull Users users) { int flag; @@ -101,10 +102,11 @@ public class userDAO implements userAbstractDAO { while (resultSet.next()) { users = new Users(); - users.setName(resultSet.getString("id")); + users.setId(resultSet.getLong("id")); users.setName(resultSet.getString("name")); users.setPhoneNumber(resultSet.getString("phoneNumber")); users.setAddress(resultSet.getString("address")); + users.setPassword(resultSet.getString("password")); list.add(users); } } catch (SQLException e) { diff --git a/src/main/java/entities/Indent.java b/src/main/java/entities/Indent.java index e9aaf6a..6559f02 100644 --- a/src/main/java/entities/Indent.java +++ b/src/main/java/entities/Indent.java @@ -9,10 +9,10 @@ import java.util.Date; public class Indent { private Long id;//订单ID - private Users userID;//外键,下单用户ID - private Merchants merchantsID;//外键,餐厅ID - private Float allPrice;//菜品总价 Date createdDate;//订单创建时间 String message;//下单备注 + + private Users userID;//外键,下单用户ID + private Merchants merchantsID;//外键,餐厅ID } diff --git a/src/main/java/entities/indentItem.java b/src/main/java/entities/indentItem.java index 74ca446..05a6225 100644 --- a/src/main/java/entities/indentItem.java +++ b/src/main/java/entities/indentItem.java @@ -11,5 +11,5 @@ public class indentItem { private Float finalPrice;//最终价格 private String description;//菜品描述 - private Indent indentID;//订单ID + private Indent indentID;//外键,订单ID } \ No newline at end of file diff --git a/src/main/java/example/App.java b/src/main/java/example/App.java index 9a87d1d..a506b6a 100644 --- a/src/main/java/example/App.java +++ b/src/main/java/example/App.java @@ -1,9 +1,9 @@ package example; -import entities.*; +import static view.shellUI.run; public class App { - public static void main(String[] args) { - System.out.println("你好世界!Hello World!"); + public static void main(String[] args) throws IllegalAccessException { + run(); } } diff --git a/src/main/java/example/shellUI.java b/src/main/java/example/shellUI.java deleted file mode 100644 index fcbbd5f..0000000 --- a/src/main/java/example/shellUI.java +++ /dev/null @@ -1,157 +0,0 @@ -package example; - -import dao.merchantsAbstractDAO; -import dao.specification.merchantsDAO; -import dao.specification.userDAO; -import dao.userAbstractDAO; -import entities.Merchants; -import entities.Users; -import util.Toolset; - -import java.util.Scanner; - -public class shellUI { - public static void main(String[] args) throws IllegalAccessException { - run(); - } - - public static void run() throws IllegalAccessException { - - System.out.println(""" - ------------------------------------------ - ----------------后台管理系统---------------- - ------------------------------------------ - """); - Chose(); - } - - //选项一 - private static void Chose() throws IllegalAccessException { - System.out.println(""" - 请选择你要进行的功能: - 1.商家管理\t2.用户管理\t0.退出 - """); - - switch (Toolset.option()) { - case 0 -> System.exit(0); - //商家 - case 1 -> { - System.out.println(""" - 请选择你想使用的功能: - 1.新建商家\t\t2.删除商家\t\t3.更新信息 - 4.查询所有商家\t5.根据ID查询商家\t0.退出 - """); - merchantsDB(Toolset.option()); - Chose(); - } - //用户 - case 2 -> { - System.out.println(""" - 请选择你想使用的功能: - 1.新建用户\t\t2.删除用户\t\t3.更新信息 - 4.查询所有用户\t5.根据ID查询用户\t0.退出 - """); - userDB(Toolset.option()); - Chose(); - } - } - } - - //商家数据库操作 - private static void merchantsDB(int chose) throws IllegalAccessException { - int flag; - Merchants merchants; - Scanner reader = new Scanner(System.in); - switch (chose) { - case 0 -> System.exit(0); - case 1 -> {//@TODO 输入格式化 不换行 - merchantsAbstractDAO DML_insert = new merchantsDAO(); - System.out.println("请输入:店铺名字|地址|描述|电话"); - merchants = new Merchants(reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine()); - flag = DML_insert.insert(merchants); - System.out.println(flag + "行受影响"); - } - case 2 -> { - merchantsAbstractDAO DML_delete = new merchantsDAO(); - merchants = new Merchants(); - - System.out.println("请输入你要删除商家的ID"); - merchants.setId(reader.nextLong()); - - flag = DML_delete.delete(merchants); - System.out.println(flag + "行受影响"); - } - case 3 -> { - merchantsAbstractDAO DML_update = new merchantsDAO(); - merchants = new Merchants(); - flag = DML_update.update(merchants); - System.out.println(flag + "行受影响"); - } - case 4 -> { - merchantsAbstractDAO DQL_all = new merchantsDAO(); - merchants = new Merchants(); - //格式化输出 - for (Merchants item : DQL_all.search(merchants)) { - System.out.println(Toolset.table(Merchants.class, item)); - } - - } - case 5 -> { - merchantsAbstractDAO DQL_ID = new merchantsDAO(); - new Merchants(); - System.out.println("请输入你要查询的ID:"); - merchants = DQL_ID.searchID(reader.nextLong()); - //格式化输出 - System.out.println(Toolset.table(Merchants.class, merchants)); - } - } - } - - //用户数据库操作 - private static void userDB(int chose) throws IllegalAccessException { - int flag; - Users user; - Scanner reader = new Scanner(System.in); - - switch (chose) { - case 0 -> System.exit(0); - case 1 -> {//@TODO 非换行输入 - userAbstractDAO DML_insert = new userDAO(); - System.out.println("请输入:名字|电话|地址|密码"); - user = new Users(reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine()); - flag = DML_insert.insert(user); - System.out.println(flag + "行受影响"); - } - case 2 -> { - userAbstractDAO DML_delete = new userDAO(); - user = new Users(); - flag = DML_delete.delete(user); - System.out.println(flag + "行受影响"); - } - case 3 -> { - userAbstractDAO DML_update = new userDAO(); - user = new Users(); - flag = DML_update.update(user); - System.out.println(flag + "行受影响"); - } - case 4 -> { - userAbstractDAO DQL_all = new userDAO(); - user = new Users(); - //格式化输出 - for (Users item : DQL_all.search(user)) { - System.out.println(Toolset.table(Users.class, item)); - } - - } - case 5 -> { - userAbstractDAO DQL_ID = new userDAO(); - new Users(); - System.out.println("请输入你要查询的ID:"); - user = DQL_ID.searchID(reader.nextLong()); - //格式化输出 - System.out.println(Toolset.table(Users.class, user)); - } - } - } - -} diff --git a/src/main/java/util/SQLDatabaseConnection.java b/src/main/java/util/SQLDatabaseConnection.java index 6a16325..a967080 100644 --- a/src/main/java/util/SQLDatabaseConnection.java +++ b/src/main/java/util/SQLDatabaseConnection.java @@ -27,16 +27,16 @@ public class SQLDatabaseConnection { } //释放资源 - public static void close(Connection connection, Statement statement, ResultSet resultSet) { + public static void close(Connection conn, Statement st, ResultSet rs) { try { - if (resultSet != null) { - resultSet.close(); + if (rs != null) { + rs.close(); } - if (statement != null) { - statement.close(); + if (st != null) { + st.close(); } - if (connection != null) { - connection.close(); + if (conn != null) { + conn.close(); } } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/view/MerchantsView.java b/src/main/java/view/MerchantsView.java new file mode 100644 index 0000000..1bae1ee --- /dev/null +++ b/src/main/java/view/MerchantsView.java @@ -0,0 +1,61 @@ +package view; + +import dao.merchantsAbstractDAO; +import dao.specification.merchantsDAO; +import entities.Merchants; +import util.Toolset; + +import java.util.Scanner; + +public class MerchantsView { + //商家数据库操作 + protected static void merchantsDB(int chose) throws IllegalAccessException { + int flag; + Merchants merchants; + Scanner reader = new Scanner(System.in); + switch (chose) { + case 0 -> System.exit(0); + case 1 -> {//@TODO 输入格式化 不换行 + merchantsAbstractDAO DML_insert = new merchantsDAO(); + System.out.println("请输入:店铺名字|地址|描述|电话"); + merchants = new Merchants(reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine()); + flag = DML_insert.insert(merchants); + System.out.println(flag + "行受影响"); + } + case 2 -> { + merchantsAbstractDAO DML_delete = new merchantsDAO(); + merchants = new Merchants(); + + System.out.println("请输入你要删除的ID"); + merchants.setId(reader.nextLong()); + + flag = DML_delete.delete(merchants); + System.out.println(flag + "行受影响"); + } + case 3 -> {//@FIXME update没有传值 + merchantsAbstractDAO DML_update = new merchantsDAO(); + merchants = new Merchants(); + flag = DML_update.update(merchants); + System.out.println(flag + "行受影响"); + } + case 4 -> { + merchantsAbstractDAO DQL_all = new merchantsDAO(); + merchants = new Merchants(); + //格式化输出 + for (Merchants item : DQL_all.search(merchants)) { + System.out.println(Toolset.table(Merchants.class, item)); + } + + } + case 5 -> { + merchantsAbstractDAO DQL_ID = new merchantsDAO(); + new Merchants(); + System.out.println("请输入你要查询的ID:"); + merchants = DQL_ID.searchID(reader.nextLong()); + //格式化输出 + System.out.println(Toolset.table(Merchants.class, merchants)); + } + } + } + +} diff --git a/src/main/java/view/UserView.java b/src/main/java/view/UserView.java new file mode 100644 index 0000000..ed59a72 --- /dev/null +++ b/src/main/java/view/UserView.java @@ -0,0 +1,61 @@ +package view; + +import dao.specification.userDAO; +import dao.userAbstractDAO; +import entities.Users; +import util.Toolset; + +import java.util.Scanner; + +public class UserView { + //用户数据库操作 + protected static void userDB(int chose) throws IllegalAccessException { + int flag; + Users user; + Scanner reader = new Scanner(System.in); + + switch (chose) { + case 0 -> System.exit(0); + case 1 -> {//@TODO 非换行输入 + userAbstractDAO DML_insert = new userDAO(); + System.out.println("请输入:名字|电话|地址|密码"); + user = new Users(reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine()); + flag = DML_insert.insert(user); + System.out.println(flag + "行受影响"); + } + case 2 -> { + userAbstractDAO DML_delete = new userDAO(); + user = new Users(); + + System.out.println("请输入你要删除的ID:"); + user.setId(reader.nextLong()); + flag = DML_delete.delete(user); + + System.out.println(flag + "行受影响"); + } + case 3 -> {//@FIXME update没有传值 + userAbstractDAO DML_update = new userDAO(); + user = new Users(); + flag = DML_update.update(user); + System.out.println(flag + "行受影响"); + } + case 4 -> { + userAbstractDAO DQL_all = new userDAO(); + user = new Users(); + //格式化输出 + for (Users item : DQL_all.search(user)) { + System.out.println(Toolset.table(Users.class, item)); + } + + } + case 5 -> { + userAbstractDAO DQL_ID = new userDAO(); + new Users(); + System.out.println("请输入你要查询的ID:"); + user = DQL_ID.searchID(reader.nextLong()); + //格式化输出 + System.out.println(Toolset.table(Users.class, user)); + } + } + } +} diff --git a/src/main/java/view/shellUI.java b/src/main/java/view/shellUI.java new file mode 100644 index 0000000..5cc605b --- /dev/null +++ b/src/main/java/view/shellUI.java @@ -0,0 +1,51 @@ +package view; + +import util.Toolset; + +import static view.MerchantsView.merchantsDB; +import static view.UserView.userDB; + +public class shellUI { + public static void run() throws IllegalAccessException { + + System.out.println(""" + ------------------------------------------ + -----------------后台管理系统--------------- + ------------------------------------------ + """); + Chose(); + } + + //选项一 + private static void Chose() throws IllegalAccessException { + System.out.println(""" + 请选择你要进行的功能: + 1.商家管理\t2.用户管理\t0.退出 + """); + + switch (Toolset.option()) { + case 0 -> System.exit(0); + //商家 + case 1 -> { + System.out.println(""" + 请选择你想使用的功能: + 1.新建商家\t\t2.删除商家\t\t3.更新信息 + 4.查询所有商家\t5.根据ID查询商家\t0.退出 + """); + merchantsDB(Toolset.option()); + Chose(); + } + //用户 + case 2 -> { + System.out.println(""" + 请选择你想使用的功能: + 1.新建用户\t\t2.删除用户\t\t3.更新信息 + 4.查询所有用户\t5.根据ID查询用户\t0.退出 + """); + userDB(Toolset.option()); + Chose(); + } + } + } + +}