简体   繁体   中英

getting multiple value for different java object from mybatis select

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());
    // }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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