将结果从SP映射到复杂对象

时间:2012-06-22 13:03:01

标签: java mapping resultset ibatis mybatis

我正在尝试在我的项目中实施MyBatis。它是一个遗留系统,它使用vanilla JDBC来访问数据库,仅通过存储过程。我理解,要调用存储过程,MyBatis需要一个包含存储过程的输入参数的对象和另一个包含结果集的对象。不确定这是否完全正确。

为了防止在系统中创建过多的数据实体,我想重用现有的数据实体。这就是问题出现的地方。让我解释一下我面临的典型情况/场景,以及我如何解决它。

假设我在系统中有以下数据实体:

class Account {
    private int accountID;
    private String accountName;
    private OrganizationAddress address;
    // Getters-Setters Go Here
}
class OrganizationAddress extends Address {
    // ... some attributes here
    // Getters-Setters Go Here
}
class Address {
    private String address;
    private String city;
    private String state;
    private String country;
    // Getters-Setters Go Here
}

我正在使用注释,所以我的Mapper类有这样的东西:

@Select(value = "{call Get_AccountList(#{accountType, mode=IN, jdbcType=String})}")
@Options(statementType = StatementType.CALLABLE)
@Results(value = {
    @org.apache.ibatis.annotations.Result
        (property = "accountID", column = "Account_ID"),
    @org.apache.ibatis.annotations.Result
        (property = "accountName", column = "Organization_Name"),
    @org.apache.ibatis.annotations.Result
        (property = "state", column = "State", javaType=OrganizationAddress.class)
    })
List<Account> getAccountList(Param param);

问题:当我调用存储过程时,Account对象始终state null

为了增加伤害,我无法访问上述数据实体的来源。所以我无法尝试此链接上提供的解决方案 - Mybatis select with nested objects

我的查询:

  • 我是否可以使用系统中已存在的数据,或者我是否必须创建新数据,然后将数据映射到现有数据?
    • 如果是,我该如何解决?任何参考文献,如果有的话。
    • 如果不是,有没有办法减少我为调用存储过程而创建的数据实体的数量(对于in和out参数)?

1 个答案:

答案 0 :(得分:4)

我认为针对您的情况的最佳解决方案(如果我理解正确的话)是使用将状态列映射到OrganizationAddress对象的MyBatis TypeHandler。

我根据您提供的信息汇总了一个示例,并且它有效。这是经过修改的带注释的Mapper:

// Note: you have an error in the @Select line => maps to VARCHAR not "String"
@Select(value = "{call Get_AccountList(#{accountType, mode=IN, jdbcType=VARCHAR})}")
@Options(statementType = StatementType.CALLABLE)
@Results(value = {
  @org.apache.ibatis.annotations.Result
      (property = "accountID", column = "Account_ID"),
  @org.apache.ibatis.annotations.Result
      (property = "accountName", column = "Organization_Name"),
  @org.apache.ibatis.annotations.Result
      (property = "address", column = "State", typeHandler=OrgAddressTypeHandler.class)
  })
List<Account> getAccountList(Param param);

您需要将Account的地址字段映射到“state”列,并使用TypeHandler创建一个填充了“state”属性的OrganizationAddress。

我创建的OrgAddressTypeHandler如下所示:

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

public class OrgAddressTypeHandler extends BaseTypeHandler<OrganizationAddress> {

  @Override
  public OrganizationAddress getNullableResult(ResultSet rs, String colName) throws SQLException {
    OrganizationAddress oa = new OrganizationAddress();
    oa.setState(rs.getString(colName));
    return oa;
  }

  @Override
  public OrganizationAddress getNullableResult(ResultSet rs, int colNum) throws SQLException {
    OrganizationAddress oa = new OrganizationAddress();
    oa.setState(rs.getString(colNum));
    return oa;
  }

  @Override
  public OrganizationAddress getNullableResult(CallableStatement cs, int colNum) throws SQLException {
    OrganizationAddress oa = new OrganizationAddress();
    oa.setState(cs.getString(colNum));
    return oa;
  }

  @Override
  public void setNonNullParameter(PreparedStatement arg0, int arg1, OrganizationAddress arg2, JdbcType arg3) throws SQLException {
    // not needed for this example
  }
} 

如果您需要一个比这更完整的工作示例,我会很乐意发送更多。或者如果我误解了你的例子,请告诉我。

使用此解决方案,您无需修改​​即可使用域对象。您只需要使用TypeHandler进行映射,而不需要XML映射器文件。

我也是用MySQL中的MyBatis-3.1.1做的。这是我为测试它而创建的简单模式和存储过程:

DROP TABLE IF EXISTS account;
DROP TABLE IF EXISTS organization_address;

CREATE TABLE account (
  account_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  organization_name VARCHAR(45) NOT NULL,
  account_type VARCHAR(10) NOT NULL,
  organization_address_id SMALLINT UNSIGNED NOT NULL,
  PRIMARY KEY  (account_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE organization_address (
  organization_address_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  address VARCHAR(45) NOT NULL,
  city VARCHAR(45) NOT NULL,
  state VARCHAR(45) NOT NULL,
  country VARCHAR(45) NOT NULL,
  PRIMARY KEY  (organization_address_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO organization_address VALUES(1, '123 Foo St.', 'Foo City', 'Texas', 'USA');
INSERT INTO organization_address VALUES(2, '456 Bar St.', 'Bar City', 'Arizona', 'USA');
INSERT INTO organization_address VALUES(3, '789 Quux Ave.', 'Quux City', 'New Mexico', 'USA');

INSERT INTO account VALUES(1, 'Foo',  'Type1', 1);
INSERT INTO account VALUES(2, 'Bar',  'Type1', 2);
INSERT INTO account VALUES(3, 'Quux', 'Type2', 3);

DROP PROCEDURE IF EXISTS Get_AccountList;

DELIMITER $$

CREATE PROCEDURE Get_AccountList(IN p_account_type VARCHAR(10))
READS SQL DATA
BEGIN
     SELECT a.account_id, a.organization_name, o.state
     FROM account a
     JOIN organization_address o ON a.organization_address_id = o.organization_address_id
     WHERE account_type = p_account_type
     ORDER BY a.account_id;
END $$

DELIMITER ;