如何将ResultSet对象转换为一个特定的POJO类对象? [英] How to convert ResultSet object to one specific POJO class object?

查看:1301
本文介绍了如何将ResultSet对象转换为一个特定的POJO类对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 例如,我有一个Resultset,其中包含有关员工详细信息的详细信息,我在数据库中有一个表For Employee,我正在从中选择所有详细信息

  1. For example i have a Resultset which contain Details about Employee details and I have a table For Employee in database and i am selecting all details from that

我有java POJO类,它类似于Employee Table,它是将转换结果集实现为Employee类对象的最佳方法

I have java POJO class which resembles Employee Table what is the best method to implement convert result set into Employee class object

如果我有多个类,如Employee和多个表,那么实现的最佳方法是如何编写可重用的代码。

what is the best way to implement if i have multiple classes Like Employee and multiple tables too how to write a reusable code.

我现在使用以下符号。

public Employee{

    private int empId;

    public setEmpId(int empId){
        this.empId = empId;
    }

    public int getEmpId(){
        return this.empId;
    }
}

public class SetResultSetToEmployee{

    public Employee getEmployee(){
        Employee e = new Employee();
        e.setEmpId(resultSet.getInt("EmpId"));
        return e;
    }
}


推荐答案

Mybatis或者像Hibernate这样的ORM可能是你最好的解决方案。

但是,如果你真的需要使用jdbc的ResultSet,并想将ResultSet转换为java Beans,你可以检查:

Mybatis, or ORM like Hibernate may be your best solution.
But, if you really need to use jdbc's ResultSet, and want to convert ResultSet into java Beans, you can check:


  1. BeanUtils

  2. 或者,自己编写一些代码,如 this

  1. ResultSetDynaClass of BeanUtils from apache.
  2. or, write a bit code yourself like this.

这篇关于如何将ResultSet对象转换为一个特定的POJO类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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