是否有正确的方法在DAO中实例化强类型对象?

时间:2011-05-30 23:07:00

标签: java mysql dao

我正在尝试确定哪种方法是为包含一些原始数据类型的类编码DAO的正确方法,以及三个具有自己的DAO的强类型对象。

这是我正在实现DAO的类,减去getter / setter:

public class Course
{
    private int id;
    private int number;
    private String title;
    private Subject subject;
    private School school;
    private Instructor instructor;
    ...
}

主题,学校和讲师课程都有自己的DAO。所以我最初的想法是使用那些DAO来实例化Course对象中的Subject,School和Instructor对象(相关代码在try块中):

public class CourseDAO
{
    public static Course selectCourse(int id)
    {
        ConnectionPool cp = ConnectionPool.getInstance();
        Connection c = cp.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        Course course = null;

        String query = "select id, number, title, subjectId, schoolId, instructorId " +
                "from course " +
                "where id = ? " +
                "limit 1";

        try
        {
            ps = c.prepareStatement(query);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next())
            {
                course = new Course();
                course.setId(rs.getInt(1));
                course.setNumber(rs.getInt(2));
                course.setTitle(rs.getString(3));
                course.setSubject(SubjectDAO.selectSubject(rs.getInt(4)));
                course.setSchool(SchoolDAO.selectSchool(rs.getInt(5)));
                course.setInstructor(InstructorDAO.selectInstructor(rs.getInt(6)));
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            cp.releaseConnection(c);
        }

        return course;
    }
}

其他DAO看起来与此非常相似。每个从ConnectionPool获取一个Connection,使用PreparedStatement执行简单查询,关闭ResultSet和PreparedStatement,并将Connection释放回池中。

以这种方式实现CourseDAO之后,我开始怀疑在查询中使用连接而不是调用SubjectDAO,SchoolDAO和InstructorDAO是否会更好/更有效,因为每个人都使用来自ConnectionPool的Connection。查询和try块将更改如下:

String query = "select course.id, course.number, course.title, " +
        "subject.id, subject.name, " +
        "school.id, school.name, school.abbreviation, school.type, school.city, school.region, school.country, " +
        "instructor.id, instructor.firstName, instructor.lastName " +
        "from course " +
        "inner join subject " +
        "on course.subjectId = subject.id " +
        "inner join school " +
        "on course.schoolId = school.id " +
        "inner join instructor " +
        "on course.instructorId = instructor.id " +
        "where course.id = ? " +
        "limit 1";

try
{
    ps = c.prepareStatement(query);
    ps.setInt(1, id);
    rs = ps.executeQuery();
    if (rs.next())
    {
        course = new Course();
        course.setId(rs.getInt(1));
        course.setNumber(rs.getInt(2));
        course.setTitle(rs.getString(3));

        Subject subject = new Subject();
        subject.setId(rs.getInt(4));
        subject.setName(rs.getString(5));

        School school = new School();
        school.setId(rs.getInt(6));
        school.setName(rs.getString(7));
        school.setAbbreviation(rs.getString(8));
        school.setType(rs.getString(9));
        school.setCity(rs.getString(10));
        school.setRegion(rs.getString(11));
        school.setCountry(rs.getString(12));

        Instructor instructor = new Instructor();
        instructor.setId(rs.getInt(13));
        instructor.setFirstName(rs.getString(14));
        instructor.setLastName(rs.getString(15));

        course.setSubject(subject);
        course.setSchool(school);
        course.setInstructor(instructor);
    }
}

在这种情况下,哪个是更好的选择?使用一个数据库连接,连接三个表,并返回15个字段?或者使用ConnectionPool中的四个连接(每个DAO使用一个连接),编写更少的代码,并执行简单的查询?

1 个答案:

答案 0 :(得分:2)

你应该定义使用连接方法。对每个属性进行查询是非常低效的,如果你有大量数据,由于n + 1选择类型的问题(在你的情况下为3n + 1),它将导致悲惨的性能。