简体   繁体   English

从mybatis选择中获取不同java对象的多个值

[英]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: 我试图执行一个查询,该查询从数据库返回与2个表名称user和device相关的数据。我以以下方式使用了mybatis:

<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. 当我尝试从java中访问对象值时,我无法获取值,只能获取放置在最前面的resultMap(userMap)值。 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; 您可以设置resultType="hashmap" ,然后您的结果将是List<HashMap<String, Object>> result; . You can access fields by result.get("DEVICE_ID"), result.get("USER_COMPANY") etc. 您可以通过result.get("DEVICE_ID"), result.get("USER_COMPANY") etc.访问字段result.get("DEVICE_ID"), result.get("USER_COMPANY") etc.

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

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