简体   繁体   English

继承和DTO对象设计

[英]Inheritance and DTO object design

I am developing a standalone J2SE application (Can't add J2EE, Hibernate, Spring, etc...). 我正在开发一个独立的J2SE应用程序(无法添加J2EE,Hibernate,Spring等)。 I need idea on designing the code architecture. 我需要有关设计代码体系结构的想法。

This is my code: 这是我的代码:

class PersonBean {

    String name;
    int id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }   
}


class PersonMapper{
    PersonMapper(Connection conn){

    }

    PersonBean load(ResultSet rs){
        PersonBean entity = new PersonBean();
        entity.setId(rs.getInt("id"));
        entity.setName(rs.getString("name"));
        return entity;
    }

    PersonBean findById(int id){
        String query = "SELECT * FROM Person where id = ?";

        PreparedStatment stmt = conn.getPreparedStatement(query);

        stmt.setInt(1, id);
        ResultSet rs = stmt.executeQuery();

        if (rs.next())
            return load(rs);

        else
            return null;
    }
    List<PersonBean> findByName(String name) {}
}
  1. Created entity classes for each Table (mysql table), with getter and setter, named it as Bean.class (PersonBean.class) 使用getter和setter为每个表(mysql表)创建的实体类,将其命名为Bean.class(PersonBean.class)
  2. Created mapper classes for each table to retrieve records for that table,and populate entity. 为每个表创建映射器类,以检索该表的记录并填充实体。 Named it as Mapper.class (PersonMapper.class). 将其命名为Mapper.class(PersonMapper.class)。

Is there anything do I need to do to improve this design? 我有什么需要改进的设计吗?

In object world Student object extends Person object. 在对象世界中,Student对象扩展了Person对象。 But in database Student and Person are two different tables, which leads to create two different classes for StudentBean and PersonBean where StudentBean will not extend PersonBean. 但是在数据库Student和Person中有两个不同的表,这导致为StudentBean和PersonBean创建两个不同的类,其中StudentBean将不会扩展PersonBean。 Do I need to create business layer on top of entity Bean object layer? 我是否需要在实体Bean对象层之上创建业务层? if so how to design? 如果可以的话,如何设计?

I don't know how to start browse about this, any links also would be fine. 我不知道该如何开始浏览,任何链接也都可以。

If you want to be able to have a hierarchy, you should implement a better mapper, that can do the difference between loading a Person vs a Student bean. 如果希望具有层次结构,则应该实现一个更好的映射器,该映射器可以在加载Person Bean与Student Bean之间产生区别。 A better way would be to implement on your own, something similar to Hibernate or Spring's JdbcTemplate ... which will take you a while. 更好的方法是自己实现,类似于Hibernate或Spring的JdbcTemplate ...,这会花一些时间。

But before that, I would suggest to try out sql2java , that will generate a similar structure as yours, for all the tables that you have, and also lets you to customize it, and if the data structure changes, it can be re-generated. 但是在此之前,我建议尝试使用sql2java ,它将为您拥有的所有表生成与您类似的结构,并且还允许您对其进行自定义,并且如果数据结构发生更改,则可以重新生成它。

I go with the logic said by Albert 我遵循阿尔伯特所说的逻辑

I would recommend the same, you can program using ResultSetMetaData to get all the column/pseudo column names which will map like 我会推荐相同的方法,您可以使用ResultSetMetaData进行编程,以获取将映射为类似的所有列/伪列名称

EMPLOYEE_NAME to employeeName setter method setEmployeeName on the fly

you can use Java Reflection to call a setter method or you can use apache BeanUtils 您可以使用Java Reflection来调用setter方法,也可以使用apache BeanUtils

Either for learning or for real usage, 无论是学习还是实际使用,

It is best to review the Source Generated by DAO Generators. 最好查看DAO生成器生成的源。 You can learn many good things and additionally stay away from Design Failures. 您可以学到很多好东西,并且远离设计失败。

Review the one here: FireStorm 在这里查看以下内容: FireStorm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM