java - 在 HashMap Key 中输入 3 个属性

标签 java mysql json hashmap

我是 Java 新手。我现在使用 HashMap 来存储来自 MySQL 数据库的数据,我将使用 JSON POST 请求从用户那里获取输入并在 HashMap 中搜索相关数据并从 HashMap 中检索。我需要来自用户的三个输入,但在 HashMap 中只能输入 1 个键。所以,我尝试了将 key 作为对象输入的方法,但它不起作用。下面是我从 MySQL 数据库存储数据的代码。

public class AppDataService {
HashMap<AppDataRequest, AppData> appdatas = new HashMap<AppDataRequest, AppData>();

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://****:3306/****_demo";

static final String USER = "****";
static final String PASS = "****";

public AppDataService(){
    Connection conn = null;
    Statement stat = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        stat = conn.createStatement();
        String sql = "SELECT * FROM testdata";
        ResultSet resu = stat.executeQuery(sql);
        while(resu.next()){
            int id = resu.getInt("app_id");
            String email = resu.getString("email");
            String password = resu.getString("password");
            String status = resu.getString("status");
            String message = resu.getString("message");
            String token = resu.getString("token");
            appdatas.put(new AppDataRequest(id, email, password), new AppData(status, message, token));
        }
        resu.close();
        stat.close();
        conn.close();
    }
    catch(SQLException se){
        se.printStackTrace();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    finally{
        try{
            if(stat!=null){
                stat.close();
            }
        }
        catch (SQLException se2){

        }
        try{
            if(conn!=null){
                conn.close();
            }
        }
        catch(SQLException se3){
            se3.printStackTrace();
        }
    }       
}

    public List<AppData> getAllAppData(){
        return new ArrayList<AppData>(appdatas.values());
    }

    public AppData getAppData(int id){
        return appdatas.get(id);
    }

    public AppData getSAppData(int id, String email, String password){
        return appdatas.get(new AppDataRequest (id, email, password));
    }
}

我的 JSon POST 代码

@Path("/appdata")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)

AppDataService ads = new AppDataService();

@POST
@Path("/appdatas")
public AppData getSAppData(AppDataRequest adr){
    return ads.getSAppData(adr.getId(), adr.getEmail(),adr.getPassword());
}

应用数据类

public class AppData {
    public String status;
    public String message;
    public String token;

    public AppData(){

    }   

    public AppData(String status, String message, String token) {
        this.status = status;
        this.message = message;
        this.token = token;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

AppDataRequest 类

public class AppDataRequest {
    public int id;
    public String email;
    public String password;

    public AppDataRequest(){

    }

    public AppDataRequest(int id, String email, String password) {
        this.id = id;
        this.email = email;
        this.password = password;
    }

    public int getId() {
        return id;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

最佳答案

你可以在你的类中覆盖 hashCode 和 equals :

@Override
public int hashCode() {
    int hash = 3;
    hash = 83 * hash + this.id;
    hash = 83 * hash + Objects.hashCode(this.email);
    hash = 83 * hash + Objects.hashCode(this.password);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final AppDataRequest other = (AppDataRequest) obj;
    if (this.id != other.id) {
        return false;
    }
    if (!Objects.equals(this.email, other.email)) {
        return false;
    }
    if (!Objects.equals(this.password, other.password)) {
        return false;
    }
    return true;
}

如果 id 在您的表中是唯一的,为什么不将其作为键,而您的其他信息作为 map 的值:

Map<Integer, AppData> appdatas;
appdatas.put(id, new AppData(status, message, token, email, password));

关于java - 在 HashMap Key 中输入 3 个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43251702/

相关文章:

mysql - InnoDB 经常崩溃

json - 如何在 Kotlin 的 Volley 请求中在 Url 中添加正文?

java - Jadira 6.0 与 Hibernate 4.3 集成

java - 使用 VLCJ 和 JMF 在 Linux 中播放视频时遇到问题

java - mysql.jdbc 异常超出数据类型 INTEGER 的有效范围

php - 通过 PHP 将 MySQL 数据库显示为 HTML 编码表

java - 使用Java扫描条形码后获取额外的字母

PHP PDO 查询等待不同 MySQL 连接上的请求,返回 "Mysql Server Has Gone Away"

javascript - 使用 $.each 将值放入数组对象中

asp.net-mvc - 无法将 PartialView 返回到 jQuery.ajax 调用