UserDAO以及MerchantsDAO抽象接口的实现,ShellUI的部分功能
This commit is contained in:
parent
460f276596
commit
135e2156dd
21
README.md
21
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`八字不合。。。
|
||||
# 2023-03-29
|
||||
- 添加`JDBC`驱动,连接数据库
|
||||
# 2023-05-04
|
||||
- 创建数据库表,并成功连接
|
||||
- 调整实体类与数据库对应的结构,修改外键
|
||||
- 调整项目结构
|
||||
- 完成DAO层抽象接口
|
||||
- 完成MerchantsDAO和UserDAO具体功能
|
||||
# 2023-05-05
|
||||
- Debug MerchantsDAO 按ID查询功能
|
||||
- 搭建shellUI的大体框架
|
||||
- 完成UserDAO具体功能
|
||||
- 第一次测试
|
19
pom.xml
19
pom.xml
@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>JavaEle</artifactId>
|
||||
<artifactId>JavaElm</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
@ -12,9 +12,9 @@
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17.0.6</maven.compiler.source>
|
||||
<maven.compiler.target>17.0.6</maven.compiler.target>
|
||||
<java.version>17.0.6</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@ -32,5 +32,16 @@
|
||||
<version>1.18.26</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.microsoft.sqlserver</groupId>
|
||||
<artifactId>mssql-jdbc</artifactId>
|
||||
<version>12.2.0.jre11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
<version>20.1.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
4
src/main/java/dao/IndentAbstractDAO.java
Normal file
4
src/main/java/dao/IndentAbstractDAO.java
Normal file
@ -0,0 +1,4 @@
|
||||
package dao;
|
||||
|
||||
public interface IndentAbstractDAO {
|
||||
}
|
13
src/main/java/dao/abstractDAO.java
Normal file
13
src/main/java/dao/abstractDAO.java
Normal file
@ -0,0 +1,13 @@
|
||||
package dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface abstractDAO<T> {
|
||||
int insert(T t); //增
|
||||
|
||||
int delete(T t);//删
|
||||
|
||||
int update(T t);//改
|
||||
|
||||
List<T> search(T t);//查
|
||||
}
|
7
src/main/java/dao/indentItemAbstractDAO.java
Normal file
7
src/main/java/dao/indentItemAbstractDAO.java
Normal file
@ -0,0 +1,7 @@
|
||||
package dao;
|
||||
|
||||
import entities.indentItem;
|
||||
|
||||
public interface indentItemAbstractDAO extends abstractDAO<indentItem> {
|
||||
|
||||
}
|
8
src/main/java/dao/merchantsAbstractDAO.java
Normal file
8
src/main/java/dao/merchantsAbstractDAO.java
Normal file
@ -0,0 +1,8 @@
|
||||
package dao;
|
||||
|
||||
import entities.Merchants;
|
||||
|
||||
public interface merchantsAbstractDAO extends abstractDAO<Merchants> {
|
||||
//根据id查询商家
|
||||
Merchants searchMerchantByID(Long id);
|
||||
}
|
136
src/main/java/dao/specification/merchantsDAO.java
Normal file
136
src/main/java/dao/specification/merchantsDAO.java
Normal file
@ -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<Merchants> search(Merchants merchants) {
|
||||
List<Merchants> 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;
|
||||
}
|
||||
|
||||
}
|
119
src/main/java/dao/specification/userDAO.java
Normal file
119
src/main/java/dao/specification/userDAO.java
Normal file
@ -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<Users> search(Users users) {
|
||||
List<Users> 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;
|
||||
}
|
||||
}
|
8
src/main/java/dao/userAbstractDAO.java
Normal file
8
src/main/java/dao/userAbstractDAO.java
Normal file
@ -0,0 +1,8 @@
|
||||
package dao;
|
||||
|
||||
import entities.Users;
|
||||
|
||||
public interface userAbstractDAO extends abstractDAO<Users> {
|
||||
//根据ID查询用户
|
||||
Users searchUsersByID(Long id);
|
||||
}
|
@ -2,12 +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;//菜品总价
|
||||
Date createdDate;//订单创建时间
|
||||
String message;//下单备注
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
}
|
@ -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!");
|
||||
}
|
||||
}
|
||||
|
55
src/main/java/example/shellUI.java
Normal file
55
src/main/java/example/shellUI.java
Normal file
@ -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:
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
47
src/main/java/util/SQLDatabaseConnection.java
Normal file
47
src/main/java/util/SQLDatabaseConnection.java
Normal file
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user