Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:34:24+00:00 2026-06-06T13:34:24+00:00

I am trying to implement MyBatis in my project at work. It is a

  • 0

I am trying to implement MyBatis in my project at work. It is a legacy system, which uses vanilla JDBC to access the database, solely through stored procedures. I understand that to call a stored procedure, MyBatis requires an object which contains the input parameters for the stored procedure and another that will hold the result set. Not sure if this is entirely true.

To prevent creating too many data entities in the system, I want to reuse the existing ones. And here is where the problem arises. Let me explain what the typical situation/scenario I am facing, and then how I am trying to solve it.

Let’s say I have the following data entity(ies) in the system:

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
}

I am using annotations, so my Mapper class has something like this:

@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);

Problem: When I make the call to the stored procedure, the Account object has the state always null.

To add to the injury, I do not have access to the source of the above data entities. So I couldn’t try the solution provided on this link either – Mybatis select with nested objects

My query:

  • Is it possible for me to use the data entites already present in the system, or do I have to create new ones, and then map the data to the existing ones?
    • If yes, how do I go about it? Any references, if any.
    • If no, is there a way to reduce the number of data entities I would create to call the stored procedures (for both in and out parameters)?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:34:25+00:00Added an answer on June 6, 2026 at 1:34 pm

    I think the best solution for your situation (if I understand it correctly) is to use a MyBatis TypeHandler that will map the state column to an OrganizationAddress object.

    I’ve put together a example based on the information you provided and it works. Here is the revised annotated 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);
    

    You need to map the address field of Account to the “state” column and use a TypeHandler to create an OrganizationAddress with its “state” property filled in.

    The OrgAddressTypeHandler I created looks like this:

    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
      }
    } 
    

    If you need a more complete working example than this, I’ll be happy to send more of it. Or if I have misunderstood your example, let me know.

    With this solution you can use your domain objects without modification. You just need the TypeHandler to do the mapping and you don’t need an XML mapper file.

    Also I did this with MyBatis-3.1.1 in MySQL. Here is the simple schema and stored proc I created to test it:

    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 ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to create a Spring project that uses MyBatis for the
Im trying to implement multi-language support in my system, the other systems at work
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
Trying to implement a rating system of users and postings. What is the best
Trying to implement a shell, mainly piping. I've written this test case which I
I'm trying implement my project in Apache Struts 2 but I'm not very familiar
am trying to implement fluent nhibernate in MVC project...there were no build errors... but
I am trying implement Using local storage with BLOB storage . I went through
I trying to implement the url template tag into my project. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.