0

I trying to execute a query which returns the data related with 2 tables name user and device from database.I've used mybatis in the folowing way:

<resultMap id="userMap" type="com.motilink.server.User">
    <result property="company" column="USER_COMPANY" />
</resultMap>

<resultMap id="deviceMap" type="com.motilink.server.Device">
    <result property="deviceId" column="DEVICE_ID" />
    <result property="userId" column="USER_ID" />
</resultMap>

<select id="selectDeviceUser" resultMap="userMap,deviceMap">

    select device.deviceId
    as DEVICE_ID,
    user.company
    USER_COMPANY,
    device.userId as USER_ID from
    device,user where user.id =
    device.userId

</select>

When i try to access the object value from the java , i cannot get values only the resultMap placed first (userMap) values can be obtained. Please tell me what i am doing wrong.

Code to access the result map:

        SqlSession session = MyBatisSqlSessionFactory.getSqlSessionFactory()
            .openSession();
    List<User> urs = (List<User>) session.selectList("selectDeviceUser",
            null);
    for (User u : urs) {
        System.out.println("Company: " + u.getCompany());
    }

    // List<Device> devices = (List<Device>) session.selectList(
    // "selectDeviceUser", null);
    // for (Device d : devices) {
    // System.out.println("Device ID: " + d.getDeviceId());
    // }

1 Answer 1

1

You could set resultType="hashmap" and then your result would be List<HashMap<String, Object>> result;. You can access fields by result.get("DEVICE_ID"), result.get("USER_COMPANY") etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.