简体   繁体   English

具有类层次结构的 Spring JDBC RowMapper

[英]Spring JDBC RowMapper with Class Hierarchies

I wanted to know what the community considers the "best practices" in respect to mapping class hierarchies with Spring JDBC.我想知道社区认为关于使用 Spring JDBC 映射类层次结构的“最佳实践”是什么。

We do not have the ability to use a full fledged ORM tool, however we are using the Spring JDBC to alleviate some of the tedious nature of JDBC.我们没有能力使用成熟的 ORM 工具,但是我们使用 Spring JDBC 来减轻 JDBC 的一些乏味本质。 One class which we leverage very regularly are the BeanPropertyRowMapper for it's ease of use and the ability to have type insensitive bean property access from our result set.我们经常使用的一个类是 BeanPropertyRowMapper,因为它易于使用,并且能够从我们的结果集中访问类型不敏感的 bean 属性。

I have a class hierarchy that all maps back to a single table (taking the table-per-hiearchy approach for this small class hierarchy).我有一个类层次结构,它全部映射回单个表(对这个小类层次结构采用 table-per-hiearchy 方法)。 As such, the table contains an classId column which can be used to determine what class should actually be instantiated.因此,该表包含一个 classId 列,可用于确定应实际实例化哪个类。 Ex.前任。 1 = Manager, 2 = Employee, 3 = Contractor. 1 = 经理,2 = 员工,3 = 承包商。 All of these are "People" but each subclass of person has a few attributes which are unique to their class.所有这些都是“人”,但人的每个子类都有一些对他们的类来说是独一无二的属性。

My initial thought is to create a subclass of BeanPropertyRowMapper and try and inject this logic to say "if column A = 1 then instantiate a Manager and then do your nomral binding".我最初的想法是创建 BeanPropertyRowMapper 的子类并尝试注入此逻辑以说“如果列 A = 1,则实例化一个 Manager,然后进行正常绑定”。

Does this seem like a reasonable approach?这看起来是一个合理的方法吗? Are there any other suggestions people may have that have worked for you?人们是否还有其他对您有用的建议?

It doesn't look like there's a place in the subclass where you could add a hook to switch the class without completely copying the implementation of mapRow() for BeanPropertyRowMapper.看起来子类中没有一个地方可以添加一个钩子来切换类,而无需完全复制 BeanPropertyRowMapper 的 mapRow() 的实现。 Your best approach might be to create a RowMapper class that delegates to the appropriate BeanPropertyRowMapper.您最好的方法可能是创建一个委托给适当的 BeanPropertyRowMapper 的 RowMapper 类。

For example:例如:

    final RowMapper managerMapper = new BeanPropertyRowMapper(Manager.class);
    final RowMapper employeeMapper = new BeanPropertyRowMapper(Employee.class);
    final RowMapper contractorMapper = new BeanPropertyRowMapper(Contractor.class);

    RowMapper rm = new RowMapper()
    {
        @Override
        public Object mapRow(ResultSet rs, int rowNum)
            throws SQLException
        {
            int employeeType = rs.getInt("type");
            switch (employeeType)
            {
                case 1:
                    return managerMapper.mapRow(rs, rowNum); 

                case 2:
                    return employeeMapper.mapRow(rs, rowNum);

                case 3:
                    return contractorMapper.mapRow(rs, rowNum);

                default:
                    break;

            }
        }
    };

I'm not sure it's the 'best practice' but i suggest the following approach (without using bean properties --> should work faster).我不确定这是“最佳实践”,但我建议采用以下方法(不使用 bean 属性 --> 应该工作得更快)。

Usually you know what kind of object you expect to retrieve.通常,您知道希望检索什么类型的对象。 So you can provide corresponding row mapper when execute the sql.所以你可以在执行sql时提供相应的行映射器。

Declare custom abstract generic RowMapper and create own row mapper for each type of person, ie:声明自定义抽象通用 RowMapper 并为每种类型的人创建自己的行映射器,即:

private static abstract class PersonRowMapper<T extends Person> implements RowMapper<T> {

 @Override
 public abstract T mapRow(ResultSet rs, int rowNum) throws SQLException;

 protected void mapBase(ResultSet rs, T person) throws SQLException {
  //base mapping here
 }
}


private static class EmployeeRowMapper extends PersonRowMapper<Employee> {

 @Override
 public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
  Employee e = new Employee();
  mapBase(rs, e);
  //set other specific employee props
 }
}

By other approach you can declare abstract method in base mapper for specific props, ie通过其他方法,您可以在基本映射器中为特定道具声明抽象方法,即

private static abstract class PersonRowMapper<T extends Person> implements RowMapper<T> {
 @Override
 public T mapRow(ResultSet rs, int rowNum) throws SQLException {
  T instance = getInstance();
  //set base props here
  fill(rs, instance);
 }

 //e.g. return new Employee()
 protected abstract T getInstance();
 //fill specific instance props
 protected abstract void fill(ResultSet rs, T instance) throws SQLException;
}

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

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