diff --git a/README.md b/README.md
index cd3c994..403e326 100644
--- a/README.md
+++ b/README.md
@@ -1,12 +1,12 @@
# Homework-entities
*网安实训作业*
-# 20230316
+# 2023-03-16
- 初始化项目
-# 20230322
+# 2023-03-22
- 增加实体类
- 删除.idea文件
-# 20230326
+# 2023-03-26
- 完善实体类
- 我靠,折腾了一晚上,提交了十几次,终于提交时把.idea这个文件给忽略了
- 方法:在`.gitgnore`文件中添加
@@ -19,7 +19,20 @@
- `update`一下远端项目
- `push`代码
- 我好菜。。。
-# 20230327
+# 2023-03-27
- 完善了一下实体类,发现商家那一部分漏了一点东西,给它补上了
- 另外就是为什么`.idea`文件我已经取消提交了,为啥本地的`commit`里面还会有有啊……无语了
-- 是不是跟`git`八字不合。。。
\ No newline at end of file
+- 是不是跟`git`八字不合。。。
+# 2023-03-29
+- 添加`JDBC`驱动,连接数据库
+# 2023-05-04
+- 创建数据库表,并成功连接
+- 调整实体类与数据库对应的结构,修改外键
+- 调整项目结构
+- 完成DAO层抽象接口
+- 完成MerchantsDAO和UserDAO具体功能
+# 2023-05-05
+- Debug MerchantsDAO 按ID查询功能
+- 搭建shellUI的大体框架
+- 完成UserDAO具体功能
+- 第一次测试
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index fe2baff..ba0c763 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,36 +1,47 @@
- 4.0.0
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ 4.0.0
- org.example
- JavaEle
- 1.0-SNAPSHOT
- jar
+ org.example
+ JavaElm
+ 1.0-SNAPSHOT
+ jar
- JavaEle
- https://maven.apache.org
+ JavaEle
+ https://maven.apache.org
-
- UTF-8
- 17
- 17
- 17
-
+
+ UTF-8
+ 17.0.6
+ 17.0.6
+ 17.0.6
+
-
-
- junit
- junit
- 4.13.2
- test
-
+
+
+ junit
+ junit
+ 4.13.2
+ test
+
-
- org.projectlombok
- lombok
- true
- 1.18.26
-
+
+ org.projectlombok
+ lombok
+ true
+ 1.18.26
+
-
+
+ com.microsoft.sqlserver
+ mssql-jdbc
+ 12.2.0.jre11
+
+
+ org.jetbrains
+ annotations
+ 20.1.0
+ compile
+
+
diff --git a/src/main/java/dao/IndentAbstractDAO.java b/src/main/java/dao/IndentAbstractDAO.java
new file mode 100644
index 0000000..ef85f83
--- /dev/null
+++ b/src/main/java/dao/IndentAbstractDAO.java
@@ -0,0 +1,4 @@
+package dao;
+
+public interface IndentAbstractDAO {
+}
diff --git a/src/main/java/dao/abstractDAO.java b/src/main/java/dao/abstractDAO.java
new file mode 100644
index 0000000..f167625
--- /dev/null
+++ b/src/main/java/dao/abstractDAO.java
@@ -0,0 +1,13 @@
+package dao;
+
+import java.util.List;
+
+public interface abstractDAO {
+ int insert(T t); //增
+
+ int delete(T t);//删
+
+ int update(T t);//改
+
+ List search(T t);//查
+}
diff --git a/src/main/java/dao/indentItemAbstractDAO.java b/src/main/java/dao/indentItemAbstractDAO.java
new file mode 100644
index 0000000..3e41e28
--- /dev/null
+++ b/src/main/java/dao/indentItemAbstractDAO.java
@@ -0,0 +1,7 @@
+package dao;
+
+import entities.indentItem;
+
+public interface indentItemAbstractDAO extends abstractDAO {
+
+}
diff --git a/src/main/java/dao/merchantsAbstractDAO.java b/src/main/java/dao/merchantsAbstractDAO.java
new file mode 100644
index 0000000..6e27f93
--- /dev/null
+++ b/src/main/java/dao/merchantsAbstractDAO.java
@@ -0,0 +1,8 @@
+package dao;
+
+import entities.Merchants;
+
+public interface merchantsAbstractDAO extends abstractDAO {
+ //根据id查询商家
+ Merchants searchMerchantByID(Long id);
+}
diff --git a/src/main/java/dao/specification/merchantsDAO.java b/src/main/java/dao/specification/merchantsDAO.java
new file mode 100644
index 0000000..9c222c7
--- /dev/null
+++ b/src/main/java/dao/specification/merchantsDAO.java
@@ -0,0 +1,136 @@
+package dao.specification;
+
+import dao.merchantsAbstractDAO;
+import org.jetbrains.annotations.NotNull;
+import entities.Merchants;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.sql.*;
+
+import static util.SQLDatabaseConnection.close;
+import static util.SQLDatabaseConnection.getConnection;
+
+public class merchantsDAO implements merchantsAbstractDAO {
+ private Connection connection = null;
+ private PreparedStatement preparedStatement = null;
+ private ResultSet resultSet = null;
+
+ //通过id寻找商家
+ @Override
+ public Merchants searchMerchantByID(Long id) {
+ Merchants merchants = null;
+ String selectID_sql = "select * from Merchants where id = ?";
+ try {
+ connection = getConnection();
+ preparedStatement = connection.prepareStatement(selectID_sql);
+ preparedStatement.setLong(1, id);
+ resultSet = preparedStatement.executeQuery();
+
+ while (resultSet.next()) {
+ merchants = new Merchants();
+ merchants.setId(resultSet.getLong("id"));
+ merchants.setName(resultSet.getString("name"));
+ merchants.setAddress(resultSet.getString("address"));
+ merchants.setDescription(resultSet.getString("description"));
+ merchants.setPhoneNumber(resultSet.getString("phoneNumber"));
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ } finally {
+ close(connection, preparedStatement, resultSet);
+ }
+ return merchants;
+ }
+
+ //新加商家
+ @Override
+ public int insert(@NotNull Merchants merchants) {
+ int flag = 0;
+ try {
+ connection = getConnection();
+ String insert_sql = "insert into Merchants(name,address,description,phoneNumber) values(?,?,?,?)";
+ preparedStatement = connection.prepareStatement(insert_sql);
+
+ preparedStatement.setString(1, merchants.getName());
+ preparedStatement.setString(2, merchants.getAddress());
+ preparedStatement.setString(3, merchants.getDescription());
+ preparedStatement.setString(4, merchants.getPhoneNumber());
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ } finally {
+ close(connection, preparedStatement, resultSet);
+ }
+ return flag;
+ }
+
+ //删除商家
+ @Override
+ public int delete(Merchants merchants) {
+ int flag = 0;
+ try {
+ connection = getConnection();
+ String delete_sql = "delete from Merchants where id = ?";
+ preparedStatement = connection.prepareStatement(delete_sql);
+ preparedStatement.setLong(1, merchants.getId());
+ flag = preparedStatement.executeUpdate();
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally {
+ close(connection, preparedStatement, resultSet);
+ }
+ return flag;
+ }
+
+ //更新信息
+ @Override
+ public int update(@NotNull Merchants merchants) {
+ int flag;
+ String update_sql = "update Merchants " +
+ "set name = ?, address = ?, description = ?, phoneNumber= ? " +
+ "where id = ?";
+ try {
+ connection = getConnection();
+ preparedStatement = connection.prepareStatement(update_sql);
+
+ preparedStatement.setString(1, merchants.getName());
+ preparedStatement.setString(2, merchants.getAddress());
+ preparedStatement.setString(3, merchants.getDescription());
+ preparedStatement.setString(4, merchants.getPhoneNumber());
+ preparedStatement.setLong(5, merchants.getId());
+
+ flag = preparedStatement.executeUpdate(update_sql);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ return flag;
+ }
+
+ //查询所有商家
+ @Override
+ public List search(Merchants merchants) {
+ List list = new ArrayList<>();
+ try {
+ connection = getConnection();
+ String selectAll_sql = "select * from Merchants";
+ preparedStatement = connection.prepareStatement(selectAll_sql);
+ resultSet = preparedStatement.executeQuery();
+
+ while (resultSet.next()) {
+ merchants.setId(resultSet.getLong("id"));
+ merchants.setName(resultSet.getString("name"));
+ merchants.setPhoneNumber(resultSet.getString("phoneNumber"));
+ merchants.setAddress(resultSet.getString("address"));
+ merchants.setDescription(resultSet.getString("description"));
+ list.add(merchants);
+ }
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ } finally {
+ close(connection, preparedStatement, resultSet);
+ }
+ return list;
+ }
+
+}
diff --git a/src/main/java/dao/specification/userDAO.java b/src/main/java/dao/specification/userDAO.java
new file mode 100644
index 0000000..892a061
--- /dev/null
+++ b/src/main/java/dao/specification/userDAO.java
@@ -0,0 +1,119 @@
+package dao.specification;
+
+import dao.userAbstractDAO;
+import entities.Users;
+import org.jetbrains.annotations.NotNull;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static util.SQLDatabaseConnection.getConnection;
+import static util.SQLDatabaseConnection.close;
+
+public class userDAO implements userAbstractDAO {
+ private Connection connection = null;
+ private PreparedStatement preparedStatement = null;
+ private ResultSet resultSet = null;
+
+ //@TODO UserDAO 插入功能
+ @Override
+ public int insert(Users users) {
+
+ return 0;
+ }
+
+ @Override
+ public int delete(@NotNull Users users) {
+ int flag;
+ try {
+ connection = getConnection();
+ String delete_sql = "delete from Users where id=?";
+ preparedStatement = connection.prepareStatement(delete_sql);
+ preparedStatement.setLong(1, users.getId());
+ flag = preparedStatement.executeUpdate();
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ } finally {
+ close(connection, preparedStatement, resultSet);
+ }
+
+ return flag;
+ }
+
+ @Override
+ public int update(@NotNull Users users) {
+ int flag = 0;
+ try {
+ connection = getConnection();
+ String update_sql = "update Users " +
+ "set name = ?, phoneNumber = ?, address = ? " +
+ "where id = ?";
+ preparedStatement = connection.prepareStatement(update_sql);
+ preparedStatement.setString(1,users.getName());
+ preparedStatement.setString(2,users.getPhoneNumber());
+ preparedStatement.setString(3,users.getAddress());
+ preparedStatement.setLong(4,users.getId());
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ return flag;
+ }
+
+ //查询所有用户
+ @Override
+ public List search(Users users) {
+ List list = new ArrayList<>();
+
+ try {
+ connection = getConnection();
+ String selectAll_sql = "select * from Users";
+ preparedStatement = connection.prepareStatement(selectAll_sql);
+ preparedStatement.executeQuery();
+
+ while (resultSet.next()) {
+ users.setName(resultSet.getString("name"));
+ users.setPhoneNumber(resultSet.getString("phoneNumber"));
+ users.setAddress(resultSet.getString("address"));
+ list.add(users);
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ } finally {
+ close(connection, preparedStatement, resultSet);
+ }
+ return list;
+ }
+
+ //用ID查询用户
+ @Override
+ public Users searchUsersByID(Long id) {
+ Users user = null;
+ String selectID_sql = "select * from Users where id = ?";
+ try {
+ connection = getConnection();
+ preparedStatement = connection.prepareStatement(selectID_sql);
+ preparedStatement.setLong(1, id);
+ resultSet = preparedStatement.executeQuery();
+
+ while (resultSet.next()) {
+ user = new Users();
+ user.setId(resultSet.getLong("id"));
+ user.setName(resultSet.getString("name"));
+ user.setPhoneNumber(resultSet.getString("phoneNumber"));
+ user.setAddress(resultSet.getString("address"));
+ }
+
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ } finally {
+ close(connection, preparedStatement, resultSet);
+ }
+ return user;
+ }
+}
diff --git a/src/main/java/dao/userAbstractDAO.java b/src/main/java/dao/userAbstractDAO.java
new file mode 100644
index 0000000..740ff21
--- /dev/null
+++ b/src/main/java/dao/userAbstractDAO.java
@@ -0,0 +1,8 @@
+package dao;
+
+import entities.Users;
+
+public interface userAbstractDAO extends abstractDAO {
+ //根据ID查询用户
+ Users searchUsersByID(Long id);
+}
diff --git a/src/main/java/entities/Indent.java b/src/main/java/entities/Indent.java
index 75cd81b..e9aaf6a 100644
--- a/src/main/java/entities/Indent.java
+++ b/src/main/java/entities/Indent.java
@@ -2,13 +2,17 @@ package entities;
import lombok.Data;
+import java.util.Date;
+
//订单类
@Data
public class Indent {
private Long id;//订单ID
- private indentItem indentItemID;//外键,菜品ID
+
private Users userID;//外键,下单用户ID
private Merchants merchantsID;//外键,餐厅ID
- private Float allPrice;//菜品总价
+ private Float allPrice;//菜品总价
+ Date createdDate;//订单创建时间
+ String message;//下单备注
}
diff --git a/src/main/java/entities/Merchants.java b/src/main/java/entities/Merchants.java
index 85cdd7e..3785e1b 100644
--- a/src/main/java/entities/Merchants.java
+++ b/src/main/java/entities/Merchants.java
@@ -5,10 +5,19 @@ import lombok.Data;
//商家类
@Data
public class Merchants {
- private Long id;//商家编号
+ private Long id;//主键,商家编号
+
private String name;//店铺名字
private String address;//店铺地址
private String description;//店铺描述
private String phoneNumber;//商家联系方式
- private Indent IndentID;//外键,订单ID
+
+ @Override
+ public String toString() {
+ return "\n商家编号:" + this.id +
+ "\n店铺名字:" + this.name +
+ "\n店铺地址:" + this.address +
+ "\n店铺描述:" + this.description +
+ "\n联系方式:" + this.phoneNumber;
+ }
}
diff --git a/src/main/java/entities/Users.java b/src/main/java/entities/Users.java
index ce8d221..e3d54c8 100644
--- a/src/main/java/entities/Users.java
+++ b/src/main/java/entities/Users.java
@@ -5,8 +5,18 @@ import lombok.Data;
//用户类
@Data
public class Users {
- private Long id;//用户ID
+ private Long id;//主键,用户ID
+ private String password;//用户密码
private String name;//用户姓名
private String phoneNumber;//用户联系方式
private String address;//家庭住址
+
+ @Override
+ public String toString() {
+ return "\n用户编号:" + this.id +
+ "\n用户姓名:" + this.name +
+ "\n家庭地址:" + this.address +
+ "\n联系方式:" + this.phoneNumber;
+ }
+
}
diff --git a/src/main/java/entities/indentItem.java b/src/main/java/entities/indentItem.java
index 1786401..74ca446 100644
--- a/src/main/java/entities/indentItem.java
+++ b/src/main/java/entities/indentItem.java
@@ -4,10 +4,12 @@ import lombok.Data;
//订单中的菜品
@Data
public class indentItem {
- private Long id;//菜品ID
+ private Long id;//主键,菜品ID
private String name;//菜品名字
private Float initialPrice;//菜品原价
private Float discount;//折扣
private Float finalPrice;//最终价格
private String description;//菜品描述
+
+ 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 fba0024..9a87d1d 100644
--- a/src/main/java/example/App.java
+++ b/src/main/java/example/App.java
@@ -1,7 +1,9 @@
package example;
+import entities.*;
+
public class App {
public static void main(String[] args) {
- System.out.println("Hello World!");
+ System.out.println("你好世界!Hello World!");
}
}
diff --git a/src/main/java/example/shellUI.java b/src/main/java/example/shellUI.java
new file mode 100644
index 0000000..f3fb82b
--- /dev/null
+++ b/src/main/java/example/shellUI.java
@@ -0,0 +1,55 @@
+package example;
+
+import com.microsoft.sqlserver.jdbc.StringUtils;
+import dao.*;
+import dao.specification.*;
+
+import java.util.Scanner;
+
+public class shellUI {
+ public static void main(String[] args) {
+ run();
+ }
+
+ public static void run() {
+ Scanner reader = new Scanner(System.in);
+ System.out.println("""
+ ------------------------------------------
+ ----------------后台管理系统----------------
+ ------------------------------------------
+ """);
+ System.out.println("""
+ 请选择你要进行的功能
+ 1.商家管理
+ 2.用户管理
+ """);
+ //
+ int chose1;
+ while (true) {
+ if (reader.hasNextInt()) {
+ chose1 = reader.nextInt();
+ break;
+ } else {
+ System.out.println("你的输入有误,请重新输入!");
+ }
+ }
+ //@TODO 2023-05-05 shellUI功能选择
+ int chose2;
+ switch (chose1) {
+ case 1:
+ System.out.println("""
+ 请选择你想使用的功能:
+ 1.
+ 2.
+ 3.
+ 4.
+ 5.
+ """);
+
+ break;
+ case 2:
+ }
+
+
+ }
+}
diff --git a/src/main/java/util/SQLDatabaseConnection.java b/src/main/java/util/SQLDatabaseConnection.java
new file mode 100644
index 0000000..d44d04c
--- /dev/null
+++ b/src/main/java/util/SQLDatabaseConnection.java
@@ -0,0 +1,47 @@
+package util;
+
+import java.sql.*;
+
+public class SQLDatabaseConnection {
+ // Connect to database
+ private static final String connectionUrl =
+ "jdbc:sqlserver://152.136.182.168:1433;"
+ + "database=Elm;"
+ + "user=guest@152.136.182.168;"
+ + "password=20230504#Guest;"
+ + "encrypt=true;"
+ + "trustServerCertificate=false;"
+ + "loginTimeout=30;";
+
+ public static Connection getConnection() {
+ Connection connection = null;
+ try {
+ // Code here.
+ connection = DriverManager.getConnection(connectionUrl);
+ }
+ // Handle any errors that may have occurred.
+ catch (SQLException e) {
+ e.printStackTrace();
+ }
+ return connection;
+ }
+
+ //释放资源
+ public static void close(Connection connection, Statement statement, ResultSet resultSet) {
+ try {
+ if (resultSet != null) {
+ resultSet.close();
+ }
+ if (statement != null) {
+ statement.close();
+ }
+ if (connection != null) {
+ connection.close();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+}