将ResultSet映射到Pojo对象 [英] Mapping ResultSet to Pojo Objects

查看:144
本文介绍了将ResultSet映射到Pojo对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那真是令人尴尬我已经制作了一个标准的pojo类及其dao类用于数据检索。我很难理解如何处理自定义查询数据到Pojo类的基本过程。

Well that's really embarrassing I have made a standard pojo class and its dao class for data retrieval purpose. I am having a difficulty to understand a basic procedure to how to handle a customized query data to Pojo class.

假设我的用户类是

public class User{

private int userId;
private String username;
private int addressId;

}

public class Address{
private int addressId;
private String zip;
}
public class UserDAO{

public void getUserDetails(){

String getSql = select u.userId, u.username, a.zipcode from user u, address a where u.addressId =     a.addressId;

 //no pojo class is now specific to the resultset returned. so we can't map result to pojo object
}

}

现在我应该如何使用我的pojo类对其进行建模,就好像使用String来管理这个面向对象的概念消失一样,未来复杂性也会增加。亲切的指导!

now how I should model this with my pojo class as if using String to manage this then concept of object oriented vanishes, also complexity would increase in the future as well. kindly guide!

进一步解释的更新

我们知道我们可以映射具有相同pojo类的相同表对象,但是当自定义查询并且返回的数据未映射到任何特定类时,那么该过程是什么?即我们应该再上一堂课?或者我们应该将这些数据放在String变量中?请给出一些例子。

We know that we can map same table objects with same pojo class, but when the query is customized and there is a data returned which doesn't map to any specific class then what would be the procedure? i.e. should we make another class? or should we throw that data in a String variable? kindly give some example as well.

推荐答案

为此你可以使用 JPA 。但是,如果您想手动完成,我会给您一些小例子。

For this purpose you can use one of implementation of JPA. But as you want to do it manually I will give you small example.

UPD:

public class User {
   private int userId;
   private String username;
   private Address address; // USE POJO not ID
}

public class Address{
   private int addressId;
   private String zip;
   List<User> users;
}
    public User getUserById(Connection con, long userId) {
        PreparedStatement stmt;
        String query = "select u.user_id, u.user_name, a.id, a.zip from user u, address a where a.address_id = u.id and u.id = ?";
        User user = new User();
        Address address = new Address;
        try {
            stmt = con.prepareStatement(query);
            stmt.setLong(1, userId);
            ResultSet rs = stmt.executeQuery();
            address.setId(rs.getInt("id"));
            address.setZip(rs.getString("zip");
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("user_name"));
            user.setAddressId(rs.getInt("address_id"));
            user.setAddress(address); // look here
        } catch (SQLException e) {
            if (con != null) {
                try {
                    System.err.print("Transaction is being rolled back");
                    con.rollback();
                } catch (SQLException excep) {
                }
            }
        } finally {
            if (stmt != null) {
                stmt.close();
            }
        }
        return user;
    }

你不应该不为该查询做新的POJO,你应该编写正常的查询。记住 - 你的对象模型是主要的,数据库中的表只是一种保存应用程序数据的方法。

这篇关于将ResultSet映射到Pojo对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆